phonegap-nfc
Advanced tools
Comparing version 0.7.3 to 1.0.0
@@ -0,1 +1,5 @@ | ||
= 1.0.0 = | ||
Add new commands nfc.connect, nfc.tranceive, nfc.close to support sending raw commands to NFC tags on Android. | ||
Support tag types: NfcA (ISO 14443-3A), NfcB (ISO 14443-3B), NfcF (JIS 6319-4), NfcV (ISO 15693), and IsoDep (ISO 14443-4) #320 | ||
= 0.7.3 = | ||
@@ -2,0 +6,0 @@ Bump version for npm issues |
{ | ||
"name": "phonegap-nfc", | ||
"version": "0.7.3", | ||
"version": "1.0.0", | ||
"description": "Near Field Communication (NFC) Plugin. Read and write NDEF messages to NFC tags and share NDEF messages with peers.", | ||
@@ -5,0 +5,0 @@ "cordova": { |
162
README.md
@@ -11,2 +11,3 @@ PhoneGap NFC Plugin | ||
* receive data from NFC devices | ||
* send raw commands (ISO 14443-3A, ISO 14443-3A, ISO 14443-4, JIS 6319-4, ISO 15693) to NFC tags | ||
@@ -99,2 +100,9 @@ This plugin uses NDEF (NFC Data Exchange Format) for maximum compatibilty between NFC devices, tag types, and operating systems. | ||
## Tag Technology Functions | ||
- [nfc.connect](#nfcconnect) | ||
- [nfc.transceive](#nfctransceive) | ||
- [nfc.close](#nfcclose) | ||
- [ISO-DEP example](#tag-technology-functions) | ||
## nfc.addNdefListener | ||
@@ -578,3 +586,157 @@ | ||
# Tag Technology Functions | ||
The tag technology functions provide access to I/O operations on a tag. Connect to a tag, send commands with tranceive, close the tag. See the [Android TagTechnology](https://developer.android.com/reference/android/nfc/tech/TagTechnology) and implementations like [IsoDep](https://developer.android.com/reference/android/nfc/tech/IsoDep) and [NfcV](https://developer.android.com/reference/android/nfc/tech/NfcV) for more details. These new APIs are promise based rather than using callbacks. | ||
#### ISO-DEP (ISO 14443-4) Example | ||
const DESFIRE_SELECT_PICC = '00 A4 04 00 07 D2 76 00 00 85 01 00'; | ||
const DESFIRE_SELECT_AID = '90 5A 00 00 03 AA AA AA 00' | ||
async function handleDesfire(nfcEvent) { | ||
const tagId = nfc.bytesToHexString(nfcEvent.tag.id); | ||
console.log('Processing', tagId); | ||
try { | ||
await nfc.connect('android.nfc.tech.IsoDep', 500); | ||
console.log('connected to', tagId); | ||
let response = await nfc.transceive(DESFIRE_SELECT_PICC); | ||
ensureResponseIs('9000', response); | ||
response = await nfc.transceive(DESFIRE_SELECT_AID); | ||
ensureResponseIs('9100', response); | ||
// 91a0 means the requested application not found | ||
alert('Selected application AA AA AA'); | ||
// more transcieve commands go here | ||
} catch (error) { | ||
alert(error); | ||
} finally { | ||
await nfc.close(); | ||
console.log('closed'); | ||
} | ||
} | ||
function ensureResponseIs(expectedResponse, buffer) { | ||
const responseString = util.arrayBufferToHexString(buffer); | ||
if (expectedResponse !== responseString) { | ||
const error = 'Expecting ' + expectedResponse + ' but received ' + responseString; | ||
throw error; | ||
} | ||
} | ||
function onDeviceReady() { | ||
nfc.addTagDiscoveredListener(handleDesfire); | ||
} | ||
document.addEventListener('deviceready', onDeviceReady, false); | ||
## nfc.connect | ||
Connect to the tag and enable I/O operations to the tag from this TagTechnology object. | ||
nfc.connect(tech); | ||
nfc.connect(tech, timeout); | ||
### Description | ||
Function `connect` enables I/O operations to the tag from this TagTechnology object. `nfc.connect` should be called after receiving a nfcEvent from the `addTagDiscoveredListener`. Only one TagTechnology object can be connected to a Tag at a time. | ||
See Android's [TagTechnology.connect()](https://developer.android.com/reference/android/nfc/tech/TagTechnology.html#connect()) for more info. | ||
### Parameters | ||
- __tech__: The tag technology e.g. android.nfc.tech.IsoDep | ||
- __timeout__: The transceive(byte[]) timeout in milliseconds [optional] | ||
### Returns | ||
- Promise when the connection is successful | ||
### Quick Example | ||
nfc.addTagDiscoveredListener(function(nfcEvent) { | ||
nfc.connect('android.nfc.tech.IsoDep', 500).then( | ||
() => console.log('connected to', nfc.bytesToHexString(nfcEvent.tag.id)), | ||
(error) => console.log('connection failed', error) | ||
); | ||
}) | ||
### Supported Platforms | ||
- Android | ||
## nfc.transceive | ||
Send raw command to the tag and receive the response. | ||
nfc.transceive(data); | ||
### Description | ||
Function `transceive` sends raw commands to the tag and receives the response. `nfc.connect` must be called before calling `transceive`. Data passed to transceive can be a hex string representation of bytes or an ArrayBuffer. The response is returned as an ArrayBuffer in the promise. | ||
See Android's documentation [IsoDep.transceive()](https://developer.android.com/reference/android/nfc/tech/IsoDep.html#transceive(byte[])), [NfcV.transceive()](https://developer.android.com/reference/android/nfc/tech/NfcV.html#transceive(byte[])), [MifareUltralight.transceive()](https://developer.android.com/reference/android/nfc/tech/MifareUltralight.html#transceive(byte[])) for more info. | ||
### Parameters | ||
- __data__: a string of hex data or an ArrayBuffer | ||
### Returns | ||
- Promise with the response data as an ArrayBuffer | ||
### Quick Example | ||
// Promise style | ||
nfc.transceive('90 5A 00 00 03 AA AA AA 00').then( | ||
response => console.log(util.arrayBufferToString(response)), | ||
error => console.log('Error selecting DESFire application') | ||
) | ||
// async await | ||
const response = await nfc.transceive('90 5A 00 00 03 AA AA AA 00'); | ||
console.log('response =',util.arrayBufferToString(response)); | ||
### Supported Platforms | ||
- Android | ||
## nfc.close | ||
Close TagTechnology connection. | ||
nfc.close(); | ||
### Description | ||
Function `close` disabled I/O operations to the tag from this TagTechnology object, and releases resources. | ||
See Android's [TagTechnology.close()](https://developer.android.com/reference/android/nfc/tech/TagTechnology.html#close()) for more info. | ||
### Parameters | ||
- none | ||
### Returns | ||
- Promise when the connection is successfully closed | ||
### Quick Example | ||
nfc.transceive().then( | ||
() => console.log('connection closed'), | ||
(error) => console.log('error closing connection', error); | ||
) | ||
### Supported Platforms | ||
- Android | ||
# NDEF | ||
@@ -581,0 +743,0 @@ |
@@ -498,2 +498,34 @@ /*jshint bitwise: false, camelcase: false, quotmark: false, unused: vars */ | ||
cordova.exec(win, fail, "NfcPlugin", "invalidateSession", []); | ||
}, | ||
connect: function(tech, timeout) { | ||
return new Promise(function(resolve, reject) { | ||
cordova.exec(resolve, reject, 'NfcPlugin', 'connect', [tech, timeout]); | ||
}); | ||
}, | ||
close: function() { | ||
return new Promise(function(resolve, reject) { | ||
cordova.exec(resolve, reject, 'NfcPlugin', 'close', []); | ||
}); | ||
}, | ||
// data - ArrayBuffer or string of hex data for transcieve | ||
// the results of transcieve are returned in the promise success as an ArrayBuffer | ||
transceive: function(data) { | ||
return new Promise(function(resolve, reject) { | ||
let buffer; | ||
if (typeof data === 'string') { | ||
buffer = util.hexStringToArrayBuffer(data); | ||
} else if (data instanceof ArrayBuffer) { | ||
buffer = data; | ||
} else if (data instanceof Uint8Array) { | ||
buffer = data.buffer; | ||
} else { | ||
reject("Expecing an ArrayBuffer or String"); | ||
} | ||
cordova.exec(resolve, reject, 'NfcPlugin', 'transceive', [buffer]); | ||
}); | ||
} | ||
@@ -646,2 +678,53 @@ | ||
return false; | ||
}, | ||
/** | ||
* Convert an ArrayBuffer to a hex string | ||
* | ||
* @param {ArrayBuffer} buffer | ||
* @returns {srting} - hex representation of bytes e.g. 000407AF | ||
*/ | ||
arrayBufferToHexString: function(buffer) { | ||
function toHexString(byte) { | ||
return ('0' + (byte & 0xFF).toString(16)).slice(-2); | ||
} | ||
const typedArray = new Uint8Array(buffer); | ||
var array = Array.from(typedArray); // need to convert to [] so our map result is not typed | ||
const parts = array.map(i => toHexString(i)); | ||
return parts.join(''); | ||
}, | ||
/** | ||
* Convert a hex string to an ArrayBuffer. | ||
* | ||
* @param {string} hexString - hex representation of bytes | ||
* @return {ArrayBuffer} - The bytes in an ArrayBuffer. | ||
*/ | ||
hexStringToArrayBuffer: function(hexString) { | ||
// remove any delimiters - space, dash, or colon | ||
hexString = hexString.replace(/[\s-:]/g, ''); | ||
// remove the leading 0x | ||
hexString = hexString.replace(/^0x/, ''); | ||
// ensure even number of characters | ||
if (hexString.length % 2 != 0) { | ||
console.log('WARNING: expecting an even number of characters in the hexString'); | ||
} | ||
// check for some non-hex characters | ||
const bad = hexString.match(/[G-Z\s]/i); | ||
if (bad) { | ||
console.log('WARNING: found non-hex characters', bad); | ||
} | ||
// split the string into pairs of octets | ||
const pairs = hexString.match(/[\dA-F]{2}/gi); | ||
// convert the octets to integers | ||
const ints = pairs.map(s => parseInt(s, 16)); | ||
var array = new Uint8Array(ints); | ||
return array.buffer; | ||
} | ||
@@ -648,0 +731,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
404943
1169
0
1083