I want to read a card from Samsung Nexus phone but the android nfc api does not provide enough options. i've also tried using a third party api names "open nfc" but the it gives error of not supporting the api. Can anyone provide me the code to read data from a card. I have the code to read a tag but not to read a card. Here is the link to download the open nfc api.
http://sourceforge.net/projects/open-nfc/files/Open%20NFC%204.3%20beta%20%2810381%29/
Any help is appreciated.
This is the code i used. Its giving an error of opennfc failing...
public class NFCone extends Activity implements CardDetectionEventHandler, ReadCompletionEventHandler,NfcTagDetectionEventHandler{
CardListenerRegistry i=null;
CardDetectionEventHandler hand=null;
NfcManager nfcMngr = null;
NfcTagManager mNfcTagManager=null;
NfcTagDetectionEventHandler tagHand=null;
ReadCompletionEventHandler readHand=null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
System.out.println("onCreate");
try
{
nfcMngr.start();
i.registerCardListener(NfcPriority.MAXIMUM, hand);
}
catch(Exception e)
{
}
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
Toast.makeText(this, "NDEF reader Starting ... ", Toast.LENGTH_SHORT)
.show();
try{
if (mNfcTagManager != null) {
mNfcTagManager.registerTagListener(NfcPriority.MAXIMUM, tagHand);
mNfcTagManager.registerMessageReader(NdefTypeNameFormat.WELL_KNOWN,
"U", NfcPriority.MINIMUM, this);
}
}
catch(Exception e)
{ }
}
protected void onPause()
{
super.onPause();
System.out.println("onPause");
mNfcTagManager.unregisterMessageReader(readHand);
mNfcTagManager.unregisterTagListener(tagHand);
}
protected void onDestroy()
{
super.onDestroy();
System.out.println("onDestroy");
i.unregisterCardListener(hand);
try{
nfcMngr.stop();
}
catch(Exception e)
{ }
}
public void onCardDetected(Connection connection) {
System.out.println("onCardDetected");
//ConnectionProperty[] con = connection.getProperties();
}
public void onCardDetectedError(NfcErrorCode what) {
System.out.println("onCardDetectedError");
// TODO Auto-generated method stub
}
private void startBrowserOn(String url) {
System.out.println("startBrowserOn");
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
public void onReadError(NfcErrorCode what) {
System.out.println("onReadError");
}
public void onTagRead(NdefMessage message) {
{
System.out.println("onTagRead");
if (message != null) {
Vector<NdefRecord> records = message.getRecords();
for (int i = 0; i < records.size(); i++) {
if (UriRecord.isUriRecord(records.elementAt(i))) {
UriRecord uri;
try {
try {
uri = new UriRecord(records.elementAt(i));
startBrowserOn(uri.getUri().toString());
} catch (NfcException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "URISyntaxException! ",
Toast.LENGTH_SHORT).show();
}
break;
}}}}
}
public void onTagDetected(NfcTagConnection connection) {
System.out.println("onTagDetected");
}
public void onTagDetectedError(NfcErrorCode what) {
System.out.println("onTagDetectedError");
}
}
The OpenNFC stack SDK add-on package provided on sourceforg comes with a custom kernel which emulates the NFC controller (see the SDK doc that comes with the package). So I doubt if you are talking to the actual HW without changing the driver (open_nfc_driver.ko) to do so. The Porting Guide (also included in the package) discusses how to port the driver to real hardware (section 4.5 Adapting the porting to a real hardware platform).
You need to add the opennfc library to your Eclipse project.
Right click on the project and select
Properties
, the go toJava Build Path|Libraries
. ClickAdd external JARs
and select your opennfc library. Finally, Click on theOrder and Export
tab and make sure that your opennfc library is checked for export.You cannot read a card using Nexus S while using Open NFC. At least not yet.
The Open NFC stack is hardware independant and uses an abstraction layer, called the HAL. The only available HALs for Open NFC are for Inside Secure hardware, while Nexus S uses NXP hardware.
You dont write what kind of card you are trying to read, but probably its achievable by using the Android api. Among other, I was able to read from and write to a MIFARE card.
You need to filter for technology discovered
then, in the
onNewIntent
methodDefaultAnthenticate is my method that authenticates using default MIFARE keys:
The code could use some refactoring and is a bit chopped up, but i think it shows what's going on. I hope it will help you on your own search - i suggest visiting http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html