Skip to content

Commit

Permalink
feat: requestTechnology support multi-techs
Browse files Browse the repository at this point in the history
  • Loading branch information
whitedogg13 committed Aug 18, 2019
1 parent dcb26b8 commit 65e8f02
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 83 deletions.
20 changes: 4 additions & 16 deletions NfcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,11 @@ class NfcManager {
// -------------------------------------
requestTechnology(tech) {
return new Promise((resolve, reject) => {
NativeNfcManager.requestTechnology(tech, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
})
})
}
if (typeof tech === 'string') {
tech = [tech];
}

requestTechnologies(techs) {
if (Platform.OS === 'ios') {
return Promise.reject('not implemented');
}

return new Promise((resolve, reject) => {
NativeNfcManager.requestTechnologies(techs, (err, result) => {
NativeNfcManager.requestTechnology(tech, (err, result) => {
if (err) {
reject(err);
} else {
Expand Down
22 changes: 2 additions & 20 deletions android/src/main/java/community/revteltech/nfc/NfcManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,7 @@ public void cancelTechnologyRequest(Callback callback) {
}

@ReactMethod
public void requestTechnology(String tech, Callback callback) {
synchronized(this) {
if (!isForegroundEnabled) {
callback.invoke("you should requestTagEvent first");
return;
}

if (hasPendingRequest()) {
callback.invoke("You can only issue one request at a time");
} else {
techRequest = new TagTechnologyRequest(tech, callback);
}
}
}

@ReactMethod
public void requestTechnologies(ReadableArray techs, Callback callback) {
public void requestTechnology(ReadableArray techs, Callback callback) {
synchronized(this) {
if (!isForegroundEnabled) {
callback.invoke("you should requestTagEvent first");
Expand All @@ -150,7 +134,6 @@ public void requestTechnologies(ReadableArray techs, Callback callback) {
}
}


@ReactMethod
public void closeTechnology(Callback callback) {
synchronized(this) {
Expand Down Expand Up @@ -585,7 +568,6 @@ public void setTimeout(int timeout, Callback callback) {
}
}


@ReactMethod
public void connect(ReadableArray techs, Callback callback){
synchronized(this) {
Expand Down Expand Up @@ -1119,7 +1101,7 @@ private WritableMap parseNfcIntent(Intent intent) {
if (!techRequest.isConnected()) {
boolean result = techRequest.connect(tag);
if (result) {
techRequest.getPendingCallback().invoke();
techRequest.getPendingCallback().invoke(null, techRequest.getTechType());
} else {
techRequest.getPendingCallback().invoke("fail to connect tag");
techRequest = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@ class TagTechnologyRequest {
static String LOG_TAG = "NfcManager-tech";
Tag mTag;
TagTechnology mTech;
String mTechType;
ArrayList<Object> mTechTypes;

String mTechType; // the actual connected type
ArrayList<Object> mTechTypes; // the desired types
Callback mJsCallback;

TagTechnologyRequest(String techType, Callback cb) {
mTechTypes = new ArrayList<Object>();
mTechTypes.add(techType);
mJsCallback = cb;
}

TagTechnologyRequest(ArrayList<Object> techTypes, Callback cb) {
mTechTypes = techTypes;
mJsCallback = cb;
}

String getTechType() {
return mTechType;
}
Expand All @@ -48,7 +42,7 @@ TagTechnology getTechHandle() {
}

boolean isConnected() {
return mTag != null;
return mTech != null;
}

boolean connect(Tag tag) {
Expand All @@ -57,47 +51,49 @@ boolean connect(Tag tag) {
return false;
}

mTech = null;
mTag = tag;
int i = 0;
boolean connection = false;
while(i < mTechTypes.size() && connection == false){
mTechType = (String)mTechTypes.get(i);
i++;
if (mTechType.equals("Ndef")) {
mTech = Ndef.get(tag);
} else if (mTechType.equals("NfcA")) {
mTech = NfcA.get(tag);
} else if (mTechType.equals("NfcB")) {
mTech = NfcB.get(tag);
} else if (mTechType.equals("NfcF")) {
mTech = NfcF.get(tag);
} else if (mTechType.equals("NfcV")) {
mTech = NfcV.get(tag);
} else if (mTechType.equals("IsoDep")) {
mTech = IsoDep.get(tag);
} else if (mTechType.equals("MifareClassic")) {
mTech = MifareClassic.get(tag);
} else if (mTechType.equals("MifareUltralight")) {
mTech = MifareUltralight.get(tag);
}
for (int i = 0; i < mTechTypes.size(); i++) {
String techType = (String)mTechTypes.get(i);

if (mTech == null) {
connection = false;
}
if (techType.equals("Ndef")) {
mTech = Ndef.get(tag);
} else if (techType.equals("NfcA")) {
mTech = NfcA.get(tag);
} else if (techType.equals("NfcB")) {
mTech = NfcB.get(tag);
} else if (techType.equals("NfcF")) {
mTech = NfcF.get(tag);
} else if (techType.equals("NfcV")) {
mTech = NfcV.get(tag);
} else if (techType.equals("IsoDep")) {
mTech = IsoDep.get(tag);
} else if (techType.equals("MifareClassic")) {
mTech = MifareClassic.get(tag);
} else if (techType.equals("MifareUltralight")) {
mTech = MifareUltralight.get(tag);
}

try {
Log.d(LOG_TAG, "connect to " + mTechType);
mTech.connect();
connection = true;
} catch (Exception ex) {
Log.d(LOG_TAG, "fail to connect tech");
connection = false;
}
if (mTech == null) {
continue;
}

try {
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");
}
}
return connection;
}

// not connected, restore to default
mTech = null;
mTechType = null;
mTag = null;

return false;
}

void close() {
try {
Expand Down
64 changes: 64 additions & 0 deletions example/AppMultiTech.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';
import {
View,
Text,
Button,
Platform,
TouchableOpacity,
Linking,
TextInput,
ScrollView,
} from 'react-native';
import NfcManager, {NfcTech} from '../NfcManager';

class AppMultiTech extends React.Component {
componentDidMount() {
NfcManager.start();
}

componentWillUnmount() {
this._cleanUp();
}

render() {
return (
<View style={{padding: 20}}>
<Text>NFC Multi-tech Demo</Text>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._testMultiTech}
>
<Text>Test</Text>
</TouchableOpacity>

<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._cleanUp}
>
<Text>Cancel Test</Text>
</TouchableOpacity>

</View>
)
}

_cleanUp = () => {
NfcManager.cancelTechnologyRequest().catch(() => 0);
NfcManager.unregisterTagEvent().catch(() => 0);
}

_testMultiTech = async () => {
try {
await NfcManager.registerTagEvent()
let resp = await NfcManager.requestTechnology([NfcTech.IsoDep, NfcTech.Ndef]);
console.warn(resp);
let tag = await NfcManager.getTag();
console.warn(tag);
this._cleanUp();
} catch (ex) {
this._cleanUp();
}
}
}

export default AppMultiTech;

0 comments on commit 65e8f02

Please sign in to comment.