Skip to content

Commit

Permalink
Merge pull request #389 from pschlang/provide-max-transceive-length
Browse files Browse the repository at this point in the history
Return the tag tech's max transceive length after connect()
  • Loading branch information
don authored Apr 7, 2020
2 parents 0783ec0 + 55dc1fb commit df3afc1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ See Android's [TagTechnology.connect()](https://developer.android.com/reference/

### Returns

- Promise when the connection is successful
- Promise when the connection is successful, optionally with a maxTransceiveLength attribute in case the tag technology supports it

### Quick Example

Expand Down
14 changes: 13 additions & 1 deletion src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,25 @@ private void connect(final String tech, final int timeout, final CallbackContext
return;
}

JSONObject resultObject = new JSONObject();

// get technologies supported by this tag
List<String> techList = Arrays.asList(tag.getTechList());
if (techList.contains(tech)) {
// use reflection to call the static function Tech.get(tag)
tagTechnologyClass = Class.forName(tech);
Method method = tagTechnologyClass.getMethod("get", Tag.class);
tagTechnology = (TagTechnology) method.invoke(null, tag);

// If the tech supports it, return maxTransceiveLength and return it to the user
try {
Method maxTransceiveLengthMethod = tagTechnologyClass.getMethod("getMaxTransceiveLength");
resultObject.put("maxTransceiveLength", maxTransceiveLengthMethod.invoke(tagTechnology));
} catch(NoSuchMethodException e) {
// Some technologies do not support this, so just ignore.
} catch(JSONException e) {
Log.e(TAG, "Error serializing JSON", e);
}
}

if (tagTechnology == null) {
Expand All @@ -886,7 +898,7 @@ private void connect(final String tech, final int timeout, final CallbackContext

tagTechnology.connect();
setTimeout(timeout);
callbackContext.success();
callbackContext.success(resultObject);

} catch (IOException ex) {
Log.e(TAG, "Tag connection failed", ex);
Expand Down

0 comments on commit df3afc1

Please sign in to comment.