From d35643524eb46de8efe4c7d53e8f34831d828700 Mon Sep 17 00:00:00 2001 From: Gilles Boccon-Gibod Date: Fri, 8 Dec 2023 18:46:25 -0800 Subject: [PATCH] allow specifying the address type --- .../github/google/bumble/btbench/L2capClient.kt | 16 +++++++++++++++- .../com/github/google/bumble/btbench/Model.kt | 5 +++-- .../github/google/bumble/btbench/RfcommClient.kt | 3 ++- .../github/google/bumble/btbench/SocketClient.kt | 2 +- .../github/google/bumble/btbench/SocketServer.kt | 2 +- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/L2capClient.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/L2capClient.kt index 874bc26b..228a741e 100644 --- a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/L2capClient.kt +++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/L2capClient.kt @@ -21,6 +21,7 @@ import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCallback import android.bluetooth.BluetoothProfile import android.content.Context +import android.os.Build import java.util.logging.Logger private val Log = Logger.getLogger("btbench.l2cap-client") @@ -33,7 +34,20 @@ class L2capClient( @SuppressLint("MissingPermission") fun run() { viewModel.running = true - val remoteDevice = bluetoothAdapter.getRemoteDevice(viewModel.peerBluetoothAddress) + val addressIsPublic = viewModel.peerBluetoothAddress.endsWith("/P") + val address = viewModel.peerBluetoothAddress.take(17) + val remoteDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + bluetoothAdapter.getRemoteLeDevice( + address, + if (addressIsPublic) { + BluetoothDevice.ADDRESS_TYPE_PUBLIC + } else { + BluetoothDevice.ADDRESS_TYPE_RANDOM + } + ) + } else { + bluetoothAdapter.getRemoteDevice(address) + } val gatt = remoteDevice.connectGatt( context, diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Model.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Model.kt index b709be32..35ee8da5 100644 --- a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Model.kt +++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/Model.kt @@ -68,11 +68,12 @@ class AppViewModel : ViewModel() { } fun updatePeerBluetoothAddress(peerBluetoothAddress: String) { - this.peerBluetoothAddress = peerBluetoothAddress + val address = peerBluetoothAddress.uppercase() + this.peerBluetoothAddress = address // Save the address to the preferences with(preferences!!.edit()) { - putString(PEER_BLUETOOTH_ADDRESS_PREF_KEY, peerBluetoothAddress) + putString(PEER_BLUETOOTH_ADDRESS_PREF_KEY, address) apply() } } diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/RfcommClient.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/RfcommClient.kt index 644a5bda..e976c429 100644 --- a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/RfcommClient.kt +++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/RfcommClient.kt @@ -25,7 +25,8 @@ private val Log = Logger.getLogger("btbench.rfcomm-client") class RfcommClient(private val viewModel: AppViewModel, val bluetoothAdapter: BluetoothAdapter) { @SuppressLint("MissingPermission") fun run() { - val remoteDevice = bluetoothAdapter.getRemoteDevice(viewModel.peerBluetoothAddress) + val address = viewModel.peerBluetoothAddress.take(17) + val remoteDevice = bluetoothAdapter.getRemoteDevice(address) val socket = remoteDevice.createInsecureRfcommSocketToServiceRecord( DEFAULT_RFCOMM_UUID ) diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketClient.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketClient.kt index 28c53542..bd5b7f4a 100644 --- a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketClient.kt +++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketClient.kt @@ -22,7 +22,7 @@ import kotlin.concurrent.thread private val Log = Logger.getLogger("btbench.socket-client") -private const val DEFAULT_STARTUP_DELAY = 1000 +private const val DEFAULT_STARTUP_DELAY = 3000 class SocketClient(private val viewModel: AppViewModel, private val socket: BluetoothSocket) { @SuppressLint("MissingPermission") diff --git a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketServer.kt b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketServer.kt index e461617d..e83a47f2 100644 --- a/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketServer.kt +++ b/extras/android/BtBench/app/src/main/java/com/github/google/bumble/btbench/SocketServer.kt @@ -45,7 +45,7 @@ class SocketServer(private val viewModel: AppViewModel, private val serverSocket cleanup() return@thread } - Log.info("got connection") + Log.info("got connection from ${socket.remoteDevice.address}") onConnected() viewModel.aborter = {