我使用chariot solutions phonegap plugin编写NFC标签,但我不确定如何使用phonegap插件编写Android Application Record。
以下是我编写NDEF记录的代码:
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 ) );
} );
如何使用phonegap编写Android应用程序记录(AAR)?
答案 0 :(得分:1)
答案是根据spec found here
写一个TNF_EXTERNAL_TYPE记录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 ) );
} );