BtBench: Scan
diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/MainActivity.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/MainActivity.kt
index 6081837..dea3e3c 100644
--- a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/MainActivity.kt
+++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/MainActivity.kt
@@ -142,7 +142,7 @@
::runRfcommClient,
::runRfcommServer,
::runL2capClient,
- ::runL2capServer
+ ::runL2capServer,
)
}
@@ -166,6 +166,8 @@
"rfcomm-server" -> runRfcommServer()
"l2cap-client" -> runL2capClient()
"l2cap-server" -> runL2capServer()
+ "scan-start" -> runScan(true)
+ "stop-start" -> runScan(false)
}
}
}
@@ -190,6 +192,11 @@
l2capServer?.run()
}
+ private fun runScan(startScan: Boolean) {
+ val scan = bluetoothAdapter?.let { Scan(it) }
+ scan?.run(startScan)
+ }
+
@SuppressLint("MissingPermission")
fun becomeDiscoverable() {
val discoverableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
@@ -206,7 +213,7 @@
runRfcommClient: () -> Unit,
runRfcommServer: () -> Unit,
runL2capClient: () -> Unit,
- runL2capServer: () -> Unit
+ runL2capServer: () -> Unit,
) {
BTBenchTheme {
val scrollState = rememberScrollState()
diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Scan.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Scan.kt
new file mode 100644
index 0000000..7cb8e7a
--- /dev/null
+++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Scan.kt
@@ -0,0 +1,38 @@
+package com.github.google.bumble.btbench
+
+import android.annotation.SuppressLint
+import android.bluetooth.BluetoothAdapter
+import android.bluetooth.BluetoothDevice
+import android.bluetooth.le.ScanCallback
+import android.bluetooth.le.ScanResult
+import java.util.logging.Logger
+
+private val Log = Logger.getLogger("btbench.scan")
+
+class Scan(val bluetoothAdapter: BluetoothAdapter) {
+ @SuppressLint("MissingPermission")
+ fun run(startScan: Boolean) {
+ var bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
+
+ val scanCallback = object : ScanCallback() {
+ override fun onScanResult(callbackType: Int, result: ScanResult?) {
+ super.onScanResult(callbackType, result)
+ val device: BluetoothDevice? = result?.device
+ val deviceName = device?.name ?: "Unknown"
+ val deviceAddress = device?.address ?: "Unknown"
+ Log.info("Device found: $deviceName ($deviceAddress)")
+ }
+
+ override fun onScanFailed(errorCode: Int) {
+ // Handle scan failure
+ Log.warning("Scan failed with error code: $errorCode")
+ }
+ }
+
+ if (startScan) {
+ bluetoothLeScanner?.startScan(scanCallback)
+ } else {
+ bluetoothLeScanner?.stopScan(scanCallback)
+ }
+ }
+}
\ No newline at end of file