@@ -2,21 +2,25 @@ package com.juul.kable
22
33import android.bluetooth.BluetoothAdapter
44import android.bluetooth.le.ScanCallback
5+ import android.bluetooth.le.ScanFilter
56import android.bluetooth.le.ScanResult
7+ import android.bluetooth.le.ScanSettings
8+ import android.os.ParcelUuid
69import android.util.Log
10+ import com.benasher44.uuid.Uuid
711import kotlinx.coroutines.cancel
812import kotlinx.coroutines.channels.awaitClose
913import kotlinx.coroutines.channels.sendBlocking
1014import kotlinx.coroutines.flow.Flow
1115import kotlinx.coroutines.flow.callbackFlow
1216
1317public class ScanFailedException internal constructor(
14- public val errorCode : Int
18+ public val errorCode : Int ,
1519) : IllegalStateException(" Bluetooth scan failed with error code $errorCode " )
1620
17- public actual fun Scanner (): Scanner = AndroidScanner ()
21+ public actual fun Scanner (services : List < Uuid > ? ): Scanner = AndroidScanner (services )
1822
19- public class AndroidScanner internal constructor() : Scanner {
23+ public class AndroidScanner internal constructor(private val filterServices : List < Uuid > ? ) : Scanner {
2024
2125 private val bluetoothAdapter = BluetoothAdapter .getDefaultAdapter()
2226 ? : error(" Bluetooth not supported" )
@@ -47,7 +51,17 @@ public class AndroidScanner internal constructor() : Scanner {
4751 cancel(" Bluetooth scan failed" , ScanFailedException (errorCode))
4852 }
4953 }
50- bluetoothAdapter.bluetoothLeScanner.startScan(callback)
54+
55+ val scanFilter =
56+ filterServices
57+ ?.map { ScanFilter .Builder ().setServiceUuid(ParcelUuid (it)).build() }
58+ ?.toList()
59+ bluetoothAdapter.bluetoothLeScanner.startScan(
60+ scanFilter,
61+ ScanSettings .Builder ().build(),
62+ callback,
63+ )
64+
5165 awaitClose {
5266 bluetoothAdapter.bluetoothLeScanner.stopScan(callback)
5367 }
0 commit comments