diff --git a/NfcManager.js b/NfcManager.js index 6126e82..c9966e7 100644 --- a/NfcManager.js +++ b/NfcManager.js @@ -583,6 +583,25 @@ class NfcManager { }) } + // ------------------------------------- + // setTimeout works for NfcA, NfcF, IsoDep, MifareClassic, MifareUltralight + // ------------------------------------- + setTimeout(timeout) { + if (Platform.OS === 'ios') { + return Promise.reject('not implemented'); + } + + return new Promise((resolve, reject) => { + NativeNfcManager.setTimeout(timeout, (err, result) => { + if (err) { + reject(err); + } else { + resolve(result); + } + }) + }) + } + // ------------------------------------- // transceive works for NfcA, NfcB, NfcF, NfcV, IsoDep and MifareUltralight // ------------------------------------- @@ -601,6 +620,22 @@ class NfcManager { }) }) } + + getMaxTransceiveLength() { + if (Platform.OS === 'ios') { + return Promise.reject('not implemented'); + } + + return new Promise((resolve, reject) => { + NativeNfcManager.getMaxTransceiveLength((err, result) => { + if (err) { + reject(err); + } else { + resolve(result); + } + }) + }) + } } export default new NfcManager(); diff --git a/README.md b/README.md index c6ce129..9a14da6 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,7 @@ Make the tag become read-only. ## Generic NfcTech API [Android only] -To use the these API, you first need to request the `NfcTech.Ndef` technology (see `requestTechnology`). Once you have the tech request, you can use the following methods: +To use the these API, you first need to request specific NFC technology (see `requestTechnology`). Once you have the tech request, you can use the following methods: ### transceive(bytes) [Android only] Send raw data to a tag and receive the response. This API is compatible with following NfcTech: NfcA, NfcB, NfcF, NfcV, IsoDep and MifareUltralight. @@ -355,6 +355,24 @@ Send raw data to a tag and receive the response. This API is compatible with fol __Arguments__ - `bytes` - `array` - the raw data you want to send, which is an array of bytes +### getMaxTransceiveLength() [Android only] +Return the maximum number of bytes that can be sent. This API is compatible with following NfcTech: NfcA, NfcB, NfcF, NfcV, IsoDep and MifareUltralight. + +> This method returns a promise: +> * if resolved, the resolved value will be the maximum number of bytes that can be sent to transceive. +> * if rejected, it means either the request is cancelled, the operation fail or the operation is not supported in current tech handle. + +### setTimeout(timeout) [Android only] +Set the transceive timeout in milliseconds. This API is compatible with following NfcTech: NfcA, NfcF, IsoDep, MifareClassic and MifareUltralight. + +> This method returns a promise: +> * if resolved, it means the setTimeout operation is success. +> * if rejected, it means either the request is cancelled, the operation fail or the operation is not supported in current tech handle. + +__Arguments__ +- `timeout` - `int` - the transceive timeout in milliseconds + + ## NfcTech.MifareClassic API [Android only] This module enables you to read encrypted [Mifare Classic](https://en.wikipedia.org/wiki/MIFARE#MIFARE_Classic_family) cards (as long as you have the authentication keys). A concrete example can be found in `example/AndroidMifareClassic.js` diff --git a/android/src/main/java/community/revteltech/nfc/NfcManager.java b/android/src/main/java/community/revteltech/nfc/NfcManager.java index 5d65748..64fa768 100644 --- a/android/src/main/java/community/revteltech/nfc/NfcManager.java +++ b/android/src/main/java/community/revteltech/nfc/NfcManager.java @@ -516,6 +516,51 @@ public void makeReadOnly(Callback callback) { } } + @ReactMethod + public void setTimeout(int timeout, Callback callback) { + synchronized (this) { + if (techRequest != null) { + try { + String tech = techRequest.getTechType(); + TagTechnology baseTechHandle = techRequest.getTechHandle(); + // TagTechnology is the base class for each tech (ex, NfcA, NfcB, IsoDep ...) + // but it doesn't provide transceive in its interface, so we need to explicitly cast it + if (tech.equals("NfcA")) { + NfcA techHandle = (NfcA) baseTechHandle; + techHandle.setTimeout(timeout); + callback.invoke(); + return; + } else if (tech.equals("NfcF")) { + NfcF techHandle = (NfcF) baseTechHandle; + techHandle.setTimeout(timeout); + callback.invoke(); + return; + } else if (tech.equals("IsoDep")) { + IsoDep techHandle = (IsoDep) baseTechHandle; + techHandle.setTimeout(timeout); + callback.invoke(); + return; + } else if (tech.equals("MifareClassic")) { + MifareClassic techHandle = (MifareClassic) baseTechHandle; + techHandle.setTimeout(timeout); + callback.invoke(); + return; + } else if (tech.equals("MifareUltralight")) { + MifareUltralight techHandle = (MifareUltralight) baseTechHandle; + techHandle.setTimeout(timeout); + callback.invoke(); + return; + } + } catch (Exception ex) { + Log.d(LOG_TAG, "setTimeout fail"); + } + callback.invoke("setTimeout not supported"); + } else { + callback.invoke("no tech request available"); + } + } + } + @ReactMethod public void transceive(ReadableArray rnArray, Callback callback) { synchronized(this) { @@ -575,6 +620,59 @@ public void transceive(ReadableArray rnArray, Callback callback) { } } + @ReactMethod + public void getMaxTransceiveLength(Callback callback) { + synchronized(this) { + if (techRequest != null) { + try { + String tech = techRequest.getTechType(); + + TagTechnology baseTechHandle = techRequest.getTechHandle(); + // TagTechnology is the base class for each tech (ex, NfcA, NfcB, IsoDep ...) + // but it doesn't provide transceive in its interface, so we need to explicitly cast it + if (tech.equals("NfcA")) { + NfcA techHandle = (NfcA)baseTechHandle; + int max = techHandle.getMaxTransceiveLength(); + callback.invoke(null, max); + return; + } else if (tech.equals("NfcB")) { + NfcB techHandle = (NfcB)baseTechHandle; + int max = techHandle.getMaxTransceiveLength(); + callback.invoke(null, max); + return; + } else if (tech.equals("NfcF")) { + NfcF techHandle = (NfcF)baseTechHandle; + int max = techHandle.getMaxTransceiveLength(); + callback.invoke(null, max); + return; + } else if (tech.equals("NfcV")) { + NfcV techHandle = (NfcV)baseTechHandle; + int max = techHandle.getMaxTransceiveLength(); + callback.invoke(null, max); + return; + } else if (tech.equals("IsoDep")) { + IsoDep techHandle = (IsoDep)baseTechHandle; + int max = techHandle.getMaxTransceiveLength(); + callback.invoke(null, max); + return; + } else if (tech.equals("MifareUltralight")) { + MifareUltralight techHandle = (MifareUltralight)baseTechHandle; + int max = techHandle.getMaxTransceiveLength(); + callback.invoke(null, max); + return; + } + Log.d(LOG_TAG, "getMaxTransceiveLength not supported"); + return; + } catch (Exception ex) { + Log.d(LOG_TAG, "getMaxTransceiveLength fail"); + } + return; + } else { + callback.invoke("no tech request available"); + } + } + } + @ReactMethod public void cancelNdefWrite(Callback callback) { synchronized(this) {