Skip to content

Commit

Permalink
feat(android): allow requestTechnology resolves to tags even when tech
Browse files Browse the repository at this point in the history
doesn't match
  • Loading branch information
whitedogg13 committed Feb 22, 2021
1 parent 6c64072 commit 8b7f862
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
21 changes: 9 additions & 12 deletions android/src/main/java/community/revteltech/nfc/NfcManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,17 @@ public void closeTechnology(Callback callback) {
public void getTag(Callback callback) {
synchronized(this) {
if (techRequest != null) {
try {
Tag tag = techRequest.getTagHandle();
WritableMap parsed = null;
if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
Tag tag = techRequest.getTagHandle();
WritableMap parsed = tag2React(tag);
if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
try {
Ndef ndef = Ndef.get(tag);
parsed = ndef2React(ndef, new NdefMessage[] { ndef.getCachedNdefMessage() });
} else {
parsed = tag2React(tag);
} catch (Exception ex) {
// bypass
}
callback.invoke(null, parsed);
} catch (Exception ex) {
Log.d(LOG_TAG, "getTag fail");
callback.invoke("getTag fail");
}
callback.invoke(null, parsed);
} else {
callback.invoke("no tech request available");
}
Expand Down Expand Up @@ -1140,8 +1137,8 @@ private WritableMap parseNfcIntent(Intent intent) {
if (result) {
techRequest.getPendingCallback().invoke(null, techRequest.getTechType());
} else {
techRequest.getPendingCallback().invoke("fail to connect tag");
techRequest = null;
// this indicates that we get a NFC tag, but none of the user required tech is matched
techRequest.getPendingCallback().invoke(null, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ boolean connect(Tag tag) {
return false;
}

mTag = tag;

for (int i = 0; i < mTechTypes.size(); i++) {
String techType = (String)mTechTypes.get(i);

Expand Down Expand Up @@ -84,7 +86,6 @@ boolean connect(Tag tag) {
Log.d(LOG_TAG, "connect to " + techType);
mTech.connect();
mTechType = techType;
mTag = tag;
return true;
} catch (Exception ex) {
Log.d(LOG_TAG, "fail to connect tech");
Expand All @@ -94,7 +95,6 @@ boolean connect(Tag tag) {
// not connected, restore to default
mTech = null;
mTechType = null;
mTag = null;

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ declare module 'react-native-nfc-manager' {
requestTechnology(
tech: NfcTech | NfcTech[],
options?: RegisterTagEventOpts,
): Promise<NfcTech>;
): Promise<NfcTech | null>;
cancelTechnologyRequest: () => Promise<void>;
getTag: () => Promise<TagEvent | null>;

Expand Down

0 comments on commit 8b7f862

Please sign in to comment.