Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions android/src/main/java/so/onekey/lib/ble/utils/BleUtilsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,36 @@ class BleUtilsModule(private val reactContext: ReactApplicationContext) :
return
}

val wantedServiceUuids = HashSet<String>()
if (serviceUUIDs != null) {
for (i in 0 until serviceUUIDs.size()) {
serviceUUIDs.getString(i)?.let { wantedServiceUuids.add(it.lowercase()) }
}
}

val peripherals: List<BluetoothDevice> =
getBluetoothManager()?.getConnectedDevices(GATT) ?: emptyList()
for (entry in peripherals) {
if (wantedServiceUuids.isNotEmpty() && !deviceExposesService(entry, wantedServiceUuids)) {
continue
}
val peripheral = Peripheral(entry)
val jsonBundle: WritableMap = peripheral.asWritableMap()
map.pushMap(jsonBundle)
}
callback.invoke(null, map)
}

@SuppressLint("MissingPermission")
private fun deviceExposesService(
device: BluetoothDevice,
wantedServiceUuids: Set<String>
): Boolean {
val uuids = device.uuids
if (uuids.isNullOrEmpty()) return true
return uuids.any { wantedServiceUuids.contains(it.uuid.toString().lowercase()) }
}

private class MyBroadcastReceiver(private val module: BleUtilsModule) : BroadcastReceiver() {
@SuppressLint("MissingPermission")
override fun onReceive(context: Context, intent: Intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Peripheral(
advertising.putBoolean("isConnectable", true)

map.putMap("advertising", advertising)

val serviceUUIDs = Arguments.createArray()
device.uuids?.forEach { serviceUUIDs.pushString(it.uuid.toString()) }
map.putArray("serviceUUIDs", serviceUUIDs)
} catch (e: Exception) { // this shouldn't happen
Log.e("BleUtils", "Unexpected error on asWritableMap", e)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/react-native-ble-utils",
"version": "0.1.5",
"version": "0.1.6",
"description": "ble uilts",
"source": "./src/index.tsx",
"main": "./dist/commonjs/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class BleUtils {

/**
*
* @param serviceUUIDs [optional] not used on android, optional on ios.
* @param serviceUUIDs [optional] filter by service UUID (android + ios); empty = no filter.
* @returns
*/
getConnectedPeripherals(serviceUUIDs: string[] = []) {
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface Peripheral {
name?: string;
advertising: AdvertisingData;
bondState: BondState;
serviceUUIDs?: string[];
}

export interface BondState {
Expand Down
Loading