Skip to content

Commit

Permalink
Removed example unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Mar 3, 2022
1 parent 6217f83 commit 327aadb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
16 changes: 9 additions & 7 deletions app/src/main/java/com/welie/blessedexample/BluetoothHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ internal class BluetoothHandler private constructor(context: Context) {
}

private fun startScanning() {
central.scanForPeripheralsWithServices(supportedServices) { peripheral, scanResult ->
Timber.i("Found peripheral '${peripheral.name}' with RSSI ${scanResult.rssi}")
central.stopScan()
connectPeripheral(peripheral)
}
central.scanForPeripheralsWithServices(supportedServices,
{ peripheral, scanResult ->
Timber.i("Found peripheral '${peripheral.name}' with RSSI ${scanResult.rssi}")
central.stopScan()
connectPeripheral(peripheral)
},
{ scanFailure -> Timber.e("scan failed with reason $scanFailure") })
}

private fun connectPeripheral(peripheral: BluetoothPeripheral) {
Expand All @@ -233,7 +235,7 @@ internal class BluetoothHandler private constructor(context: Context) {
companion object {
// UUIDs for the Blood Pressure service (BLP)
private val BLP_SERVICE_UUID: UUID = UUID.fromString("00001810-0000-1000-8000-00805f9b34fb")
private val BLP_MEASUREMENT_CHARACTERISTIC_UUID : UUID = UUID.fromString("00002A35-0000-1000-8000-00805f9b34fb")
private val BLP_MEASUREMENT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A35-0000-1000-8000-00805f9b34fb")

// UUIDs for the Health Thermometer service (HTS)
private val HTS_SERVICE_UUID = UUID.fromString("00001809-0000-1000-8000-00805f9b34fb")
Expand Down Expand Up @@ -309,7 +311,7 @@ internal class BluetoothHandler private constructor(context: Context) {
}

central.observeAdapterState { state ->
when(state) {
when (state) {
BluetoothAdapter.STATE_ON -> startScanning()
}
}
Expand Down
44 changes: 30 additions & 14 deletions blessed/src/main/java/com/welie/blessed/BluetoothCentralManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class BluetoothCentralManager(private val context: Context) {
private var disconnectRunnable: Runnable? = null
private val pinCodes: MutableMap<String, String> = ConcurrentHashMap()
private var currentResultCallback : ((BluetoothPeripheral, ScanResult) -> Unit)? = null
private var currentScanErrorCallback : ((ScanFailure) -> Unit)? = null
private var adapterStateCallback: (state: Int) -> Unit = {}

private val scanByNameCallback: ScanCallback = object : ScanCallback() {
Expand Down Expand Up @@ -119,9 +120,10 @@ class BluetoothCentralManager(private val context: Context) {
private fun sendScanFailed(scanFailure: ScanFailure) {
currentCallback = null
currentFilters = null
cancelTimeoutTimer()
scope.launch {
Logger.e(TAG, "scan failed with error code %d (%s)", scanFailure.value, scanFailure)
// bluetoothCentralManagerCallback.onScanFailed(scanFailure)
currentScanErrorCallback?.invoke(scanFailure)
}
}

Expand Down Expand Up @@ -247,10 +249,6 @@ class BluetoothCentralManager(private val context: Context) {

private fun startScan(filters: List<ScanFilter>, scanSettings: ScanSettings, scanCallback: ScanCallback) {
if (bleNotReady()) return
if (isScanning) {
Logger.e(TAG, "other scan still active, stopping scan")
stopScan()
}
if (bluetoothScanner == null) {
bluetoothScanner = bluetoothAdapter.bluetoothLeScanner
}
Expand All @@ -271,9 +269,11 @@ class BluetoothCentralManager(private val context: Context) {
* @param serviceUUIDs an array of service UUIDs
* @throws IllegalArgumentException if the array of service UUIDs is empty
*/
fun scanForPeripheralsWithServices(serviceUUIDs: Array<UUID>, resultCallback: (BluetoothPeripheral, ScanResult) -> Unit ) {
fun scanForPeripheralsWithServices(serviceUUIDs: Array<UUID>, resultCallback: (BluetoothPeripheral, ScanResult) -> Unit, scanError: (ScanFailure) -> Unit ) {
require(serviceUUIDs.isNotEmpty()) { "at least one service UUID must be supplied" }

if (isScanning) stopScan()

val filters: MutableList<ScanFilter> = ArrayList()
for (serviceUUID in serviceUUIDs) {
val filter = ScanFilter.Builder()
Expand All @@ -283,6 +283,7 @@ class BluetoothCentralManager(private val context: Context) {
}

currentResultCallback = resultCallback
currentScanErrorCallback = scanError
startScan(filters, scanSettings, defaultScanCallback)
}

Expand All @@ -295,9 +296,13 @@ class BluetoothCentralManager(private val context: Context) {
* @param peripheralNames array of partial peripheral names
* @throws IllegalArgumentException if the array of peripheral names is empty
*/
fun scanForPeripheralsWithNames(peripheralNames: Array<String>, resultCallback: (BluetoothPeripheral, ScanResult) -> Unit ) {
fun scanForPeripheralsWithNames(peripheralNames: Array<String>, resultCallback: (BluetoothPeripheral, ScanResult) -> Unit, scanError: (ScanFailure) -> Unit ) {
require(peripheralNames.isNotEmpty()) { "at least one peripheral name must be supplied" }

if (isScanning) stopScan()

currentResultCallback = resultCallback
currentScanErrorCallback = scanError

// Start the scanner with no filter because we'll do the filtering ourselves
scanPeripheralNames = peripheralNames
Expand All @@ -310,9 +315,13 @@ class BluetoothCentralManager(private val context: Context) {
* @param peripheralAddresses array of peripheral mac addresses to scan for
* @throws IllegalArgumentException if the array of addresses is empty
*/
fun scanForPeripheralsWithAddresses(peripheralAddresses: Array<String>, resultCallback: (BluetoothPeripheral, ScanResult) -> Unit ) {
fun scanForPeripheralsWithAddresses(peripheralAddresses: Array<String>, resultCallback: (BluetoothPeripheral, ScanResult) -> Unit, scanError: (ScanFailure) -> Unit ) {
require(peripheralAddresses.isNotEmpty()) { "at least one peripheral address must be supplied" }

if (isScanning) stopScan()

currentResultCallback = resultCallback
currentScanErrorCallback = scanError

val filters: MutableList<ScanFilter> = ArrayList()
for (address in peripheralAddresses) {
Expand All @@ -334,17 +343,24 @@ class BluetoothCentralManager(private val context: Context) {
* @param filters A list of ScanFilters
* @throws IllegalArgumentException if the list of filters is empty
*/
fun scanForPeripheralsUsingFilters(filters: List<ScanFilter>,resultCallback: (BluetoothPeripheral, ScanResult) -> Unit ) {
fun scanForPeripheralsUsingFilters(filters: List<ScanFilter>,resultCallback: (BluetoothPeripheral, ScanResult) -> Unit, scanError: (ScanFailure) -> Unit ) {
require(filters.isNotEmpty()) { "at least one scan filter must be supplied" }

if (isScanning) stopScan()

currentResultCallback = resultCallback
currentScanErrorCallback = scanError
startScan(filters, scanSettings, defaultScanCallback)
}

/**
* Scan for any peripheral that is advertising.
*/
fun scanForPeripherals(resultCallback: (BluetoothPeripheral, ScanResult) -> Unit ) {
fun scanForPeripherals(resultCallback: (BluetoothPeripheral, ScanResult) -> Unit, scanError: (ScanFailure) -> Unit ) {
if (isScanning) stopScan()

currentResultCallback = resultCallback
currentScanErrorCallback = scanError
startScan(emptyList(), scanSettings, defaultScanCallback)
}

Expand Down Expand Up @@ -397,7 +413,7 @@ class BluetoothCentralManager(private val context: Context) {
Logger.i(TAG, "scan stopped")
}
} else {
Logger.i(TAG, "no scan to stop because no scan is running")
Logger.d(TAG, "no scan to stop because no scan is running")
}
currentCallback = null
currentFilters = null
Expand Down Expand Up @@ -455,7 +471,7 @@ class BluetoothCentralManager(private val context: Context) {

scannedPeripherals.remove(peripheral.address)
unconnectedPeripherals[peripheral.address] = peripheral
currentCentralManagerCallback = resultCentralManagerCallback
// currentCentralManagerCallback = resultCentralManagerCallback
peripheral.connect()
}
}
Expand Down Expand Up @@ -524,7 +540,7 @@ class BluetoothCentralManager(private val context: Context) {
})
}

fun cancelConnection(peripheral: BluetoothPeripheral, resultCentralManagerCallback: BluetoothCentralManagerCallback) {
private fun cancelConnection(peripheral: BluetoothPeripheral, resultCentralManagerCallback: BluetoothCentralManagerCallback) {
// First check if we are doing a reconnection scan for this peripheral
val peripheralAddress = peripheral.address
if (reconnectPeripheralAddresses.contains(peripheralAddress)) {
Expand All @@ -543,7 +559,7 @@ class BluetoothCentralManager(private val context: Context) {

// Only cancel connections if it is an known peripheral
if (unconnectedPeripherals.containsKey(peripheralAddress) || connectedPeripherals.containsKey(peripheralAddress)) {
currentCentralManagerCallback = resultCentralManagerCallback
// currentCentralManagerCallback = resultCentralManagerCallback
peripheral.cancelConnection()
} else {
Logger.e(TAG, "cannot cancel connection to unknown peripheral %s", peripheralAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import android.bluetooth.le.ScanResult
/**
* Callbacks for the BluetoothCentralManager class
*/
abstract class BluetoothCentralManagerCallback {
internal abstract class BluetoothCentralManagerCallback {
/**
* Successfully connected with a peripheral.
*
Expand Down

0 comments on commit 327aadb

Please sign in to comment.