RobolectricBleBonding
Reusable Robolectric helpers for driving Android Bluetooth bonding logic in androidHostTest source sets.
These let a unit test exercise the real AndroidBluetoothRepository.bond() (and any future BLE bonding code) without an emulator and without a production seam. They rely on two Robolectric behaviors (verified against Robolectric 4.16.x):
android.bluetooth.BluetoothAdapter.getRemoteDevice caches the returned BluetoothDevice by address in a static map, so the shadow configured here is the same instance production code reads when it calls
getRemoteDevice(mac)internally.ShadowBluetoothDevice.createBond calls
checkForBluetoothConnectPermission()first, so tests must call grantBluetoothConnectPermission orcreateBond()throws SecurityException instead of returning a value.
Isolation note: because the device cache is static and survives across tests in the same JVM, give each test a distinct MAC so bond-state cannot bleed between tests.
Properties
Functions
The (address-cached) ShadowBluetoothDevice for mac — the same instance production's getRemoteDevice sees.
Grant the runtime permissions ShadowBluetoothDevice.createBond checks, so it returns instead of throwing.
Broadcast ACTION_BOND_STATE_CHANGED for mac and pump the main looper so a runtime-registered android.content.BroadcastReceiver (e.g. the one bond() registers) is delivered synchronously.