[英]Host-based Card Emulation or HCE
我想編寫一個可以存儲唯一ID的程序,然后將其共享為NFC標簽。 我完全想編寫可以使用移動設備而不是智能卡的程序。
我在許多站點和這里都讀過它,但是我不明白如何將ID推送到我的應用程序以及如何通過NFC閱讀器設備共享此ID
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);
}
}
}
我建議閱讀更多有關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! 上面的代碼是讀取器/寫入器模式的示例,您可以在其中讀取(或寫入)標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.