-
Notifications
You must be signed in to change notification settings - Fork 375
/
Manager.java
304 lines (240 loc) · 12.2 KB
/
Manager.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package chat.berty.ble;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.arch.core.util.Function;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.ParcelUuid;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.Manifest;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;
import android.bluetooth.BluetoothGatt;
import static android.bluetooth.BluetoothGattCharacteristic.*;
import static android.bluetooth.BluetoothGattService.SERVICE_TYPE_PRIMARY;
import static android.content.Context.BLUETOOTH_SERVICE;
public class Manager {
private static Manager instance = null;
private Context mContext;
private ActivityGetter mReactContext;
private Object RmReactContext;
final static int BLUETOOTH_ENABLE_REQUEST = 1;
protected UUID SERVICE_UUID = UUID.fromString("A06C6AB8-886F-4D56-82FC-2CF8610D6664");
protected UUID WRITER_UUID = UUID.fromString("000CBD77-8D30-4EFF-9ADD-AC5F10C2CC1C");
protected UUID CLOSER_UUID = UUID.fromString("AD127A46-D065-4D72-B15A-EB2B3DA20561");
protected UUID IS_READY_UUID = UUID.fromString("D27DE0B5-2170-4C59-9C0B-750C760C74E6");
protected UUID MA_UUID = UUID.fromString("9B827770-DC72-4C55-B8AE-0870C7AC15A8");
protected UUID PEER_ID_UUID = UUID.fromString("0EF50D30-E208-4315-B323-D05E0A23E6B3");
protected UUID ACCEPT_UUID = UUID.fromString("6F110ECA-9FCC-4BB3-AB45-6F13565E2E34");
public static String TAG = "chat.berty.ble.Manager";
protected BluetoothAdapter mBluetoothAdapter;
protected BluetoothGattServer mBluetoothGattServer;
protected BluetoothLeAdvertiser mBluetoothLeAdvertiser;
protected BluetoothLeScanner mBluetoothLeScanner;
protected BluetoothGattService mService;
protected BluetoothGattCharacteristic acceptCharacteristic;
protected BluetoothGattCharacteristic maCharacteristic;
protected BluetoothGattCharacteristic peerIDCharacteristic;
protected BluetoothGattCharacteristic writerCharacteristic;
protected BluetoothGattCharacteristic isRdyCharacteristic;
protected BluetoothGattCharacteristic closerCharacteristic;
public interface ActivityGetter {
@Nullable Activity getCurrentActivity();
}
private Manager() {
super();
Log.e(TAG, "BLEManager init");
}
public void setmContext(Context ctx) {
Log.e(TAG, "BLEManager context set");
mContext = ctx;
}
public void setmReactContext(Object rCtx, Object t) {
Log.e(TAG, "BLEManager ReactContext set");
RmReactContext = t;
mReactContext = (ActivityGetter)rCtx;
}
public static Manager getInstance() {
if (instance == null) {
synchronized (Manager.class) {
if (instance == null) {
instance = new Manager();
instance.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
}
}
Log.e(TAG, "ALL INSTANCES " + instance);
return instance;
}
public boolean isRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
public static AdvertiseSettings createAdvSettings(boolean connectable, int timeoutMillis) {
AdvertiseSettings.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
builder = new AdvertiseSettings.Builder();
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
builder.setConnectable(connectable);
builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
builder.setTimeout(timeoutMillis);
return builder.build();
}
return null;
}
public AdvertiseData makeAdvertiseData() {
ParcelUuid pUuid = new ParcelUuid(SERVICE_UUID);
AdvertiseData.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.setIncludeTxPowerLevel(false)
.addServiceUuid(pUuid);
return builder.build();
}
return null;
}
private BluetoothGattService createService() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
Boolean truer = true;
mService = new BluetoothGattService(SERVICE_UUID, SERVICE_TYPE_PRIMARY);
acceptCharacteristic = new BluetoothGattCharacteristic(ACCEPT_UUID, PROPERTY_READ | PROPERTY_WRITE, PERMISSION_READ | PERMISSION_WRITE);
maCharacteristic = new BluetoothGattCharacteristic(MA_UUID, PROPERTY_READ, PERMISSION_READ);
peerIDCharacteristic = new BluetoothGattCharacteristic(PEER_ID_UUID, PROPERTY_READ, PERMISSION_READ);
writerCharacteristic = new BluetoothGattCharacteristic(WRITER_UUID, PROPERTY_WRITE, PERMISSION_WRITE);
isRdyCharacteristic = new BluetoothGattCharacteristic(IS_READY_UUID, PROPERTY_WRITE, PERMISSION_WRITE);
closerCharacteristic = new BluetoothGattCharacteristic(CLOSER_UUID, PROPERTY_WRITE, PERMISSION_WRITE);
truer = mService.addCharacteristic(acceptCharacteristic) && truer;
truer = mService.addCharacteristic(maCharacteristic) && truer;
truer = mService.addCharacteristic(peerIDCharacteristic) && truer;
truer = mService.addCharacteristic(writerCharacteristic) && truer;
truer = mService.addCharacteristic(isRdyCharacteristic) && truer;
truer = mService.addCharacteristic(closerCharacteristic) && truer;
if (truer == false) {
Log.e(TAG, "Error adding characteristic");
}
return mService;
}
return null;
}
private BluetoothGattServerCallback mGattServerCallback;
public void initGattServerCallBack() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
}
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
}
@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
}
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
}
@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
Log.d(TAG, "new mtu is: " + mtu);
}
};
}
}
private AdvertiseCallback mAdvertisingCallback;
public void initAdvertiseCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mAdvertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
Log.e( "BLE", "Advertising onStartSuccess: " + settingsInEffect);
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
String errorString;
switch (errorCode) {
case ADVERTISE_FAILED_DATA_TOO_LARGE: errorString = "ADVERTISE_FAILED_DATA_TOO_LARGE";
break;
case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS: errorString = "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS";
break;
case ADVERTISE_FAILED_ALREADY_STARTED: errorString = "ADVERTISE_FAILED_ALREADY_STARTED";
break;
case ADVERTISE_FAILED_INTERNAL_ERROR: errorString = "ADVERTISE_FAILED_INTERNAL_ERROR";
break;
case ADVERTISE_FAILED_FEATURE_UNSUPPORTED: errorString = "ADVERTISE_FAILED_FEATURE_UNSUPPORTED";
break;
default: errorString = "UNKNOWN ADVERTISE FAILURE";
break;
}
Log.e(TAG, "Advertising onStartFailure: " + errorString);
if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR) {
// mBluetoothAdapter.getBluetoothLeAdvertiser().stopAdvertising(mAdvertisingCallback);
// mBluetoothLeAdvertiser.startAdvertising(createAdvSettings(
// true, 300), makeAdvertiseData(), mAdvertisingCallback);
}
super.onStartFailure(errorCode);
}
};
}
}
public String realTest() {
Log.e(TAG, "THIS IS REAL SHIT GUY");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Activity curActivity = mReactContext.getCurrentActivity();
if (!mBluetoothAdapter.isEnabled()) {
Log.e(TAG, "NEED TO ENABLE");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
curActivity.startActivityForResult(enableBtIntent, BLUETOOTH_ENABLE_REQUEST);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
initGattServerCallBack();
initGattCallback();
initAdvertiseCallback();
BluetoothManager mb = (BluetoothManager) mContext.getSystemService(BLUETOOTH_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
mBluetoothGattServer = mb.openGattServer(mContext, mGattServerCallback);
mBluetoothGattServer.addService(createService());
AdvertiseSettings settings = createAdvSettings(true, 0);
AdvertiseData advData = makeAdvertiseData();
mBluetoothLeAdvertiser.startAdvertising(settings, advData, mAdvertisingCallback);
}
}
return "COMING FROM THAT MOTHA FUCKING JAVA";
}
}