NFC标签的P2P拦截 [英] NFC P2P tag intercept
问题描述
我建立使用Eclipse和Android SDK的Android应用程序。我想实现一个NFC P2P功能,我在这个意义上,当您将2个手机背靠背的应用程序,无论是发送一个字符串,并自动接收一个字符串。这当然会发生在一个单独的活动。我已成功地发送自定义标签(字符串),但一直未能拦截,并在应用code以后使用它。我怎样才能做到这一点?
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?
这是我迄今为止:
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.
感谢您的帮助。
推荐答案
您可以使用前景调度系统的活动中收到的NDEF消息:
You can use the foreground dispatch system to receive the NDEF message within your activity:
-
在
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);
做一些在接到意图:
Do something upon receiving the intent:
public void onNewIntent(Intent intent) {
...
}
这篇关于NFC标签的P2P拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!