Search code examples
cordovaphonegap-pluginsnfcndefandroid-applicationrecord

How do I write an Android Application Record (AAR) using phonegap?


I'm using chariot solutions phonegap plugin to write NFC tags but I'm not sure how to write an Android Application Record using the phonegap plugin.

Here is my code on how I write an NDEF record:

nfc.addNdefListener( function(nfcEvent) {

    var tag = nfcEvent.tag, ndefMessage = tag.ndefMessage;

    if (tag.isWritable && tag.canMakeReadOnly) {
        log( JSON.stringify( tag ) );

        var type = "application/com.example.name",
        id = 1234,
        payload = nfc.stringToBytes( JSON.stringify( { payloadID : 1234 } ) ),
        record = ndef.record( ndef.TNF_MIME_MEDIA, type, id, payload );

        var message = [ record ];

        nfc.write( message, function() {
            alert( "Successfully written to NFC Tag!" )
        }, function() {
            alert( "Failed to write to NFC Tag!" )
        } );
    }

}, function() { // success callback
    log( "Waiting for NDEF tag" );
}, function(error) { // error callback
    alert( "Error adding NDEF listener " + JSON.stringify( error ) );
} );

How do I write an Android Application Record (AAR) using phonegap ?


Solution

  • The answer is to write an TNF_EXTERNAL_TYPE record based on the spec found here

    nfc.addNdefListener( function(nfcEvent) {
    
        var tag = nfcEvent.tag, ndefMessage = tag.ndefMessage;
    
        if (tag.isWritable && tag.canMakeReadOnly) {
            log( JSON.stringify( tag ) );
    
            var type = "application/com.example.name",
            id = 1234,
            payload = nfc.stringToBytes( JSON.stringify( { payloadID : 1234 } ) ),
            mime = ndef.record( ndef.TNF_MIME_MEDIA, type, id, payload );
    
            var type = "android.com:pkg",
            id = "",
            payload = nfc.stringToBytes( "com.example.name" ),
            aar = ndef.record( ndef.TNF_EXTERNAL_TYPE, type, id, payload);
    
            var message = [ mime, aar ];
    
            nfc.write( message, function() {
                alert( "Successfully written to NFC Tag!" )
            }, function() {
                alert( "Failed to write to NFC Tag!" )
            } );
        }
    
    }, function() { // success callback
        log( "Waiting for NDEF tag" );
    }, function(error) { // error callback
        alert( "Error adding NDEF listener " + JSON.stringify( error ) );
    } );