0

我使用chariot solutions phonegap plugin編寫NFC標籤,但我不確定如何使用phonegap插件編寫Android Application Record如何使用phonegap編寫Android應用程序記錄(AAR)?

這裏是我的,我怎麼寫的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)); 
}); 

如何編寫一個Android應用程序記錄(AAR)使用PhoneGap的?

+0

剛剛發現的TNF_EXTERNAL_TYPE記錄這個https://plus.google.com/+JanBrands/posts/8n3x9JFnG8T – deefactorial 2014-08-29 17:34:49

回答

1

答案是編寫一個基於該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)); 
}); 
+0

記錄ID應該是一個字節數組或只是「[]」,如果你不使用和ID。 – doncoleman 2014-09-04 20:23:00