如何通过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记录?
答案 0 :(得分:0)
要传输字节数组,最好使用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下面的doc:
https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#mime