如何读取NFC标签的NDEF Msg和Records?
How to read NDEF Msg and Records of NFC tag?
我正在阅读 NDEF 消息和记录
这里我已经通过使用这个
完成了Reading ID
public class MainActivity extends Activity {
// list of NFC technologies detected:
private final String[][] techList = new String[][] {
new String[] {
NfcA.class.getName(),
NfcB.class.getName(),
NfcF.class.getName(),
NfcV.class.getName(),
IsoDep.class.getName(),
MifareClassic.class.getName(),
MifareUltralight.class.getName(), Ndef.class.getName()
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
// creating pending intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// creating intent receiver for NFC events:
IntentFilter filter = new IntentFilter();
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
// enabling foreground dispatch for getting intent from NFC event:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
}
@Override
protected void onPause() {
super.onPause();
// disabling foreground dispatch:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
((TextView)findViewById(R.id.text)).setText(
"NFC Tag\n" +
ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
}
private String ByteArrayToHexString(byte [] inarray) {
int i, j, in;
String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String out= "";
for(j = 0 ; j < inarray.length ; ++j)
{
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}
此处读取标签ID为Record[0]
以类似的方式,我想以相同的程序和相同的方式读取 NDEF 消息和记录...我们有很多方法
这里我试过
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
//read each record
}
但是我无法读取记录.. 任何人都可以建议我在我的代码中读取 NDEF 消息和记录....
更新
我已经在清单中添加了这个..
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
*<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="text/plain" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>*
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data
android:name="android.nfc.action.TAG_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
但我仍然需要使用 NfcAdapter.EXTRA_NDEF_MESSAGES
谁能告诉我如何在不影响或更改 NfcAdapter.EXTRA_ID
.
的情况下在我的代码中使用它
- 添加日志以了解发生了什么。
- 从
Intent
中的标签获取 NDEFMessage
- 您将 ByteArray 转换为 Hex 字符串的函数不是最好的
并且请阅读官方文档,所有你需要的解释:http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#obtain-info
在您的 Activity 方法 onNewIntent 中:
@Override
protected void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntent action=" + intent.getAction());
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
((TextView)findViewById(R.id.text)).setText(
"NFC Tag\n" +
byteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
for (int i = 0; i < rawMsgs.length; i++) {
NdefMessage ndefMessage = (NdefMessage) rawMsgs[i];
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
//read each record
}
}
}
}
}
/**
* convert byte array to a hexadecimal string
*
* @param bArray
* byte array to convert
* @return hexadecimal string
*/
public static String byteArrayToHexString(byte[] bArray) {
StringBuffer buffer = new StringBuffer();
for (byte b : bArray) {
buffer.append(byteToHexString(b));
buffer.append(" ");
}
return buffer.toString().toUpperCase(Locale.getDefault());
}
/**
* convert byte to a hexadecimal string
*
* @param b
* byte to convert
* @return hexadecimal string
*/
public static String byteToHexString(byte b) {
int tmp = b & 0xFF;
if (tmp <= 15) {
return "0".concat(Integer.toHexString(tmp));
} else {
return Integer.toHexString(tmp);
}
}
我正在阅读 NDEF 消息和记录
这里我已经通过使用这个
完成了Reading IDpublic class MainActivity extends Activity {
// list of NFC technologies detected:
private final String[][] techList = new String[][] {
new String[] {
NfcA.class.getName(),
NfcB.class.getName(),
NfcF.class.getName(),
NfcV.class.getName(),
IsoDep.class.getName(),
MifareClassic.class.getName(),
MifareUltralight.class.getName(), Ndef.class.getName()
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
// creating pending intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// creating intent receiver for NFC events:
IntentFilter filter = new IntentFilter();
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
// enabling foreground dispatch for getting intent from NFC event:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
}
@Override
protected void onPause() {
super.onPause();
// disabling foreground dispatch:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
((TextView)findViewById(R.id.text)).setText(
"NFC Tag\n" +
ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
}
private String ByteArrayToHexString(byte [] inarray) {
int i, j, in;
String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String out= "";
for(j = 0 ; j < inarray.length ; ++j)
{
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}
此处读取标签ID为Record[0]
以类似的方式,我想以相同的程序和相同的方式读取 NDEF 消息和记录...我们有很多方法
这里我试过
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
//read each record
}
但是我无法读取记录.. 任何人都可以建议我在我的代码中读取 NDEF 消息和记录....
更新
我已经在清单中添加了这个..
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
*<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="text/plain" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>*
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data
android:name="android.nfc.action.TAG_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
但我仍然需要使用 NfcAdapter.EXTRA_NDEF_MESSAGES
谁能告诉我如何在不影响或更改 NfcAdapter.EXTRA_ID
.
- 添加日志以了解发生了什么。
- 从
Intent
中的标签获取 - 您将 ByteArray 转换为 Hex 字符串的函数不是最好的
NDEFMessage
并且请阅读官方文档,所有你需要的解释:http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#obtain-info
在您的 Activity 方法 onNewIntent 中:
@Override
protected void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntent action=" + intent.getAction());
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
((TextView)findViewById(R.id.text)).setText(
"NFC Tag\n" +
byteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
for (int i = 0; i < rawMsgs.length; i++) {
NdefMessage ndefMessage = (NdefMessage) rawMsgs[i];
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
//read each record
}
}
}
}
}
/**
* convert byte array to a hexadecimal string
*
* @param bArray
* byte array to convert
* @return hexadecimal string
*/
public static String byteArrayToHexString(byte[] bArray) {
StringBuffer buffer = new StringBuffer();
for (byte b : bArray) {
buffer.append(byteToHexString(b));
buffer.append(" ");
}
return buffer.toString().toUpperCase(Locale.getDefault());
}
/**
* convert byte to a hexadecimal string
*
* @param b
* byte to convert
* @return hexadecimal string
*/
public static String byteToHexString(byte b) {
int tmp = b & 0xFF;
if (tmp <= 15) {
return "0".concat(Integer.toHexString(tmp));
} else {
return Integer.toHexString(tmp);
}
}