@onekeyfe/react-native-ble-utils 0.1.5 → 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.
@@ -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
  }
@@ -52,7 +52,7 @@ class BleUtils {
52
52
 
53
53
  /**
54
54
  *
55
- * @param serviceUUIDs [optional] not used on android, optional on ios.
55
+ * @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
56
56
  * @returns
57
57
  */
58
58
  getConnectedPeripherals(serviceUUIDs = []) {
@@ -48,7 +48,7 @@ class BleUtils {
48
48
 
49
49
  /**
50
50
  *
51
- * @param serviceUUIDs [optional] not used on android, optional on ios.
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] not used on android, optional on ios.
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[]>;
@@ -32,6 +32,7 @@ export interface Peripheral {
32
32
  name?: string;
33
33
  advertising: AdvertisingData;
34
34
  bondState: BondState;
35
+ serviceUUIDs?: string[];
35
36
  }
36
37
  export interface BondState {
37
38
  state: string;
@@ -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;CACtB;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"}
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] not used on android, optional on ios.
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[]>;
@@ -32,6 +32,7 @@ export interface Peripheral {
32
32
  name?: string;
33
33
  advertising: AdvertisingData;
34
34
  bondState: BondState;
35
+ serviceUUIDs?: string[];
35
36
  }
36
37
  export interface BondState {
37
38
  state: string;
@@ -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;CACtB;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"}
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-ble-utils",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "ble uilts",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./dist/commonjs/index.js",
package/src/index.ts CHANGED
@@ -57,7 +57,7 @@ class BleUtils {
57
57
 
58
58
  /**
59
59
  *
60
- * @param serviceUUIDs [optional] not used on android, optional on ios.
60
+ * @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
61
61
  * @returns
62
62
  */
63
63
  getConnectedPeripherals(serviceUUIDs: string[] = []) {
package/src/type.ts CHANGED
@@ -33,6 +33,7 @@ export interface Peripheral {
33
33
  name?: string;
34
34
  advertising: AdvertisingData;
35
35
  bondState: BondState;
36
+ serviceUUIDs?: string[];
36
37
  }
37
38
 
38
39
  export interface BondState {