1

如何通过 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 记录中?

4

1 回答 1

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的文档:

https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#mime

于 2015-03-24T13:34:25.783 回答