简体   繁体   中英

NFC P2P tag intercept

I am building an Android app using Eclipse and Android SDK. I would like to implement an NFC P2P function in my app in the sense that when you place the 2 phones back to back, both send a string and receive a string automatically. This would of course happen in a separate activity. I have managed to send a custom tag (String) but have been unable to intercept it and use it afterwards in the app code. How can I do this?

This is what I have so far:

public class MainActivity extends Activity {

public NfcAdapter mAdapter;
PendingIntent mPendingIntent;

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


     mAdapter = NfcAdapter.getDefaultAdapter(this);

     NdefRecord rec = NdefRecord.createUri("www.stackoverflow.com"); 
     NdefMessage ndef = new NdefMessage(rec);

     mAdapter.setNdefPushMessage(ndef, this);
 }

I have spent a lot of time trying to find and understand solutions to intercept the tag. Unsuccessfully.

Thank you for your help.

You can use the foreground dispatch system to receive the NDEF message within your activity:

  1. In onResume() :

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); 
  2. Do something upon receiving the intent:

     public void onNewIntent(Intent intent) { ... } 

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