在特定长度后无法写入ndef记录

时间:2016-11-28 09:55:48

标签: android nfc mifare

使用ndef记录写入时遇到问题,因为如果超过特定长度则不会写入。 //我的数据格式为:

// String NFCPayload = sb.append("TEST").append(":").append("test@test.com").append(":").append("1").append(":").append("2").append(":").append("5").toString();

它有效,但现在如果连一个字母都添加它就不会。

private void write(String text, Tag tag) throws IOException, FormatException 
{
    NdefRecord[] records = { createRecord(text) };
    NdefMessage  message = new NdefMessage(records);
    // Get an instance of Ndef for the tag.
    Ndef ndef = Ndef.get(tag);
    // Enable I/O
    ndef.connect();
    // Write the message
    ndef.writeNdefMessage(message);
    // Close the connection
    ndef.close();
}


private NdefRecord createRecord(String text) throws UnsupportedEncodingException 
{
    String lang       = "en";
    byte[] textBytes  = text.getBytes();
    byte[] langBytes  = lang.getBytes("US-ASCII");
    int    langLength = langBytes.length;
    int    textLength = textBytes.length;
    byte[] payload    = new byte[1 + langLength + textLength];

    // set status byte (see NDEF spec for actual bits)
    payload[0] = (byte) langLength;

    // copy langbytes and textbytes into payload
    System.arraycopy(langBytes, 0, payload, 1,              langLength);
    System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);

    NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,  NdefRecord.RTD_TEXT,  new byte[0], payload);

    return recordNFC;
}

0 个答案:

没有答案