Skip to content

Commit

Permalink
feat: support getMaxTransceiveLength and setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
whitedogg13 committed Feb 17, 2019
1 parent edb85aa commit e759c39
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
35 changes: 35 additions & 0 deletions NfcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -------------------------------------
Expand All @@ -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();
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`
Expand Down
98 changes: 98 additions & 0 deletions android/src/main/java/community/revteltech/nfc/NfcManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e759c39

Please sign in to comment.