//here we make some sample transmissions that we convert to work out the max length of data we can send int test_payload_len = 300; //need to make this >255 to ensure the longer length field format is used NdefMessage testDeviceNdef = new NdefMessage(NdefRecord.createExternal(CommandConsts.deviceDomain, CommandConsts.deviceType, test_data)); //the domain and type strings are defined elsewhere NdefMessage testTerminalNdef = new NdefMessage(NdefRecord.createExternal(CommandConsts.terminalDomain, CommandConsts.terminalType, test_data)); maxNdefDeviceSendLength = ndefTag.getMaxSize() - (testDeviceNdef.getByteArrayLength() - test_payload_len); maxNdefTerminalSendLength = ndefTag.getMaxSize() - (testTerminalNdef.getByteArrayLength() - test_payload_len)
/** * Return this NDEF Message as raw bytes.<p> * The NDEF Message is formatted as per the NDEF 1.0 specification, * and the byte array is suitable for network transmission or storage * in an NFC Forum NDEF compatible tag.<p> * This method will not chunk any records, and will always use the * short record (SR) format and omit the identifier field when possible. * * @return NDEF Message in binary format * @see getByteArrayLength */ public byte[] toByteArray() { int length = getByteArrayLength(); ByteBuffer buffer = ByteBuffer.allocate(length); for (int i=0; i<mRecords.length; i++) { boolean mb = (i == 0); // first record boolean me = (i == mRecords.length - 1); // last record mRecords[i].writeToByteBuffer(buffer, mb, me); } return buffer.array(); }