Android GetCachedDefMessage返回null
我正在尝试从NFC卡读取数据。我使用以下函数来获取和读取Android GetCachedDefMessage返回null,android,nfc,ndef,Android,Nfc,Ndef,我正在尝试从NFC卡读取数据。我使用以下函数来获取和读取NDEF消息,但得到的消息为空 使用getMaxSize()和getType()返回org.nfcforum.ndef.type2可以获得正确的数据长度,但getCachedDefMessage()函数返回空值。更重要的是,我发现了android.nfc.action.TAG\u,作为action\NDEF\u DISCOVERED之外的一个意图动作 @Override protected void onResume() { sup
NDEF
消息,但得到的消息为空
使用getMaxSize()
和getType()
返回org.nfcforum.ndef.type2
可以获得正确的数据长度,但getCachedDefMessage()
函数返回空值。更重要的是,我发现了android.nfc.action.TAG\u,作为action\NDEF\u DISCOVERED
之外的一个意图动作
@Override
protected void onResume() {
super.onResume();
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected,tagDetected,ndefDetected};
String [][] techListsArray = new String[][]{new String[]{
Ndef.class.getName()
}};
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
if(mNfcAdapter!= null) {
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
}
else {
Log.d(TAG, "NFC not supported.");
}
}
@Override
protected void onPause() {
super.onPause();
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "onNewIntent: "+intent.getAction());
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
printTagId(intent);
if(tag != null) {
Log.d(TAG, "TAG detected.");
String [] tagList = tag.getTechList();
if(tagList != null)
{
for (String str : tagList) {
Log.d(TAG, "Tech " + str);
}
}
Ndef ndef = Ndef.get(tag);
readNDef(ndef);
}
else {
Log.d(TAG, "TAG is null.");
}
}
private void readNDef(Ndef ndef) {
try {
ndef.connect();
Log.d(TAG, "ndef type: " + ndef.getType());
Log.d(TAG, "ndef maxSize: " + ndef.getMaxSize());
Log.d(TAG, "ndef isWritable: " + ndef.isWritable());
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
if(ndefMessage != null && ndefMessage.getRecords().length > 0) {
String message = new String(ndefMessage.getRecords()[0].getPayload());
Log.d(TAG, "readNDef: " + message);
}
else{
Log.d(TAG, "readNDef: NO Records.");
}
ndef.close();
} catch (Exception e) {
Log.d(TAG, "readNDef exception!");
e.printStackTrace();
}
try{ if(ndef != null) ndef.close(); } catch (Exception ex){}
}
输出如下所示:
onNewIntent: android.nfc.action.TAG_DISCOVERED
Tech android.nfc.tech.MifareUltralight
Tech android.nfc.tech.NfcA
Tech android.nfc.tech.Ndef
ndef type: org.nfcforum.ndef.type2
ndef maxSize: 1908
ndef isWritable: true
readNDef: NO Records.
当我通过另一个免费应用程序读取NDEF数据时,我确信该卡具有NDEF类型的数据。我不明白为什么我没有找到动作
而不是标记
。如何使用NDEF格式获取数据?如果标记处于NFC Forum定义的初始化状态,则此方法可能返回null,因为在此状态下,标记的格式为支持NDEF,但尚未包含消息。见文件
如果是,在初始化状态下我应该怎么做?我应该在事件触发后等待一段时间吗?你认为呢?你必须等到tag收到消息。在调用GetCachedDefMessage之前,您可以对此进行检查。我在尝试onNewIntent回调后进行了一些延迟,但仍然是一样的。希望此链接将对您有所帮助