ndef-lib/index.js
function (ndefRecords) {

        var encoded = [],
            tnf_byte,
            record_type,
            payload_length,
            id_length,
            i,
            mb, me, // messageBegin, messageEnd
            cf = false, // chunkFlag TODO implement
            sr, // boolean shortRecord
            il; // boolean idLengthFieldIsPresent

        for(i = 0; i < ndefRecords.length; i++) {

            mb = (i === 0);
            me = (i === (ndefRecords.length - 1));
            sr = (ndefRecords[i].payload.length < 0xFF);
            il = (ndefRecords[i].id.length > 0);
            tnf_byte = ndef.encodeTnf(mb, me, cf, sr, il, ndefRecords[i].tnf);
            encoded.push(tnf_byte);

            // type is stored as String, converting to bytes for storage
            record_type = util.stringToBytes(ndefRecords[i].type);
            encoded.push(record_type.length);

            if (sr) {
                payload_length = ndefRecords[i].payload.length;
                encoded.push(payload_length);
            } else {
                payload_length = ndefRecords[i].payload.length;
                // 4 bytes
                encoded.push((payload_length >> 24));
                encoded.push((payload_length >> 16));
                encoded.push((payload_length >> 8));
                encoded.push((payload_length & 0xFF));
            }

            if (il) {
                id_length = ndefRecords[i].id.length;
                encoded.push(id_length);
            }

            encoded = encoded.concat(record_type);

            if (il) {
                encoded = encoded.concat(ndefRecords[i].id);
            }

            encoded = encoded.concat(ndefRecords[i].payload);
        }

        return encoded;
    }
function (ndefRecords) {

        var encoded = [],
            tnf_byte,
            record_type,
            payload_length,
            id_length,
            i,
            mb, me, // messageBegin, messageEnd
            cf = false, // chunkFlag TODO implement
            sr, // boolean shortRecord
            il; // boolean idLengthFieldIsPresent

        for(i = 0; i < ndefRecords.length; i++) {

            mb = (i === 0);
            me = (i === (ndefRecords.length - 1));
            sr = (ndefRecords[i].payload.length < 0xFF);
            il = (ndefRecords[i].id.length > 0);
            tnf_byte = ndef.encodeTnf(mb, me, cf, sr, il, ndefRecords[i].tnf);
            encoded.push(tnf_byte);

            // type is stored as String, converting to bytes for storage
            record_type = util.stringToBytes(ndefRecords[i].type);
            encoded.push(record_type.length);

            if (sr) {
                payload_length = ndefRecords[i].payload.length;
                encoded.push(payload_length);
            } else {
                payload_length = ndefRecords[i].payload.length;
                // 4 bytes
                encoded.push((payload_length >> 24));
                encoded.push((payload_length >> 16));
                encoded.push((payload_length >> 8));
                encoded.push((payload_length & 0xFF));
            }

            if (il) {
                id_length = ndefRecords[i].id.length;
                encoded.push(id_length);
            }

            encoded = encoded.concat(record_type);

            if (il) {
                encoded = encoded.concat(ndefRecords[i].id);
            }

            encoded = encoded.concat(ndefRecords[i].payload);
        }

        return encoded;
    }
function ( ndefRecords ) { var encoded = [ ] , tnf_byte , record_type , payload_length , id_length , i , mb , me , // messageBegin, messageEnd cf = false , // chunkFlag TODO implement sr , // boolean shortRecord il ; // boolean idLengthFieldIsPresent for ( i = 0 ; i < ndefRecords . length ; i ++ ) { mb = ( i === 0 ) ; me = ( i === ( ndefRecords . length - 1 ) ) ; sr = ( ndefRecords [ i ] . payload . length < 0xFF ) ; il = ( ndefRecords [ i ] . id . length > 0 ) ; tnf_byte = ndef . encodeTnf ( mb , me , cf , sr , il , ndefRecords [ i ] . tnf ) ; encoded . push ( tnf_byte ) ; // type is stored as String, converting to bytes for storage record_type = util . stringToBytes ( ndefRecords [ i ] . type ) ; encoded . push ( record_type . length ) ; if ( sr ) { payload_length = ndefRecords [ i ] . payload . length ; encoded . push ( payload_length ) ; } else { payload_length = ndefRecords [ i ] . payload . length ; // 4 bytes encoded . push ( ( payload_length >> 24 ) ) ; encoded . push ( ( payload_length >> 16 ) ) ; encoded . push ( ( payload_length >> 8 ) ) ; encoded . push ( ( payload_length & 0xFF ) ) ; } if ( il ) { id_length = ndefRecords [ i ] . id . length ; encoded . push ( id_length ) ; } encoded = encoded . concat ( record_type ) ; if ( il ) { encoded = encoded . concat ( ndefRecords [ i ] . id ) ; } encoded = encoded . concat ( ndefRecords [ i ] . payload ) ; } return encoded ; }
Encodes an NDEF Message into bytes that can be written to a NFC tag.

@ndefRecords an Array of NDEF Records

@returns byte array

@see NFC Data Exchange Format (NDEF) http://www.nfc-forum.org/specs/spec_list/
Encodes an NDEF Message into bytes that can be written to a NFC tag .