Vur*_*tne 2 tags android nfc android-intent ndef
我有这个代码,我总是得到Ndef ndef = Ndef.get(tag)== null。我试图以不同的方式获得它,但我总是null在同一个地方到达那里。有人知道我做错了什么吗?
private ImageView imgSearch;
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
}
@Override
protected void onResume() {
super.onResume();
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
Toast.makeText(this, R.string.no_nfc_supported, Toast.LENGTH_SHORT).show();
} else {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
protected void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
getTagInfo(intent);
}
private void getTagInfo(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
NdefMessage ndefMesg = ndef.getCachedNdefMessage();
if (ndefMesg != null) {
Toast.makeText(this, ndefMesg.toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在清单中,我有:
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
Run Code Online (Sandbox Code Playgroud)
和:
<activity android:name=".activities.NFCActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
您正在使用前台调度系统来获取有关新标签的通知
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
Run Code Online (Sandbox Code Playgroud)
当您null作为意图过滤器和技术列表参数(最后两个参数)传递时,您的活动将在前台时收到有关任何标签的通知。因此,在方法getTagInfo()(或实际上onNewIntent())中,您会收到一个ACTION_TAG_DISCOVERED意图。
private void getTagInfo(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
// this part is executed, hence, you received an ACTION_TAG_DISCOVERED intent
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着getTagInfo()无论标签支持的标签技术如何,都将执行中的代码。因此,如果标签不支持Ndef标签技术,Ndef.get(tag)将返回null.
您可以使用以下方法获取标签支持的技术列表:
String[] techList = tag.getTechList();
Run Code Online (Sandbox Code Playgroud)
如果标签不支持Ndef标签技术,这可能意味着标签尚未配置为携带 NDEF 负载,必须首先使用该NdefFormatable技术进行初始化(“格式化”)。
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
NdefMessage ndefMesg = ndef.getCachedNdefMessage();
if (ndefMesg != null) {
...
}
} else {
NdefFormatable ndefFormatable = NdefFormatable.get(tag);
if (ndefFormatable != null) {
// initialize tag with new NDEF message
try {
ndefFormatable.connect();
ndefFormatable.format(newNdefMessage);
} finally {
try {
ndefFormatable.close();
} catch (Exception e) {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果标签既不支持Ndef也不支持NdefFormatable,则意味着该标签不是 NFC 论坛标签,并且根本不支持携带 NDEF 负载。
| 归档时间: |
|
| 查看次数: |
3030 次 |
| 最近记录: |