Wiki

Clone wiki

B2G Notepad / Using emulator

Using emulator

# Run emulator
$ ./run-emulator.sh

# Unlock a scrren and enable NFC in the settings.
# Screen must be unlocked for a duration of this test.

# Connect via telnet. 5554 is a default port.
# If it's not working, maybe emulator used different port.
# Port number is shown in titlebar of emulator window.
$ telnet localhost 5554

# Enable remote endpoint
nfc nci rf_intf_activated_ntf 0
OK

# Setup LLCP link
# 4 4 are default values for emulator.
nfc llcp connect 4 4

# Emulate receiving NDEF. Use "-1 -1" to send NDEF.
#
# Syntax is as follows:
#   nfc snep put -1 -1 [flags,tnf,type,payload,id]
# or if you want to ommmit id (note the trailing comma, it's required):
#   nfc snep put -1 -1 [flags,tnf,type,payload,]
#
# flags: MB, ME, CF, SR, IL bits are supported. DO NOT include
#        TNF here.
#        MB - 0x80 - 128
#        ME - 0x40 - 64
#        CF - 0x20 - 32
#        SR - 0x10 - 16
#        IL - 0x08 - 8
#
# tnf:   TNF
#        TNF_EMPTY: 0x00,
#        TNF_WELL_KNOWN: 0x01,
#        TNF_MIME_MEDIA: 0x02,
#        TNF_ABSOLUTE_URI: 0x03,
#        TNF_EXTERNAL_TYPE: 0x04,
#        TNF_UNKNOWN: 0x05,
#        TNF_UNCHANGED: 0x06,
#        TNF_RESERVED: 0x07,
#
# type:  Base64 encoded type.
# payload: Base64 encoded payload.
# id:    Base64 encoded payload.
#

# Following command has values:
# flags:    MB, ME, SR
# tnf:      1
# type:     'U'
# payload:  [1,103,111,111,103,108,101,46,99,111,109]
#           1 - short for "http://www.". Rest is "google.com"
#
nfc snep put -1 -1 [208,1,VQ,AWdvb2dsZS5jb20,A]

Example records

# flags=MB,ME,SR; tnf=1; type='U'
# payload='http://www.google.com',
# id=A (b64)
nfc snep put -1 -1 [208,1,VQ,AWdvb2dsZS5jb20,A]

# flags=MB,ME,SR; tnf=1; type='U'
# payload='https://www.google.com',
# id=A (b64)
nfc snep put -1 -1 [208,1,VQ,BGdvb2dsZS5jb20,A]

# payload='http://mozilla.com' (with no abbrevation)
# (will work once https://bugzilla.mozilla.org/show_bug.cgi?id=1016093 lands)
nfc snep put -1 -1 [208,1,VQ,AGh0dHA6Ly9tb3ppbGxhLmNvbQ,A]

# flags=MB,ME,SR; tnf=1; type='U'
# payload='tel:0048793526455',
# id=A (b64)
nfc snep put -1 -1 [208,1,VQ,BTAwNDg3OTM1MjY0NTU,A]

# flags=MB,ME,SR; tnf=1; type='U'
# payload='mailto:kamituel@gmail.com',
# id=A (b64)
# (will work once https://bugzilla.mozilla.org/show_bug.cgi?id=1015143 lands)
nfc snep put -1 -1 [208,1,VQ,BmthbWl0dWVsQGdtYWlsLmNvbQ,A]

# flags=MB,ME; tnf=2; type='text/vcard'
# payload='BEGIN:VCARD\nVERSION:2.1\nFN:Kamil Leszczuk\nTEL:0048793526455\nEMAIL:kamituel@gmail.com\nEND:VCARD'
# id=A
nfc snep put -1 -1 [192,2,dGV4dC92Y2FyZA,QkVHSU46VkNBUkQKVkVSU0lPTjoyLjEKRk46S2FtaWwgTGVzemN6dWsKVEVMOjAwNDg3OTM1MjY0NTUKRU1BSUw6a2FtaXR1ZWxAZ21haWwuY29tCkVORDpWQ0FSRA,A]

# Simple smart poster with singe record (URI) pointing to http://www.kamituel.com (with abbreviation).
nfc snep put -1 -1 [208,1,U3A,0AENVQFrYW1pdHVlbC5jb20,]

# Smart poster from https://android.googlesource.com/platform/development/+/85122d2605a22c8e7bcde868c2b7a7d8d744e613/apps/Tag/src/com/android/apps/tag/MockNdefMessages.java
nfc snep put -1 -1 [208,1,U3A,kQEXVAJlbk5GQyBGb3J1bSBUeXBlIDQgVGFnUQEMVQFueHAuY29tL25mYw,]

# Ultimate smart poster, with action record (0), URI (youtube.com), text record for english ("Best page ever! q#@") and for polish ("ąćńó") and a small icon.
nfc snep put -1 -1 [208,1,U3A,kQEMVQF5b3V0dWJlLmNvbREBF1QCZW5CZXN0IHBhZ2UgZXZlciEgIHEjQBEBC1QCcGzEhcSHxYTDsxEDAWFjdABSCVRpbWFnZS9wbmeJUE5HDQoaCgAAAA1JSERSAAAABAAAAAQIAgAAACaTCSkAAAAbSURBVAjXY/j//z8DDDDCOYyMjEwQCiqFrAwAJgsJAcPRmnsAAAAASUVORK5CYII,]

Handover requests

# Simplified pairing record.
# Name of device: 'MBH10'
# MAC address: '4C:21:D0:9F:12:F1'
nfc snep put -1 -1 [208,2,YXBwbGljYXRpb24vdm5kLmJsdWV0b290aC5lcC5vb2I,GgDxEp/QIUwEDQQEIAUDHhELEQYJTUJIMTA,]

# Regular pairing record.
# Name of device: 'UE MINI BOOM'
# MAC address: '00:0D:44:E7:95:AB'
# 
# NOTE: This isn't working as of now. See https://bugzilla.mozilla.org/show_bug.cgi?id=1034660
#
nfc snep put -1 -1 [0,1,SHM=,EtECBGFjAQEwAA==,][0,2,YXBwbGljYXRpb24vdm5kLmJsdWV0b290aC5lcC5vb2I=,FgCrledEDQANCVVFIE1JTkkgQk9PTQ==,MA==]

How to get those values?

I'm using some simple shell functions (I've put them in $HOME/.profile, Node required):

# Encode "U" as type:
$ fromUTF8 U
[85]
$ base64-bytes [85]
VQ==

# Encode "http://www.google.com" as payload:
$ fromUTF8 google.com
[103,111,111,103,108,101,46,99,111,109]
# Note I've added 1 in front, for "http://wwww."
$ base64-bytes [1,103,111,111,103,108,101,46,99,111,109]
AWdvb2dsZS5jb20=
# Usage: fromUTF8 kamil
function fromUTF8 {
  cmd=$(cat <<EOF
  var fromUTF8 = function (str) {
    if (!str) {
      return null;
    }

    var buf = new Uint8Array(str.length);
    for (var i = 0; i < str.length; i++) {
      buf[i] = str.charCodeAt(i);
    }

    return buf;
  };

  console.log(JSON.stringify(Array.apply([], fromUTF8("a$1")).splice(1)));

EOF
)

  echo $cmd | node
}

# Usage: base64-bytes [1,2,3]
function base64-bytes {
  cmd=$(cat <<EOF
    console.log(new Buffer($1).toString('base64'));
EOF
)
  echo $cmd | node
}

Where is that stuff?

Emulators telnet CLI is implemented in console.h. Their current list and syntax can be found there in nfc_commands[] array.

Updated