Skip to content

Commit

Permalink
fix can't unlock noke device
Browse files Browse the repository at this point in the history
  • Loading branch information
dendakhongduong committed Sep 18, 2018
1 parent 1d3afc1 commit 4ab86bc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
46 changes: 35 additions & 11 deletions android/src/main/java/com/lynkxyz/noke/RNNokeModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class RNNokeModule extends ReactContextBaseJavaModule {
private NokeDeviceManagerService mNokeService = null;
private NokeDevice currentNoke;
private int lastEventCode = -1;
private boolean flag = false;
private boolean isDisconnect = false;

public RNNokeModule(ReactApplicationContext context) {
// Pass in the context to the constructor and save it so you can emit events
Expand Down Expand Up @@ -95,7 +97,7 @@ public void stopScan(Promise promise) {
promise.resolve(event);
}

private NokeDevice getCurrentNoke(String macAddress) {
private NokeDevice getNoke(String macAddress) {
if(mNokeService != null) {
LinkedHashMap<String, NokeDevice> nokeDevices = mNokeService.nokeDevices;

Expand All @@ -111,7 +113,7 @@ private NokeDevice addNokeIfNeeded(NokeHashMap nokeHashMap) {
String command = nokeHashMap.getCommand();
String macAddress = nokeHashMap.getMacAddress();

NokeDevice nokeDevice = getCurrentNoke(macAddress);
NokeDevice nokeDevice = getNoke(macAddress);

if(mNokeService == null) {
return null;
Expand All @@ -133,6 +135,7 @@ private NokeDevice addNokeIfNeeded(NokeHashMap nokeHashMap) {
}

currentNoke = nokeDevice;
flag = false;

return nokeDevice;
}
Expand Down Expand Up @@ -175,7 +178,7 @@ public void sendCommands(ReadableMap data, Promise promise) {
NokeHashMap nokeHashMap = new NokeHashMap(data);
String macAddress = nokeHashMap.getMacAddress();
ArrayList<String> commands = nokeHashMap.getCommands();
NokeDevice nokeDevice = getCurrentNoke(macAddress);
NokeDevice nokeDevice = getNoke(macAddress);

if(nokeDevice == null) {
promise.reject("100", "Noke device is null");
Expand Down Expand Up @@ -208,15 +211,15 @@ public void removeNokeDevice(String mac, Promise promise) {
public void offlineUnlock(ReadableMap data, Promise promise) {
NokeHashMap nokeHashMap = new NokeHashMap(data);
String macAddress = nokeHashMap.getMacAddress();
NokeDevice nokeDevice = getCurrentNoke(macAddress);
NokeDevice nokeDevice = getNoke(macAddress);

if(nokeDevice == null) {
promise.reject("100", "Noke device is null");
return;
}

WritableMap event = createCommonEvents(nokeDevice);
if(lastEventCode == 4) {
if(lastEventCode == 4 || lastEventCode == 5) {
promise.resolve(event);
return;
}
Expand All @@ -227,6 +230,23 @@ public void offlineUnlock(ReadableMap data, Promise promise) {
promise.resolve(event);
}

@ReactMethod
public void disconnect(String macAddress, Promise promise) {
// NokeDevice nokeDevice = getNoke(macAddress);

// mNokeService.disconnectNoke(nokeDevice);

promise.resolve(null);
}

@ReactMethod
public void disconnectAll(Promise promise) {
mNokeService.disconnectNoke(currentNoke);
mNokeService.stopScanning();

promise.resolve(null);
}

@ReactMethod
public void getDeviceInfo(Promise promise) {
WritableMap event = Arguments.createMap();
Expand All @@ -252,7 +272,7 @@ public void getDeviceInfo(Promise promise) {

@ReactMethod
public void setAPIKey(String apiKey, Promise promise) {
mNokeService.setApiKey(apiKey);
// mNokeService.setApiKey(apiKey);

promise.resolve(null);
}
Expand Down Expand Up @@ -298,12 +318,13 @@ public void onServiceDisconnected(ComponentName classname) {
private NokeServiceListener mNokeServiceListener = new NokeServiceListener() {
@Override
public void onNokeDiscovered(NokeDevice noke) {
mNokeService.stopScanning();
if(currentNoke.getMac().equals(noke.getMac())) {
if(!flag && currentNoke.getMac().equals(noke.getMac())) {
mNokeService.stopScanning();
mNokeService.connectToNoke(noke);
lastEventCode = 0;
flag = true;
emitDeviceEvent("onNokeDiscovered", createCommonEvents(noke));
}
lastEventCode = 0;
emitDeviceEvent("onNokeDiscovered", createCommonEvents(noke));
}

@Override
Expand All @@ -327,15 +348,18 @@ public void onNokeSyncing(NokeDevice noke) {
@Override
public void onNokeUnlocked(NokeDevice noke) {
emitDeviceEvent("onNokeUnlocked", createCommonEvents(noke));
// mNokeService.disconnectNoke(noke);
lastEventCode = 4;
flag = false;
}

@Override
public void onNokeDisconnected(NokeDevice noke) {
emitDeviceEvent("onNokeDisconnected", createCommonEvents(noke));
mNokeService.stopScanning();
lastEventCode = 5;
mNokeService.uploadData();
flag = false;
// mNokeService.uploadData();
}

@Override
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
startScan: RNNoke.startScan,
stopScan: RNNoke.stopScan,
disconnect: RNNoke.disconnect,
disconnectAll: RNNoke.disconnectAll,
getDeviceInfo: RNNoke.getDeviceInfo,
setOfflineData: RNNoke.setOfflineData,
addNokeDeviceOnce: RNNoke.addNokeDeviceOnce,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-noke",
"version": "1.5.9",
"version": "1.5.10",
"description": "Noke SDK for React-Native",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4ab86bc

Please sign in to comment.