@onekeyfe/react-native-ble-utils 0.1.4 → 0.1.6
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/android/src/main/java/so/onekey/lib/ble/utils/BleUtilsModule.kt +20 -0
- package/android/src/main/java/so/onekey/lib/ble/utils/data/Peripheral.kt +4 -0
- package/dist/commonjs/index.js +1 -1
- package/dist/module/index.js +1 -1
- package/dist/typescript/commonjs/src/index.d.ts +1 -1
- package/dist/typescript/commonjs/src/type.d.ts +1 -0
- package/dist/typescript/commonjs/src/type.d.ts.map +1 -1
- package/dist/typescript/module/src/index.d.ts +1 -1
- package/dist/typescript/module/src/type.d.ts +1 -0
- package/dist/typescript/module/src/type.d.ts.map +1 -1
- package/ios/BleUtils.swift +15 -9
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/type.ts +1 -0
|
@@ -192,9 +192,19 @@ class BleUtilsModule(private val reactContext: ReactApplicationContext) :
|
|
|
192
192
|
return
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
val wantedServiceUuids = HashSet<String>()
|
|
196
|
+
if (serviceUUIDs != null) {
|
|
197
|
+
for (i in 0 until serviceUUIDs.size()) {
|
|
198
|
+
serviceUUIDs.getString(i)?.let { wantedServiceUuids.add(it.lowercase()) }
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
195
202
|
val peripherals: List<BluetoothDevice> =
|
|
196
203
|
getBluetoothManager()?.getConnectedDevices(GATT) ?: emptyList()
|
|
197
204
|
for (entry in peripherals) {
|
|
205
|
+
if (wantedServiceUuids.isNotEmpty() && !deviceExposesService(entry, wantedServiceUuids)) {
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
198
208
|
val peripheral = Peripheral(entry)
|
|
199
209
|
val jsonBundle: WritableMap = peripheral.asWritableMap()
|
|
200
210
|
map.pushMap(jsonBundle)
|
|
@@ -202,6 +212,16 @@ class BleUtilsModule(private val reactContext: ReactApplicationContext) :
|
|
|
202
212
|
callback.invoke(null, map)
|
|
203
213
|
}
|
|
204
214
|
|
|
215
|
+
@SuppressLint("MissingPermission")
|
|
216
|
+
private fun deviceExposesService(
|
|
217
|
+
device: BluetoothDevice,
|
|
218
|
+
wantedServiceUuids: Set<String>
|
|
219
|
+
): Boolean {
|
|
220
|
+
val uuids = device.uuids
|
|
221
|
+
if (uuids.isNullOrEmpty()) return true
|
|
222
|
+
return uuids.any { wantedServiceUuids.contains(it.uuid.toString().lowercase()) }
|
|
223
|
+
}
|
|
224
|
+
|
|
205
225
|
private class MyBroadcastReceiver(private val module: BleUtilsModule) : BroadcastReceiver() {
|
|
206
226
|
@SuppressLint("MissingPermission")
|
|
207
227
|
override fun onReceive(context: Context, intent: Intent) {
|
|
@@ -26,6 +26,10 @@ class Peripheral(
|
|
|
26
26
|
advertising.putBoolean("isConnectable", true)
|
|
27
27
|
|
|
28
28
|
map.putMap("advertising", advertising)
|
|
29
|
+
|
|
30
|
+
val serviceUUIDs = Arguments.createArray()
|
|
31
|
+
device.uuids?.forEach { serviceUUIDs.pushString(it.uuid.toString()) }
|
|
32
|
+
map.putArray("serviceUUIDs", serviceUUIDs)
|
|
29
33
|
} catch (e: Exception) { // this shouldn't happen
|
|
30
34
|
Log.e("BleUtils", "Unexpected error on asWritableMap", e)
|
|
31
35
|
}
|
package/dist/commonjs/index.js
CHANGED
|
@@ -52,7 +52,7 @@ class BleUtils {
|
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
54
|
*
|
|
55
|
-
* @param serviceUUIDs [optional]
|
|
55
|
+
* @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
|
|
56
56
|
* @returns
|
|
57
57
|
*/
|
|
58
58
|
getConnectedPeripherals(serviceUUIDs = []) {
|
package/dist/module/index.js
CHANGED
|
@@ -48,7 +48,7 @@ class BleUtils {
|
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
*
|
|
51
|
-
* @param serviceUUIDs [optional]
|
|
51
|
+
* @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
|
|
52
52
|
* @returns
|
|
53
53
|
*/
|
|
54
54
|
getConnectedPeripherals(serviceUUIDs = []) {
|
|
@@ -16,7 +16,7 @@ declare class BleUtils {
|
|
|
16
16
|
pairDevice(macAddress: string): Promise<PairDeviceResult>;
|
|
17
17
|
/**
|
|
18
18
|
*
|
|
19
|
-
* @param serviceUUIDs [optional]
|
|
19
|
+
* @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
|
|
20
20
|
* @returns
|
|
21
21
|
*/
|
|
22
22
|
getConnectedPeripherals(serviceUUIDs?: string[]): Promise<Peripheral[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../../src/type.ts"],"names":[],"mappings":"AAAA;;;KAGK;AACL,oBAAY,QAAQ;IAClB;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB,WAAW,gBAAgB;IAC3B;;OAEG;IACH,YAAY,iBAAiB;IAC7B,EAAE,OAAO;IACT,GAAG,QAAQ;IACX;;OAEG;IACH,SAAS,eAAe;IACxB;;OAEG;IACH,UAAU,gBAAgB;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../../src/type.ts"],"names":[],"mappings":"AAAA;;;KAGK;AACL,oBAAY,QAAQ;IAClB;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB,WAAW,gBAAgB;IAC3B;;OAEG;IACH,YAAY,iBAAiB;IAC7B,EAAE,OAAO;IACT,GAAG,QAAQ;IACX;;OAEG;IACH,SAAS,eAAe;IACxB;;OAEG;IACH,UAAU,gBAAgB;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
|
|
@@ -16,7 +16,7 @@ declare class BleUtils {
|
|
|
16
16
|
pairDevice(macAddress: string): Promise<PairDeviceResult>;
|
|
17
17
|
/**
|
|
18
18
|
*
|
|
19
|
-
* @param serviceUUIDs [optional]
|
|
19
|
+
* @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
|
|
20
20
|
* @returns
|
|
21
21
|
*/
|
|
22
22
|
getConnectedPeripherals(serviceUUIDs?: string[]): Promise<Peripheral[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../../src/type.ts"],"names":[],"mappings":"AAAA;;;KAGK;AACL,oBAAY,QAAQ;IAClB;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB,WAAW,gBAAgB;IAC3B;;OAEG;IACH,YAAY,iBAAiB;IAC7B,EAAE,OAAO;IACT,GAAG,QAAQ;IACX;;OAEG;IACH,SAAS,eAAe;IACxB;;OAEG;IACH,UAAU,gBAAgB;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../../src/type.ts"],"names":[],"mappings":"AAAA;;;KAGK;AACL,oBAAY,QAAQ;IAClB;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB,WAAW,gBAAgB;IAC3B;;OAEG;IACH,YAAY,iBAAiB;IAC7B,EAAE,OAAO;IACT,GAAG,QAAQ;IACX;;OAEG;IACH,SAAS,eAAe;IACxB;;OAEG;IACH,UAAU,gBAAgB;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
|
package/ios/BleUtils.swift
CHANGED
|
@@ -14,7 +14,14 @@ class BleUtilsModule: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
|
|
|
14
14
|
|
|
15
15
|
override init() {
|
|
16
16
|
super.init()
|
|
17
|
-
|
|
17
|
+
// Lazy CBCentralManager: avoid triggering iOS BT dialog on module init.
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private func ensureManager() -> CBCentralManager {
|
|
21
|
+
if let m = manager { return m }
|
|
22
|
+
let m = CBCentralManager(delegate: self, queue: nil)
|
|
23
|
+
manager = m
|
|
24
|
+
return m
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
@@ -28,13 +35,12 @@ class BleUtilsModule: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
|
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
@objc public func checkState(_ callback: @escaping RCTResponseSenderBlock) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
+
let manager = ensureManager()
|
|
39
|
+
if manager.state == .unknown {
|
|
40
|
+
stateChangedCallbacks.append(callback)
|
|
41
|
+
} else {
|
|
42
|
+
let stateName = Helper.centralManagerStateToString(manager.state)
|
|
43
|
+
callback([stateName])
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
|
|
@@ -49,7 +55,7 @@ class BleUtilsModule: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
|
|
|
49
55
|
|
|
50
56
|
var connectedPeripherals: [Peripheral] = []
|
|
51
57
|
|
|
52
|
-
let connectedCBPeripherals: [CBPeripheral] =
|
|
58
|
+
let connectedCBPeripherals: [CBPeripheral] = ensureManager().retrieveConnectedPeripherals(withServices: serviceUUIDs)
|
|
53
59
|
|
|
54
60
|
serialQueue.sync {
|
|
55
61
|
for ph in connectedCBPeripherals {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -57,7 +57,7 @@ class BleUtils {
|
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
*
|
|
60
|
-
* @param serviceUUIDs [optional]
|
|
60
|
+
* @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
|
|
61
61
|
* @returns
|
|
62
62
|
*/
|
|
63
63
|
getConnectedPeripherals(serviceUUIDs: string[] = []) {
|