【发布时间】:2016-07-30 04:01:06
【问题描述】:
我制作了一个可以读取 NFC 标签的应用,但我面临的问题是并非所有频段都有 ID。
是否可以为空标签分配新的 ID?
private String tagInfoId = "";
private NfcAdapter nfcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_player);
if (!userPrefs.isLoggedIn().get()) finish();
trackerService = ServiceGateway.createAuthorizedService(TrackerService.class);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this,
"NFC NOT supported on this devices!",
Toast.LENGTH_LONG).show();
finish();
} else if (!nfcAdapter.isEnabled()) {
Toast.makeText(this,
"NFC NOT Enabled!",
Toast.LENGTH_LONG).show();
finish();
}
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag == null) {
Log.d(TAG, "tag == null");
//here is possible to write a new tag code ???
} else {
byte[] tagId = tag.getId();
for (int i = 0; i < tagId.length; i++) {
tagInfoId += Integer.toHexString(tagId[i] & 0xFF);
}
Log.d(TAG, "onResume() called with: " + "tagID:" + tagInfoId);
}
}
}
Manifest.xml
<activity
android:name=".activities.NfcActivity"
android:noHistory="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
另外,如果我为一个新标签分配一个 ID,它会以相同的方式读取它还是我必须更改上面的代码?
【问题讨论】: