diff --git a/app/src/main/java/org/hydev/wearsync/ActivityScan.kt b/app/src/main/java/org/hydev/wearsync/ActivityScan.kt index ce94c5b..4cd9795 100644 --- a/app/src/main/java/org/hydev/wearsync/ActivityScan.kt +++ b/app/src/main/java/org/hydev/wearsync/ActivityScan.kt @@ -113,7 +113,9 @@ class ActivityScan : AppCompatActivity() { // Click scanned device binding.lvScanned.setOnItemClickListener { parent, view, position, id -> central.stopScan() - bluetoothHandler.connectPeripheral(scannedDevices[position]) + bluetoothHandler.connectPeripheral(scannedDevices[position]) { + view.snack("✅ Connected.") + } } // Format and show bounded device list @@ -129,15 +131,9 @@ class ActivityScan : AppCompatActivity() { view.snack("Connecting...") // Scan for the device with the MAC address - central.stopScan() - central.scanForPeripheralsWithAddresses(arrayOf(dev.address), { peripheral, scanResult -> - if (peripheral.address != dev.address) return@scanForPeripheralsWithAddresses - + bluetoothHandler.connectAddress(dev.address) { view.snack("✅ Connected.") - - central.stopScan() - bluetoothHandler.connectPeripheral(peripheral) - }, {}) + } } collectHeartRate(bluetoothHandler) diff --git a/app/src/main/java/org/hydev/wearsync/bles/BluetoothHandler.kt b/app/src/main/java/org/hydev/wearsync/bles/BluetoothHandler.kt index 5bb2ff7..ad4f42a 100644 --- a/app/src/main/java/org/hydev/wearsync/bles/BluetoothHandler.kt +++ b/app/src/main/java/org/hydev/wearsync/bles/BluetoothHandler.kt @@ -15,7 +15,7 @@ internal class BluetoothHandler private constructor(context: Context) { private var currentTimeCounter = 0 val heartRateChannel = Channel(UNLIMITED) - val bloodpressureChannel = Channel(UNLIMITED) + val bloodPressureChannel = Channel(UNLIMITED) val glucoseChannel = Channel(UNLIMITED) val pulseOxSpotChannel = Channel(UNLIMITED) val pulseOxContinuousChannel = Channel(UNLIMITED) @@ -171,7 +171,7 @@ internal class BluetoothHandler private constructor(context: Context) { peripheral.getCharacteristic(BLP_SERVICE_UUID, BLP_MEASUREMENT_CHARACTERISTIC_UUID)?.let { peripheral.observe(it) { value -> val measurement = BloodPressureMeasurement.fromBytes(value) - bloodpressureChannel.trySend(measurement) + bloodPressureChannel.trySend(measurement) Timber.d("%s", measurement) } } @@ -207,18 +207,20 @@ 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) - }, - { scanFailure -> Timber.e("scan failed with reason $scanFailure") }) + /** + * Scan and connect to a peripheral at a specific address + */ + fun connectAddress(address: String, callback: (BluetoothPeripheral) -> Unit = {}) { + central.stopScan() + central.scanForPeripheralsWithAddresses(arrayOf(address), { peripheral, scanResult -> + if (peripheral.address != address) return@scanForPeripheralsWithAddresses + + central.stopScan() + connectPeripheral(peripheral) { callback(peripheral) } + }, {}) } - fun connectPeripheral(peripheral: BluetoothPeripheral) { + fun connectPeripheral(peripheral: BluetoothPeripheral, callback: () -> Unit = {}) { peripheral.observeBondState { Timber.i("Bond state is $it") } @@ -226,6 +228,7 @@ internal class BluetoothHandler private constructor(context: Context) { scope.launch { try { central.connectPeripheral(peripheral) + callback() } catch (connectionFailed: ConnectionFailedException) { Timber.e("connection failed") } @@ -269,15 +272,12 @@ internal class BluetoothHandler private constructor(context: Context) { val GLUCOSE_SERVICE_UUID: UUID = UUID.fromString("00001808-0000-1000-8000-00805f9b34fb") val GLUCOSE_MEASUREMENT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A18-0000-1000-8000-00805f9b34fb") val GLUCOSE_RECORD_ACCESS_POINT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A52-0000-1000-8000-00805f9b34fb") - val GLUCOSE_MEASUREMENT_CONTEXT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A34-0000-1000-8000-00805f9b34fb") // Contour Glucose Service val CONTOUR_SERVICE_UUID: UUID = UUID.fromString("00000000-0002-11E2-9E96-0800200C9A66") private val CONTOUR_CLOCK = UUID.fromString("00001026-0002-11E2-9E96-0800200C9A66") private var instance: BluetoothHandler? = null - private val supportedServices = arrayOf(BLP_SERVICE_UUID, HTS_SERVICE_UUID, HRS_SERVICE_UUID, PLX_SERVICE_UUID, WSS_SERVICE_UUID, GLUCOSE_SERVICE_UUID) - @JvmStatic @Synchronized fun getInstance(context: Context): BluetoothHandler diff --git a/app/src/main/java/org/hydev/wearsync/bles/ScanException.kt b/app/src/main/java/org/hydev/wearsync/bles/ScanException.kt new file mode 100644 index 0000000..cab6ff4 --- /dev/null +++ b/app/src/main/java/org/hydev/wearsync/bles/ScanException.kt @@ -0,0 +1,7 @@ +package org.hydev.wearsync.bles + +import com.welie.blessed.ScanFailure + +class ScanException(val fail: ScanFailure) : Exception() +{ +} \ No newline at end of file