This repository was archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathItoBluetoothModule.java
More file actions
125 lines (111 loc) · 4.35 KB
/
ItoBluetoothModule.java
File metadata and controls
125 lines (111 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.reactlibrary;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import org.itoapp.DistanceCallback;
import org.itoapp.PublishUUIDsCallback;
import org.itoapp.TracingServiceInterface;
import org.itoapp.strict.service.TracingService;
public class ItoBluetoothModule extends ReactContextBaseJavaModule {
private static final String LOG_TAG = "ItoBluetoothModule";
private final ReactApplicationContext reactContext;
private TracingServiceInterface tracingServiceInterface;
private DistanceCallback.Stub nativeContactCallback = new DistanceCallback.Stub() {
@Override
public void onDistanceMeasurements(float[] distances) {
Log.d(LOG_TAG, "emitting onDistancesChanged");
reactContext.
getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("onDistancesChanged", Arguments.fromArray(distances));
}
};
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.d(LOG_TAG, "Service connected");
tracingServiceInterface = TracingServiceInterface.Stub.asInterface(service);
try {
Log.d(LOG_TAG, "Registering callback");
tracingServiceInterface.setDistanceCallback(nativeContactCallback);
} catch (RemoteException e) {
Log.e(LOG_TAG, "looks like the service already crashed!", e);
}
}
public void onServiceDisconnected(ComponentName className) {
Log.d(LOG_TAG, "Service disconnected");
tracingServiceInterface = null;
}
};
public ItoBluetoothModule(ReactApplicationContext reactContext) {
super(reactContext);
Log.d(LOG_TAG, "Creating ItoBluetoothModule");
this.reactContext = reactContext;
Intent intent = new Intent(reactContext, TracingService.class);
reactContext.startService(intent);
bindService();
}
//make this method synchronous because it has to return a boolean
@ReactMethod()
public boolean isPossiblyInfected() {
try {
return tracingServiceInterface.isPossiblyInfected();
} catch (RemoteException e) {
Log.e(LOG_TAG, "Could not get infected status", e);
return false;
}
}
//make this method synchronous because it has to return a boolean
@ReactMethod()
public int getLatestFetchTime() {
try {
return tracingServiceInterface.getLatestFetchTime();
} catch (RemoteException e) {
Log.e(LOG_TAG, "Could not get latest fetch time", e);
return -1;
}
}
@ReactMethod
public void publishBeaconUUIDs(int from, int to, Callback callback) {
try {
tracingServiceInterface.publishBeaconUUIDs(from * 1000L, to * 1000L, new PublishUUIDsCallback.Stub() {
@Override
public void onSuccess() {
callback.invoke(true);
}
@Override
public void onFailure() {
callback.invoke(false);
}
});
} catch (RemoteException e) {
e.printStackTrace();
}
}
@ReactMethod
public void restartTracing() {
try {
tracingServiceInterface.restartTracingService();
} catch (RemoteException e) {
Log.e(LOG_TAG, "Could not get TracingService", e);
}
}
private void bindService() {
Log.d(LOG_TAG, "binding service");
Intent intent = new Intent(reactContext, TracingService.class);
reactContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public String getName() {
return "ItoBluetooth";
}
}