我想写一个程序,可以存储一个唯一的ID,然后分享它作为NFC标签。总而言之,我想写程序,可以使用移动设备,而不是智能卡。
我在许多网站和这里看到过,但我不明白如何将ID推送到我的应用程序,以及如何与NFC阅读器设备共享此ID
我不知道code below是做什么的,它们的功能是什么
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
filters = new IntentFilter[] { new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED) };
techLists = new String[][] { { "android.nfc.tech.IsoPcdA" } };
}
public void onResume() {
super.onResume();
if (adapter != null) {
adapter.enableForegroundDispatch(this, pendingIntent, filters,
techLists);
}
}
public void onPause() {
super.onPause();
if (adapter != null) {
adapter.disableForegroundDispatch(this);
}
}
}发布于 2015-06-29 19:35:26
我建议你多读一些关于NFC和Android的ForegroundDispatcher的知识。作为开始,我将在baseline中描述这段代码正在做什么。
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
//Here you define an intent that will be raised when a tag is received.
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//These are the tag conditions to throw the intent of above.
//ACTION_TECH_DISCOVERED means the tag need to be of the type defined in the techlist.
filters = new IntentFilter[] { new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED) };
//As the name already says, this is your techlist
techLists = new String[][] { { "android.nfc.tech.IsoPcdA" } };
}
public void onResume() {
super.onResume();
if (adapter != null) {
//This enabled the foreground dispatching
//It means that when a tag is detected of the type `IsoPcdA`. The `pendingIntent` will be given to the current activity
adapter.enableForegroundDispatch(this, pendingIntent, filters,
techLists);
}
}
public void onPause() {
super.onPause();
if (adapter != null) {
//This is to disable the foreground dispatching. You don't want to send this intents to this activity when it isn't active
adapter.disableForegroundDispatch(this);
}
}
}因为这个ForegroundDispatching抛出了一个新的intent,所以您需要覆盖onNewIntent方法。在这里您可以读取(和写入)标记。
@Override
public void onNewIntent(Intent intent)
{
// Get the tag from the given intent
Tag t = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
} 祝好运!
顺便说一句:这不是HCE!上面的代码是一个读取器/写入器模式的示例,您可以在该模式下读取(或写入)标签。
https://stackoverflow.com/questions/31097799
复制相似问题