简体   繁体   中英

How to read and write to a NfcA card from Android

I've made an app that is called when the intent android.nfc.action.TAG_DISCOVERED is sent, but then I want to get the info of the card in the onNewIntent method, but I don't know how to handle this kind of nfc cards. I tried with the following code:

    public void onNewIntent(Intent intent) {
        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        //do something with tagFromIntent
        NfcA nfca = NfcA.get(tagFromIntent);
        try{
            nfca.connect();
            Short s = nfca.getSak();
            byte[] a = nfca.getAtqa();
            String atqa = new String(a, Charset.forName("US-ASCII"));
            tv.setText("SAK = "+s+"\nATQA = "+atqa);
            nfca.close();
        }
        catch(Exception e){
            Log.e(TAG, "Error when reading tag");
            tv.setText("Error");
        }
    }

tv is a TextView, but when this code is executed it never gets changed.

OnNewIntent is called if your activity is already running and is set to be singleTask. You'll want to make the code it's own method and call it in onCreate() as well as onNewIntent()

If you have other apps that can manage the same tag at higher level (NDEF_DISCOVERED or TECH_DISCOVERED) your app, that manage only the lower level, never will be called.

To use your app you need to open your app, than scan the tag.

If your app never starts, be sure to have done these steps:

In OnCreate() get your NfcAdapter:

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   ....
   mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

   if (mNfcAdapter == null) {
      // Stop here, we definitely need NFC
      Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
      finish();
      return;
    }

    //method to handle your intent
    handleTag(getIntent());
}

In onResume enable the foreground dispatch:

@Override
public void onResume() {
   super.onResume();

   final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
   final PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);

    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}

In onPause disable it:

@Override
protected void onPause() {
   mNfcAdapter.disableForegroundDispatch(this);

   super.onPause();
}

In onNewIntent call the function to handle your intent

@Override
protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);

   handleTag(intent);
}

In your function handle your tag:

private void handleTag(Intent intent){
   String action = intent.getAction();
   Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

   if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action){

      // here your code
   }
}

Don't forget to add the permissions in your manifest:

<uses-permission android:name="android.permission.NFC" />

<uses-feature  
   android:name="android.hardware.nfc"
   android:required="true" />

<activity>
   ....
   <intent-filter>
      <action android:name="android.nfc.action.TAG_DISCOVERED"/>
   </intent-filter>
</activity>

More info here NFC Basis and here Read NFC tags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM