[O] Encapsulate connectAddress
This commit is contained in:
@@ -113,7 +113,9 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
// Click scanned device
|
// Click scanned device
|
||||||
binding.lvScanned.setOnItemClickListener { parent, view, position, id ->
|
binding.lvScanned.setOnItemClickListener { parent, view, position, id ->
|
||||||
central.stopScan()
|
central.stopScan()
|
||||||
bluetoothHandler.connectPeripheral(scannedDevices[position])
|
bluetoothHandler.connectPeripheral(scannedDevices[position]) {
|
||||||
|
view.snack("✅ Connected.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format and show bounded device list
|
// Format and show bounded device list
|
||||||
@@ -129,15 +131,9 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
view.snack("Connecting...")
|
view.snack("Connecting...")
|
||||||
|
|
||||||
// Scan for the device with the MAC address
|
// Scan for the device with the MAC address
|
||||||
central.stopScan()
|
bluetoothHandler.connectAddress(dev.address) {
|
||||||
central.scanForPeripheralsWithAddresses(arrayOf(dev.address), { peripheral, scanResult ->
|
|
||||||
if (peripheral.address != dev.address) return@scanForPeripheralsWithAddresses
|
|
||||||
|
|
||||||
view.snack("✅ Connected.")
|
view.snack("✅ Connected.")
|
||||||
|
}
|
||||||
central.stopScan()
|
|
||||||
bluetoothHandler.connectPeripheral(peripheral)
|
|
||||||
}, {})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
collectHeartRate(bluetoothHandler)
|
collectHeartRate(bluetoothHandler)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ internal class BluetoothHandler private constructor(context: Context) {
|
|||||||
|
|
||||||
private var currentTimeCounter = 0
|
private var currentTimeCounter = 0
|
||||||
val heartRateChannel = Channel<HeartRateMeasurement>(UNLIMITED)
|
val heartRateChannel = Channel<HeartRateMeasurement>(UNLIMITED)
|
||||||
val bloodpressureChannel = Channel<BloodPressureMeasurement>(UNLIMITED)
|
val bloodPressureChannel = Channel<BloodPressureMeasurement>(UNLIMITED)
|
||||||
val glucoseChannel = Channel<GlucoseMeasurement>(UNLIMITED)
|
val glucoseChannel = Channel<GlucoseMeasurement>(UNLIMITED)
|
||||||
val pulseOxSpotChannel = Channel<PulseOximeterSpotMeasurement>(UNLIMITED)
|
val pulseOxSpotChannel = Channel<PulseOximeterSpotMeasurement>(UNLIMITED)
|
||||||
val pulseOxContinuousChannel = Channel<PulseOximeterContinuousMeasurement>(UNLIMITED)
|
val pulseOxContinuousChannel = Channel<PulseOximeterContinuousMeasurement>(UNLIMITED)
|
||||||
@@ -171,7 +171,7 @@ internal class BluetoothHandler private constructor(context: Context) {
|
|||||||
peripheral.getCharacteristic(BLP_SERVICE_UUID, BLP_MEASUREMENT_CHARACTERISTIC_UUID)?.let {
|
peripheral.getCharacteristic(BLP_SERVICE_UUID, BLP_MEASUREMENT_CHARACTERISTIC_UUID)?.let {
|
||||||
peripheral.observe(it) { value ->
|
peripheral.observe(it) { value ->
|
||||||
val measurement = BloodPressureMeasurement.fromBytes(value)
|
val measurement = BloodPressureMeasurement.fromBytes(value)
|
||||||
bloodpressureChannel.trySend(measurement)
|
bloodPressureChannel.trySend(measurement)
|
||||||
Timber.d("%s", measurement)
|
Timber.d("%s", measurement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,18 +207,20 @@ internal class BluetoothHandler private constructor(context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startScanning() {
|
/**
|
||||||
central.scanForPeripheralsWithServices(
|
* Scan and connect to a peripheral at a specific address
|
||||||
supportedServices,
|
*/
|
||||||
{ peripheral, scanResult ->
|
fun connectAddress(address: String, callback: (BluetoothPeripheral) -> Unit = {}) {
|
||||||
Timber.i("Found peripheral '${peripheral.name}' with RSSI ${scanResult.rssi}")
|
central.stopScan()
|
||||||
central.stopScan()
|
central.scanForPeripheralsWithAddresses(arrayOf(address), { peripheral, scanResult ->
|
||||||
connectPeripheral(peripheral)
|
if (peripheral.address != address) return@scanForPeripheralsWithAddresses
|
||||||
},
|
|
||||||
{ scanFailure -> Timber.e("scan failed with reason $scanFailure") })
|
central.stopScan()
|
||||||
|
connectPeripheral(peripheral) { callback(peripheral) }
|
||||||
|
}, {})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun connectPeripheral(peripheral: BluetoothPeripheral) {
|
fun connectPeripheral(peripheral: BluetoothPeripheral, callback: () -> Unit = {}) {
|
||||||
peripheral.observeBondState {
|
peripheral.observeBondState {
|
||||||
Timber.i("Bond state is $it")
|
Timber.i("Bond state is $it")
|
||||||
}
|
}
|
||||||
@@ -226,6 +228,7 @@ internal class BluetoothHandler private constructor(context: Context) {
|
|||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
try {
|
||||||
central.connectPeripheral(peripheral)
|
central.connectPeripheral(peripheral)
|
||||||
|
callback()
|
||||||
} catch (connectionFailed: ConnectionFailedException) {
|
} catch (connectionFailed: ConnectionFailedException) {
|
||||||
Timber.e("connection failed")
|
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_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_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_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
|
// Contour Glucose Service
|
||||||
val CONTOUR_SERVICE_UUID: UUID = UUID.fromString("00000000-0002-11E2-9E96-0800200C9A66")
|
val CONTOUR_SERVICE_UUID: UUID = UUID.fromString("00000000-0002-11E2-9E96-0800200C9A66")
|
||||||
private val CONTOUR_CLOCK = UUID.fromString("00001026-0002-11E2-9E96-0800200C9A66")
|
private val CONTOUR_CLOCK = UUID.fromString("00001026-0002-11E2-9E96-0800200C9A66")
|
||||||
private var instance: BluetoothHandler? = null
|
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
|
@JvmStatic
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun getInstance(context: Context): BluetoothHandler
|
fun getInstance(context: Context): BluetoothHandler
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.hydev.wearsync.bles
|
||||||
|
|
||||||
|
import com.welie.blessed.ScanFailure
|
||||||
|
|
||||||
|
class ScanException(val fail: ScanFailure) : Exception()
|
||||||
|
{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user