gpt4 book ai didi

java - 使用 Java 和 Android 从 NFC 读取特定值

转载 作者:行者123 更新时间:2023-12-02 08:57:38 26 4
gpt4 key购买 nike

在 NFC 卡中,我编写了一条文本作为 NDEF 消息,我想使用应用程序从卡中读取它。

我尝试过,但我无法理解如何读取该文本 NDEF 消息:

    @Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// GET MY TEXT

Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
Log.e("MSG", messages[i].toString());
}
}
}
}

在 list 中,我将这些行添加到我的 Activity 中:

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>

最佳答案

尝试这个方法:

private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] messages = null;
if (rawMessages != null) {
messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
NdefRecord [] records = messages[i].getRecords();
//if you are sure you have text then you don't need to test TNF
for(NdefRecord record: records){
processRecord(record);
}
}
}
}
}
public void processRecord(NdefRecord record) {

short tnf = record.getTnf();
switch (tnf) {

case NdefRecord.TNF_WELL_KNOWN: {
if (Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {
String yourtext = processRtdTextRecord(record.getPayload());
} else if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
String yourtext = record.toUri().toString();
} else if (Arrays.equals(record.getType(), NdefRecord.RTD_SMART_POSTER)) {
processSmartPosterRecord(record);
} else {
//Record is not Text or URI or Poster
}
}
case NdefRecord.TNF_MIME_MEDIA: {
if (record.toMimeType().equals("MIME/Type")) {
// handle this as you want
} else {
//Record is not our MIME
}
}
// you can write more cases
default: {
//unsupported NDEF Record
}
}
}

private String processRtdTextRecord(byte[] payload) {
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = payload[0] & 0063;

String text = "";
try {
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("UnsupportedEncoding", e.toString());
}
return text;
}

关于java - 使用 Java 和 Android 从 NFC 读取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60391435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com