@iotize/device-com-ble.cordova 3.6.0 → 3.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/iotize-device-com-ble.cordova.umd.js +19 -0
- package/bundles/iotize-device-com-ble.cordova.umd.js.map +1 -1
- package/bundles/iotize-device-com-ble.cordova.umd.min.js +1 -1
- package/bundles/iotize-device-com-ble.cordova.umd.min.js.map +1 -1
- package/esm2015/iotize-device-com-ble.cordova.ngsummary.json +1 -1
- package/esm2015/lib/definitions.js.map +1 -1
- package/esm2015/lib/utility.js +18 -0
- package/esm2015/lib/utility.js.map +1 -1
- package/esm2015/lib/utility.metadata.json +1 -1
- package/esm2015/lib/utility.ngsummary.json +1 -1
- package/esm2015/public_api.js +1 -1
- package/esm2015/public_api.js.map +1 -1
- package/esm2015/public_api.metadata.json +1 -1
- package/esm2015/public_api.ngsummary.json +1 -1
- package/fesm2015/iotize-device-com-ble.cordova.js +19 -1
- package/fesm2015/iotize-device-com-ble.cordova.js.map +1 -1
- package/iotize-device-com-ble.cordova.metadata.json +1 -1
- package/lib/definitions.d.ts +2 -0
- package/lib/utility.d.ts +4 -0
- package/package.json +1 -1
- package/public_api.d.ts +1 -1
- package/src/android/build.gradle +27 -27
- package/src/android/src/ble/BLECom.java +10 -6
- package/src/android/src/ble/BLEComError.java +119 -119
- package/src/android/src/ble/BLEProtocol.java +874 -0
- package/src/android/src/ble/BLEScanner.java +283 -283
- package/src/android/src/ble/JSONBuilder.java +83 -83
- package/src/android/src/ble/PluginResponse.java +129 -129
- package/src/android/src/ble/TapUtility.java +26 -26
- package/src/android/src/ble/characteristics/AbstractLwM2MRequestCharacteristic.java +48 -0
- package/src/android/src/ble/characteristics/CharacteristicAdapter.java +221 -0
- package/src/android/src/ble/characteristics/CharacteristicAdapterCallbacks.java +21 -0
- package/src/android/src/ble/characteristics/LwM2M20BytesRequestCharacteristicAdapter.java +161 -0
- package/src/android/src/ble/characteristics/LwM2MMTURequestCharacteristicAdapter.java +134 -0
- package/src/android/src/ble/commands/BLECommand.java +18 -0
- package/src/android/src/ble/commands/CharacteristicOperationCompletedNotifier.java +38 -0
- package/src/android/src/ble/commands/CommandCompletedNotifier.java +10 -0
- package/src/android/src/ble/commands/DescriptorOperationCompletedNotifier.java +37 -0
- package/src/android/src/ble/commands/GattOperationCompletedNotifier.java +29 -0
- package/src/ios/BLECom.swift +54 -42
- package/src/ios/BLEManager.swift +41 -23
- package/src/ios/BLETapPeripheral.swift +5 -10
- package/src/ios/CBPeripheralConverter.swift +164 -139
- package/src/ios/Queue.swift +114 -108
- package/iotize-device-com-ble.cordova-3.6.0.tgz +0 -0
|
@@ -1,284 +1,284 @@
|
|
|
1
|
-
package com.iotize.plugin.cordova.ble;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import static android.os.Build.VERSION_CODES.LOLLIPOP;
|
|
5
|
-
|
|
6
|
-
import android.bluetooth.BluetoothAdapter;
|
|
7
|
-
import android.bluetooth.BluetoothDevice;
|
|
8
|
-
import android.bluetooth.le.BluetoothLeScanner;
|
|
9
|
-
import android.bluetooth.le.ScanCallback;
|
|
10
|
-
import android.bluetooth.le.ScanFilter;
|
|
11
|
-
import android.bluetooth.le.ScanRecord;
|
|
12
|
-
import android.bluetooth.le.ScanResult;
|
|
13
|
-
import android.bluetooth.le.ScanSettings;
|
|
14
|
-
import android.content.Context;
|
|
15
|
-
import android.content.pm.PackageManager;
|
|
16
|
-
import android.os.Build;
|
|
17
|
-
import android.util.Log;
|
|
18
|
-
|
|
19
|
-
import androidx.annotation.NonNull;
|
|
20
|
-
import androidx.annotation.RequiresApi;
|
|
21
|
-
|
|
22
|
-
import com.iotize.android.device.api.device.scanner.IDeviceScanner;
|
|
23
|
-
import com.iotize.android.device.api.device.scanner.IOnDeviceDiscovered;
|
|
24
|
-
|
|
25
|
-
import java.util.List;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
31
|
-
public class BLEScanner implements IDeviceScanner<BLEScanner.BLEScanData> {
|
|
32
|
-
|
|
33
|
-
private static final String TAG = "BLEScanner";
|
|
34
|
-
|
|
35
|
-
@NonNull
|
|
36
|
-
private final BluetoothAdapter mBluetoothAdapter;
|
|
37
|
-
@NonNull
|
|
38
|
-
private final Context mContext;
|
|
39
|
-
@NonNull
|
|
40
|
-
private final List<ScanFilter> filters;
|
|
41
|
-
|
|
42
|
-
// Device scan callback.
|
|
43
|
-
protected BLE_Callback mLeScanCallback;
|
|
44
|
-
protected BLE_Callback_API21 mLeScanCallback_API21;
|
|
45
|
-
private BluetoothLeScanner mBluetoothScanner;
|
|
46
|
-
private IOnDeviceDiscovered<BLEScanData> onDeviceDiscovered;
|
|
47
|
-
private boolean mRunning;
|
|
48
|
-
|
|
49
|
-
public BLEScanner(@NonNull Context context,
|
|
50
|
-
@NonNull BluetoothAdapter bluetoothAdapter,
|
|
51
|
-
@NonNull List<ScanFilter> filters) {
|
|
52
|
-
this.mBluetoothAdapter = bluetoothAdapter;
|
|
53
|
-
if (Build.VERSION.SDK_INT < LOLLIPOP)
|
|
54
|
-
mLeScanCallback = new BLE_Callback();
|
|
55
|
-
else {
|
|
56
|
-
mLeScanCallback_API21 = new BLE_Callback_API21();
|
|
57
|
-
}
|
|
58
|
-
this.mContext = context;
|
|
59
|
-
this.filters = filters;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public void setOnDeviceDiscoveredCallback(IOnDeviceDiscovered<BLEScanData> onDeviceDiscovered) {
|
|
63
|
-
this.onDeviceDiscovered = onDeviceDiscovered;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public void start() {
|
|
67
|
-
if (mRunning) {
|
|
68
|
-
Log.w(TAG, "Scan is already running... ignoring this call");
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
mRunning = true;
|
|
72
|
-
if (Build.VERSION.SDK_INT < LOLLIPOP) {
|
|
73
|
-
Log.i(TAG, "Starting Bluetooth scan...");
|
|
74
|
-
mBluetoothAdapter.startLeScan(mLeScanCallback);
|
|
75
|
-
} else {
|
|
76
|
-
Log.i(TAG, "Starting Bluetooth Low Energy scan...");
|
|
77
|
-
try {
|
|
78
|
-
ScanSettings.Builder buildSettings = new ScanSettings.Builder();
|
|
79
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
80
|
-
buildSettings.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
|
|
81
|
-
}
|
|
82
|
-
buildSettings.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
|
|
83
|
-
buildSettings.setReportDelay(0); // we want the result immediately
|
|
84
|
-
ScanSettings settings = buildSettings.build();
|
|
85
|
-
mBluetoothScanner = mBluetoothAdapter.getBluetoothLeScanner();
|
|
86
|
-
mBluetoothScanner.startScan(this.filters, settings, mLeScanCallback_API21);
|
|
87
|
-
} catch (Exception e) {
|
|
88
|
-
Log.e(TAG, "Cannot run bluetooth scan: " + e.getMessage(), e);
|
|
89
|
-
stop();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
95
|
-
public void stop() {
|
|
96
|
-
try {
|
|
97
|
-
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
|
|
98
|
-
if (mBluetoothScanner != null) {
|
|
99
|
-
mBluetoothScanner.stopScan(mLeScanCallback_API21);
|
|
100
|
-
}
|
|
101
|
-
mBluetoothAdapter.cancelDiscovery();
|
|
102
|
-
}
|
|
103
|
-
} catch (Throwable e) {
|
|
104
|
-
Log.e(TAG, "Error while stopping ble scan: " + e.getMessage(), e);
|
|
105
|
-
} finally {
|
|
106
|
-
mRunning = false;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
protected void onDeviceDiscovered(BLEScanData device) {
|
|
111
|
-
Log.v(TAG, "onDeviceDiscovered() " + device.getDevice().getName());
|
|
112
|
-
|
|
113
|
-
if (this.onDeviceDiscovered != null) {
|
|
114
|
-
this.onDeviceDiscovered.onDeviceDiscovered(device);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* @return true if device has bluetooth feature
|
|
120
|
-
*/
|
|
121
|
-
public boolean isAvailable() {
|
|
122
|
-
PackageManager pm = mContext.getPackageManager();
|
|
123
|
-
return pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @return true if device bluetooth is enabled
|
|
128
|
-
*/
|
|
129
|
-
public boolean isEnabled() {
|
|
130
|
-
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
135
|
-
class BLE_Callback implements BluetoothAdapter.LeScanCallback {
|
|
136
|
-
@Override
|
|
137
|
-
public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
|
|
138
|
-
Log.v(TAG, "Scan result: " + device + " RSSI: " + rssi);
|
|
139
|
-
onDeviceDiscovered(new BluetoothDeviceAdpaterBeforeLollipop(device, rssi, scanRecord));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
//YRT170410 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
145
|
-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
146
|
-
private class BLE_Callback_API21 extends ScanCallback {
|
|
147
|
-
@Override
|
|
148
|
-
public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
|
|
149
|
-
super.onScanResult(callbackType, result);
|
|
150
|
-
Log.v(TAG, "Scan result: " + result);
|
|
151
|
-
//if (callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
|
|
152
|
-
BluetoothDevice device = result.getDevice();
|
|
153
|
-
onDeviceDiscovered(new BluetoothDeviceAdpaterFromLollipop(result));
|
|
154
|
-
//}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
@Override
|
|
158
|
-
public void onScanFailed(int errorCode) {
|
|
159
|
-
Log.e(TAG, "Scan failed with error code: " + errorCode);
|
|
160
|
-
BLEScanner.this.onScanFailed(errorCode);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private void onScanFailed(int errorCode) {
|
|
165
|
-
if (this.onDeviceDiscovered != null) {
|
|
166
|
-
this.onDeviceDiscovered.onScanFailed(
|
|
167
|
-
new BleScanError(errorCode)
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
public interface BLEScanData {
|
|
174
|
-
|
|
175
|
-
int getRssi();
|
|
176
|
-
|
|
177
|
-
int getTxPower();
|
|
178
|
-
|
|
179
|
-
BluetoothDevice getDevice();
|
|
180
|
-
|
|
181
|
-
byte[] getScanRecordBytes();
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
186
|
-
public class BluetoothDeviceAdpaterFromLollipop implements BLEScanner.BLEScanData {
|
|
187
|
-
private final ScanResult result;
|
|
188
|
-
|
|
189
|
-
public BluetoothDeviceAdpaterFromLollipop(ScanResult result) {
|
|
190
|
-
this.result = result;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
@Override
|
|
194
|
-
public int getRssi() {
|
|
195
|
-
return this.result.getRssi();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
@Override
|
|
199
|
-
public int getTxPower() {
|
|
200
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
201
|
-
return this.result.getTxPower();
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
return 0;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
@Override
|
|
209
|
-
public BluetoothDevice getDevice() {
|
|
210
|
-
return this.result.getDevice();
|
|
211
|
-
}
|
|
212
|
-
@Override
|
|
213
|
-
public byte[] getScanRecordBytes() {
|
|
214
|
-
final ScanRecord scanRecord = this.result.getScanRecord();
|
|
215
|
-
if (scanRecord == null) {
|
|
216
|
-
return new byte[0];
|
|
217
|
-
}
|
|
218
|
-
return scanRecord.getBytes();
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
@Override
|
|
222
|
-
public boolean equals(Object obj) {
|
|
223
|
-
if (obj == null) {
|
|
224
|
-
return this.getDevice() == null;
|
|
225
|
-
}
|
|
226
|
-
if (!(obj instanceof BLEScanner.BLEScanData)) {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
return this.result.getDevice().equals(((BLEScanner.BLEScanData) obj).getDevice());
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
public class BluetoothDeviceAdpaterBeforeLollipop implements BLEScanner.BLEScanData {
|
|
234
|
-
|
|
235
|
-
private final BluetoothDevice device;
|
|
236
|
-
private final int rssi;
|
|
237
|
-
private final byte[] scanRecord;
|
|
238
|
-
|
|
239
|
-
public BluetoothDeviceAdpaterBeforeLollipop(BluetoothDevice device, int rssi, final byte[] scanRecord) {
|
|
240
|
-
this.device = device;
|
|
241
|
-
this.rssi = rssi;
|
|
242
|
-
this.scanRecord = scanRecord;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
@Override
|
|
246
|
-
public int getRssi() {
|
|
247
|
-
return this.rssi;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
@Override
|
|
251
|
-
public int getTxPower() {
|
|
252
|
-
return 0;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
@Override
|
|
256
|
-
public BluetoothDevice getDevice() {
|
|
257
|
-
return this.device;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
@Override
|
|
261
|
-
public byte[] getScanRecordBytes() {
|
|
262
|
-
return this.scanRecord;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
@Override
|
|
266
|
-
public boolean equals(Object obj) {
|
|
267
|
-
if (obj == null) {
|
|
268
|
-
return this.getDevice() == null;
|
|
269
|
-
}
|
|
270
|
-
if (!(obj instanceof BLEScanner.BLEScanData)) {
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
return this.device.equals(((BLEScanner.BLEScanData) obj).getDevice());
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
class BleScanError extends Exception {
|
|
278
|
-
public BleScanError(int errorCode) {
|
|
279
|
-
super("BLE scan failed (error code: " + errorCode + ")");
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
1
|
+
package com.iotize.plugin.cordova.ble;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import static android.os.Build.VERSION_CODES.LOLLIPOP;
|
|
5
|
+
|
|
6
|
+
import android.bluetooth.BluetoothAdapter;
|
|
7
|
+
import android.bluetooth.BluetoothDevice;
|
|
8
|
+
import android.bluetooth.le.BluetoothLeScanner;
|
|
9
|
+
import android.bluetooth.le.ScanCallback;
|
|
10
|
+
import android.bluetooth.le.ScanFilter;
|
|
11
|
+
import android.bluetooth.le.ScanRecord;
|
|
12
|
+
import android.bluetooth.le.ScanResult;
|
|
13
|
+
import android.bluetooth.le.ScanSettings;
|
|
14
|
+
import android.content.Context;
|
|
15
|
+
import android.content.pm.PackageManager;
|
|
16
|
+
import android.os.Build;
|
|
17
|
+
import android.util.Log;
|
|
18
|
+
|
|
19
|
+
import androidx.annotation.NonNull;
|
|
20
|
+
import androidx.annotation.RequiresApi;
|
|
21
|
+
|
|
22
|
+
import com.iotize.android.device.api.device.scanner.IDeviceScanner;
|
|
23
|
+
import com.iotize.android.device.api.device.scanner.IOnDeviceDiscovered;
|
|
24
|
+
|
|
25
|
+
import java.util.List;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
31
|
+
public class BLEScanner implements IDeviceScanner<BLEScanner.BLEScanData> {
|
|
32
|
+
|
|
33
|
+
private static final String TAG = "BLEScanner";
|
|
34
|
+
|
|
35
|
+
@NonNull
|
|
36
|
+
private final BluetoothAdapter mBluetoothAdapter;
|
|
37
|
+
@NonNull
|
|
38
|
+
private final Context mContext;
|
|
39
|
+
@NonNull
|
|
40
|
+
private final List<ScanFilter> filters;
|
|
41
|
+
|
|
42
|
+
// Device scan callback.
|
|
43
|
+
protected BLE_Callback mLeScanCallback;
|
|
44
|
+
protected BLE_Callback_API21 mLeScanCallback_API21;
|
|
45
|
+
private BluetoothLeScanner mBluetoothScanner;
|
|
46
|
+
private IOnDeviceDiscovered<BLEScanData> onDeviceDiscovered;
|
|
47
|
+
private boolean mRunning;
|
|
48
|
+
|
|
49
|
+
public BLEScanner(@NonNull Context context,
|
|
50
|
+
@NonNull BluetoothAdapter bluetoothAdapter,
|
|
51
|
+
@NonNull List<ScanFilter> filters) {
|
|
52
|
+
this.mBluetoothAdapter = bluetoothAdapter;
|
|
53
|
+
if (Build.VERSION.SDK_INT < LOLLIPOP)
|
|
54
|
+
mLeScanCallback = new BLE_Callback();
|
|
55
|
+
else {
|
|
56
|
+
mLeScanCallback_API21 = new BLE_Callback_API21();
|
|
57
|
+
}
|
|
58
|
+
this.mContext = context;
|
|
59
|
+
this.filters = filters;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public void setOnDeviceDiscoveredCallback(IOnDeviceDiscovered<BLEScanData> onDeviceDiscovered) {
|
|
63
|
+
this.onDeviceDiscovered = onDeviceDiscovered;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public void start() {
|
|
67
|
+
if (mRunning) {
|
|
68
|
+
Log.w(TAG, "Scan is already running... ignoring this call");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
mRunning = true;
|
|
72
|
+
if (Build.VERSION.SDK_INT < LOLLIPOP) {
|
|
73
|
+
Log.i(TAG, "Starting Bluetooth scan...");
|
|
74
|
+
mBluetoothAdapter.startLeScan(mLeScanCallback);
|
|
75
|
+
} else {
|
|
76
|
+
Log.i(TAG, "Starting Bluetooth Low Energy scan...");
|
|
77
|
+
try {
|
|
78
|
+
ScanSettings.Builder buildSettings = new ScanSettings.Builder();
|
|
79
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
80
|
+
buildSettings.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
|
|
81
|
+
}
|
|
82
|
+
buildSettings.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
|
|
83
|
+
buildSettings.setReportDelay(0); // we want the result immediately
|
|
84
|
+
ScanSettings settings = buildSettings.build();
|
|
85
|
+
mBluetoothScanner = mBluetoothAdapter.getBluetoothLeScanner();
|
|
86
|
+
mBluetoothScanner.startScan(this.filters, settings, mLeScanCallback_API21);
|
|
87
|
+
} catch (Exception e) {
|
|
88
|
+
Log.e(TAG, "Cannot run bluetooth scan: " + e.getMessage(), e);
|
|
89
|
+
stop();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
95
|
+
public void stop() {
|
|
96
|
+
try {
|
|
97
|
+
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
|
|
98
|
+
if (mBluetoothScanner != null) {
|
|
99
|
+
mBluetoothScanner.stopScan(mLeScanCallback_API21);
|
|
100
|
+
}
|
|
101
|
+
mBluetoothAdapter.cancelDiscovery();
|
|
102
|
+
}
|
|
103
|
+
} catch (Throwable e) {
|
|
104
|
+
Log.e(TAG, "Error while stopping ble scan: " + e.getMessage(), e);
|
|
105
|
+
} finally {
|
|
106
|
+
mRunning = false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
protected void onDeviceDiscovered(BLEScanData device) {
|
|
111
|
+
Log.v(TAG, "onDeviceDiscovered() " + device.getDevice().getName());
|
|
112
|
+
|
|
113
|
+
if (this.onDeviceDiscovered != null) {
|
|
114
|
+
this.onDeviceDiscovered.onDeviceDiscovered(device);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @return true if device has bluetooth feature
|
|
120
|
+
*/
|
|
121
|
+
public boolean isAvailable() {
|
|
122
|
+
PackageManager pm = mContext.getPackageManager();
|
|
123
|
+
return pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @return true if device bluetooth is enabled
|
|
128
|
+
*/
|
|
129
|
+
public boolean isEnabled() {
|
|
130
|
+
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
135
|
+
class BLE_Callback implements BluetoothAdapter.LeScanCallback {
|
|
136
|
+
@Override
|
|
137
|
+
public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
|
|
138
|
+
Log.v(TAG, "Scan result: " + device + " RSSI: " + rssi);
|
|
139
|
+
onDeviceDiscovered(new BluetoothDeviceAdpaterBeforeLollipop(device, rssi, scanRecord));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
//YRT170410 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
145
|
+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
146
|
+
private class BLE_Callback_API21 extends ScanCallback {
|
|
147
|
+
@Override
|
|
148
|
+
public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
|
|
149
|
+
super.onScanResult(callbackType, result);
|
|
150
|
+
Log.v(TAG, "Scan result: " + result);
|
|
151
|
+
//if (callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
|
|
152
|
+
BluetoothDevice device = result.getDevice();
|
|
153
|
+
onDeviceDiscovered(new BluetoothDeviceAdpaterFromLollipop(result));
|
|
154
|
+
//}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@Override
|
|
158
|
+
public void onScanFailed(int errorCode) {
|
|
159
|
+
Log.e(TAG, "Scan failed with error code: " + errorCode);
|
|
160
|
+
BLEScanner.this.onScanFailed(errorCode);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private void onScanFailed(int errorCode) {
|
|
165
|
+
if (this.onDeviceDiscovered != null) {
|
|
166
|
+
this.onDeviceDiscovered.onScanFailed(
|
|
167
|
+
new BleScanError(errorCode)
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
public interface BLEScanData {
|
|
174
|
+
|
|
175
|
+
int getRssi();
|
|
176
|
+
|
|
177
|
+
int getTxPower();
|
|
178
|
+
|
|
179
|
+
BluetoothDevice getDevice();
|
|
180
|
+
|
|
181
|
+
byte[] getScanRecordBytes();
|
|
182
|
+
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
186
|
+
public class BluetoothDeviceAdpaterFromLollipop implements BLEScanner.BLEScanData {
|
|
187
|
+
private final ScanResult result;
|
|
188
|
+
|
|
189
|
+
public BluetoothDeviceAdpaterFromLollipop(ScanResult result) {
|
|
190
|
+
this.result = result;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@Override
|
|
194
|
+
public int getRssi() {
|
|
195
|
+
return this.result.getRssi();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
@Override
|
|
199
|
+
public int getTxPower() {
|
|
200
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
201
|
+
return this.result.getTxPower();
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
return 0;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@Override
|
|
209
|
+
public BluetoothDevice getDevice() {
|
|
210
|
+
return this.result.getDevice();
|
|
211
|
+
}
|
|
212
|
+
@Override
|
|
213
|
+
public byte[] getScanRecordBytes() {
|
|
214
|
+
final ScanRecord scanRecord = this.result.getScanRecord();
|
|
215
|
+
if (scanRecord == null) {
|
|
216
|
+
return new byte[0];
|
|
217
|
+
}
|
|
218
|
+
return scanRecord.getBytes();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
@Override
|
|
222
|
+
public boolean equals(Object obj) {
|
|
223
|
+
if (obj == null) {
|
|
224
|
+
return this.getDevice() == null;
|
|
225
|
+
}
|
|
226
|
+
if (!(obj instanceof BLEScanner.BLEScanData)) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
return this.result.getDevice().equals(((BLEScanner.BLEScanData) obj).getDevice());
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
public class BluetoothDeviceAdpaterBeforeLollipop implements BLEScanner.BLEScanData {
|
|
234
|
+
|
|
235
|
+
private final BluetoothDevice device;
|
|
236
|
+
private final int rssi;
|
|
237
|
+
private final byte[] scanRecord;
|
|
238
|
+
|
|
239
|
+
public BluetoothDeviceAdpaterBeforeLollipop(BluetoothDevice device, int rssi, final byte[] scanRecord) {
|
|
240
|
+
this.device = device;
|
|
241
|
+
this.rssi = rssi;
|
|
242
|
+
this.scanRecord = scanRecord;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
@Override
|
|
246
|
+
public int getRssi() {
|
|
247
|
+
return this.rssi;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
@Override
|
|
251
|
+
public int getTxPower() {
|
|
252
|
+
return 0;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
@Override
|
|
256
|
+
public BluetoothDevice getDevice() {
|
|
257
|
+
return this.device;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
@Override
|
|
261
|
+
public byte[] getScanRecordBytes() {
|
|
262
|
+
return this.scanRecord;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
@Override
|
|
266
|
+
public boolean equals(Object obj) {
|
|
267
|
+
if (obj == null) {
|
|
268
|
+
return this.getDevice() == null;
|
|
269
|
+
}
|
|
270
|
+
if (!(obj instanceof BLEScanner.BLEScanData)) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
return this.device.equals(((BLEScanner.BLEScanData) obj).getDevice());
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
class BleScanError extends Exception {
|
|
278
|
+
public BleScanError(int errorCode) {
|
|
279
|
+
super("BLE scan failed (error code: " + errorCode + ")");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
|
|
284
284
|
}
|