我想编写一个程序,可以存储唯一的ID,然后将其作为NFC标记共享。我完全希望编写程序可以使用移动而不是智能卡。
我在很多网站和这里都看到了它,但我不明白我怎么能将ID推送到我的应用程序以及如何分享这个ID NFC NFC读卡器设备
我不知道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);
}
}
}
答案 0 :(得分:1)
我建议您阅读更多关于NFC和Android ForegroundDispatcher
的内容。
为了开始,我将在基线中描述此代码正在做什么。
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!上面的代码是读取器/写入器模式的示例,您可以在其中读取(或写入)标签。