使用Java和Android从NFC读取特定值
在一张NFC卡上,我写了一条文本作为NDEF消息,我想用一个应用程序从卡上读取它 我试过了,但我不明白如何阅读NDEF短信:使用Java和Android从NFC读取特定值,java,android,nfc,rfid,Java,Android,Nfc,Rfid,在一张NFC卡上,我写了一条文本作为NDEF消息,我想用一个应用程序从卡上读取它 我试过了,但我不明白如何阅读NDEF短信: @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Tag tag = intent.getPa
@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());
}
}
}
}
在清单中,我将以下行添加到我的活动中:
尝试以下方法:
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;
}
好的,你们能描述一下当你们把你们的安卓设备放到卡上时发生了什么吗?你们把文字放在卡片上是什么意思?你用应用程序把它放进去了吗?还是你自己写代码?您是否将此文本作为NDEF消息编写?
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;
}