如何通过 NFC 发送 bytes[]
How to send bytes[] by NFC
如何通过 NFC P2P 发送原始字节。我在发件人方面有这个:
private static NdefRecord createByteRecord(final byte[] b){
final byte[] data = new byte[b.length + 1];
data[0] = (byte) 0x0;
System.arraycopy(b, 0, data, 1, b.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
}
我不知道 "NdefRecord.RTD_TEXT" 在这里是否是个不错的选择。在接收方,它看起来像这样:
private String readText(NdefRecord record) throws UnsupportedEncodingException
{
byte[] payload = record.getPayload();
//DO SOMETHING WITH BYTES, BUT GOT WRONG RESULTS WITH THIS BYTES
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = payload[0] & 0063;
return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
}
应该使用什么记录类型来传输原始数据字节?它们应该如何打包到 NDEF 记录中?
传送字节数组,最好用TNF_MIME_MEDIA。
例如,您可以创建自己的 MIME 类型:
"application/vnd.com.mycompany.myapp.beam"
例如创建NDEFRecord:
byte[] bytes = ...;
NdefRecord mimeRecord = NdefRecord.createMime("application/vnd.com.mycompany.myapp.beam", bytes);
下面来自 android 的文档:
https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#mime
如何通过 NFC P2P 发送原始字节。我在发件人方面有这个:
private static NdefRecord createByteRecord(final byte[] b){
final byte[] data = new byte[b.length + 1];
data[0] = (byte) 0x0;
System.arraycopy(b, 0, data, 1, b.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
}
我不知道 "NdefRecord.RTD_TEXT" 在这里是否是个不错的选择。在接收方,它看起来像这样:
private String readText(NdefRecord record) throws UnsupportedEncodingException
{
byte[] payload = record.getPayload();
//DO SOMETHING WITH BYTES, BUT GOT WRONG RESULTS WITH THIS BYTES
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = payload[0] & 0063;
return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
}
应该使用什么记录类型来传输原始数据字节?它们应该如何打包到 NDEF 记录中?
传送字节数组,最好用TNF_MIME_MEDIA。 例如,您可以创建自己的 MIME 类型:
"application/vnd.com.mycompany.myapp.beam"
例如创建NDEFRecord:
byte[] bytes = ...;
NdefRecord mimeRecord = NdefRecord.createMime("application/vnd.com.mycompany.myapp.beam", bytes);
下面来自 android 的文档:
https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#mime