[O] Optimize bluetooth handler initialization
This commit is contained in:
@@ -10,6 +10,7 @@ import com.welie.blessed.BluetoothPeripheral
|
|||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.consumeAsFlow
|
import kotlinx.coroutines.flow.consumeAsFlow
|
||||||
import org.hydev.wearsync.bles.BluetoothHandler
|
import org.hydev.wearsync.bles.BluetoothHandler
|
||||||
|
import org.hydev.wearsync.bles.BluetoothHandler.Companion.ble
|
||||||
import org.hydev.wearsync.bles.ObservationUnit
|
import org.hydev.wearsync.bles.ObservationUnit
|
||||||
import org.hydev.wearsync.databinding.ActivityScanBinding
|
import org.hydev.wearsync.databinding.ActivityScanBinding
|
||||||
import java.text.DateFormat
|
import java.text.DateFormat
|
||||||
@@ -22,9 +23,6 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||||
private val dateFormat: DateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH)
|
private val dateFormat: DateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH)
|
||||||
|
|
||||||
private val blueMan by lazy { blueMan() }
|
|
||||||
private lateinit var bluetoothHandler: BluetoothHandler
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityScanBinding.inflate(layoutInflater)
|
binding = ActivityScanBinding.inflate(layoutInflater)
|
||||||
@@ -33,22 +31,17 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
initBluetoothHandler()
|
initBluetoothHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val central get() = bluetoothHandler.central
|
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
private fun initBluetoothHandler() {
|
private fun initBluetoothHandler() {
|
||||||
if (this::bluetoothHandler.isInitialized) return
|
|
||||||
bluetoothHandler = BluetoothHandler.ble(this)
|
|
||||||
|
|
||||||
println("OnCreate called, Initializing...")
|
println("OnCreate called, Initializing...")
|
||||||
|
|
||||||
// List bonded device addresses
|
// List bonded device addresses
|
||||||
val pairedDevices = blueMan.adapter.bondedDevices.toList()
|
val pairedDevices = blueMan().adapter.bondedDevices.toList()
|
||||||
val pairedAddresses = pairedDevices.map { it.address }.toSet()
|
val pairedAddresses = pairedDevices.map { it.address }.toSet()
|
||||||
|
|
||||||
// Scan devices
|
// Scan devices
|
||||||
val scannedDevices = ArrayList<BluetoothPeripheral>()
|
val scannedDevices = ArrayList<BluetoothPeripheral>()
|
||||||
central.scanForPeripherals({ peripheral, scanResult ->
|
ble.central.scanForPeripherals({ peripheral, scanResult ->
|
||||||
if (peripheral.name.isBlank() || scannedDevices.contains(peripheral)) return@scanForPeripherals
|
if (peripheral.name.isBlank() || scannedDevices.contains(peripheral)) return@scanForPeripherals
|
||||||
|
|
||||||
// Add to scanned devices
|
// Add to scanned devices
|
||||||
@@ -64,8 +57,8 @@ 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()
|
ble.central.stopScan()
|
||||||
bluetoothHandler.connectPeripheral(scannedDevices[position]) {
|
ble.connectPeripheral(scannedDevices[position]) {
|
||||||
view.snack("✅ Connected.")
|
view.snack("✅ Connected.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,16 +76,16 @@ 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
|
||||||
bluetoothHandler.connectAddress(dev.address) {
|
ble.connectAddress(dev.address) {
|
||||||
view.snack("✅ Connected.")
|
view.snack("✅ Connected.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
collectHeartRate(bluetoothHandler)
|
collectHeartRate(ble)
|
||||||
collectPulseOxContinuous(bluetoothHandler)
|
collectPulseOxContinuous(ble)
|
||||||
collectPulseOxSpot(bluetoothHandler)
|
collectPulseOxSpot(ble)
|
||||||
collectTemperature(bluetoothHandler)
|
collectTemperature(ble)
|
||||||
collectWeight(bluetoothHandler)
|
collectWeight(ble)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun collectHeartRate(bluetoothHandler: BluetoothHandler) {
|
private fun collectHeartRate(bluetoothHandler: BluetoothHandler) {
|
||||||
|
|||||||
@@ -236,51 +236,50 @@ internal class BluetoothHandler private constructor(context: Context) {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
// UUIDs for the Blood Pressure service (BLP)
|
// UUIDs for the Blood Pressure service (BLP)
|
||||||
private val BLP_SERVICE_UUID: UUID = UUID.fromString("00001810-0000-1000-8000-00805f9b34fb")
|
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")
|
val BLP_MEASUREMENT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A35-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Health Thermometer service (HTS)
|
// UUIDs for the Health Thermometer service (HTS)
|
||||||
private val HTS_SERVICE_UUID = UUID.fromString("00001809-0000-1000-8000-00805f9b34fb")
|
val HTS_SERVICE_UUID = UUID.fromString("00001809-0000-1000-8000-00805f9b34fb")
|
||||||
private val HTS_MEASUREMENT_CHARACTERISTIC_UUID = UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb")
|
val HTS_MEASUREMENT_CHARACTERISTIC_UUID = UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Heart Rate service (HRS)
|
// UUIDs for the Heart Rate service (HRS)
|
||||||
private val HRS_SERVICE_UUID: UUID = UUID.fromString("0000180D-0000-1000-8000-00805f9b34fb")
|
val HRS_SERVICE_UUID: UUID = UUID.fromString("0000180D-0000-1000-8000-00805f9b34fb")
|
||||||
private val HRS_MEASUREMENT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A37-0000-1000-8000-00805f9b34fb")
|
val HRS_MEASUREMENT_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A37-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Device Information service (DIS)
|
// UUIDs for the Device Information service (DIS)
|
||||||
private val DIS_SERVICE_UUID: UUID = UUID.fromString("0000180A-0000-1000-8000-00805f9b34fb")
|
val DIS_SERVICE_UUID: UUID = UUID.fromString("0000180A-0000-1000-8000-00805f9b34fb")
|
||||||
private val MANUFACTURER_NAME_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A29-0000-1000-8000-00805f9b34fb")
|
val MANUFACTURER_NAME_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A29-0000-1000-8000-00805f9b34fb")
|
||||||
private val MODEL_NUMBER_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A24-0000-1000-8000-00805f9b34fb")
|
val MODEL_NUMBER_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A24-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Current Time service (CTS)
|
// UUIDs for the Current Time service (CTS)
|
||||||
private val CTS_SERVICE_UUID: UUID = UUID.fromString("00001805-0000-1000-8000-00805f9b34fb")
|
val CTS_SERVICE_UUID: UUID = UUID.fromString("00001805-0000-1000-8000-00805f9b34fb")
|
||||||
private val CURRENT_TIME_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A2B-0000-1000-8000-00805f9b34fb")
|
val CURRENT_TIME_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A2B-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Battery Service (BAS)
|
// UUIDs for the Battery Service (BAS)
|
||||||
private val BTS_SERVICE_UUID: UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb")
|
val BTS_SERVICE_UUID: UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb")
|
||||||
private val BATTERY_LEVEL_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A19-0000-1000-8000-00805f9b34fb")
|
val BATTERY_LEVEL_CHARACTERISTIC_UUID: UUID = UUID.fromString("00002A19-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Pulse Oximeter Service (PLX)
|
// UUIDs for the Pulse Oximeter Service (PLX)
|
||||||
val PLX_SERVICE_UUID: UUID = UUID.fromString("00001822-0000-1000-8000-00805f9b34fb")
|
val PLX_SERVICE_UUID: UUID = UUID.fromString("00001822-0000-1000-8000-00805f9b34fb")
|
||||||
private val PLX_SPOT_MEASUREMENT_CHAR_UUID: UUID = UUID.fromString("00002a5e-0000-1000-8000-00805f9b34fb")
|
val PLX_SPOT_MEASUREMENT_CHAR_UUID: UUID = UUID.fromString("00002a5e-0000-1000-8000-00805f9b34fb")
|
||||||
private val PLX_CONTINUOUS_MEASUREMENT_CHAR_UUID: UUID = UUID.fromString("00002a5f-0000-1000-8000-00805f9b34fb")
|
val PLX_CONTINUOUS_MEASUREMENT_CHAR_UUID: UUID = UUID.fromString("00002a5f-0000-1000-8000-00805f9b34fb")
|
||||||
|
|
||||||
// UUIDs for the Weight Scale Service (WSS)
|
// UUIDs for the Weight Scale Service (WSS)
|
||||||
val WSS_SERVICE_UUID: UUID = UUID.fromString("0000181D-0000-1000-8000-00805f9b34fb")
|
val WSS_SERVICE_UUID: UUID = UUID.fromString("0000181D-0000-1000-8000-00805f9b34fb")
|
||||||
private val WSS_MEASUREMENT_CHAR_UUID: UUID = UUID.fromString("00002A9D-0000-1000-8000-00805f9b34fb")
|
val WSS_MEASUREMENT_CHAR_UUID: UUID = UUID.fromString("00002A9D-0000-1000-8000-00805f9b34fb")
|
||||||
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")
|
||||||
|
|
||||||
// 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")
|
val CONTOUR_CLOCK = UUID.fromString("00001026-0002-11E2-9E96-0800200C9A66")
|
||||||
private var instance: BluetoothHandler? = null
|
|
||||||
|
|
||||||
@Synchronized
|
private var instance: BluetoothHandler? = null
|
||||||
fun ble(context: Context): BluetoothHandler {
|
val Context.ble get(): BluetoothHandler {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = BluetoothHandler(context.applicationContext)
|
instance = BluetoothHandler(this)
|
||||||
}
|
}
|
||||||
return instance!!
|
return instance!!
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,3 @@ package org.hydev.wearsync.bles
|
|||||||
import com.welie.blessed.ScanFailure
|
import com.welie.blessed.ScanFailure
|
||||||
|
|
||||||
class ScanException(val fail: ScanFailure) : Exception()
|
class ScanException(val fail: ScanFailure) : Exception()
|
||||||
{
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user