[+] Allow additional setup for decoders

This commit is contained in:
Azalea Gui
2023-01-24 11:18:14 -05:00
parent 393398ebe0
commit 876e3aa606
3 changed files with 30 additions and 27 deletions
@@ -41,7 +41,6 @@ internal class BluetoothHandler private constructor(context: Context) {
observe(bloodPressureChannel, BloodPressureDecoder()) observe(bloodPressureChannel, BloodPressureDecoder())
observe(pulseOxSpotChannel, PLXSpotDecoder()) observe(pulseOxSpotChannel, PLXSpotDecoder())
observe(pulseOxContinuousChannel, PLXContinuousDecoder()) observe(pulseOxContinuousChannel, PLXContinuousDecoder())
setupGLXnotifications(this@handle)
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Timber.e(e) Timber.e(e)
} catch (b: GattException) { } catch (b: GattException) {
@@ -50,27 +49,6 @@ internal class BluetoothHandler private constructor(context: Context) {
} }
} }
private suspend fun setupGLXnotifications(peripheral: BluePeri) {
peripheral.observe(glucoseChannel, GlucoseDecoder())
peripheral.getCharacteristic(GLUCOSE_SERVICE_UUID, GLUCOSE_RECORD_ACCESS_POINT_CHARACTERISTIC_UUID)?.let {
val result = peripheral.observe(it) { value ->
Timber.d("record access response: ${value.asHexString()}")
}
if (result) {
writeGetAllGlucoseMeasurements(peripheral)
}
}
}
private suspend fun writeGetAllGlucoseMeasurements(peripheral: BluePeri) {
val OP_CODE_REPORT_STORED_RECORDS: Byte = 1
val OPERATOR_ALL_RECORDS: Byte = 1
val command = byteArrayOf(OP_CODE_REPORT_STORED_RECORDS, OPERATOR_ALL_RECORDS)
peripheral.writeCharacteristic(GLUCOSE_SERVICE_UUID, GLUCOSE_RECORD_ACCESS_POINT_CHARACTERISTIC_UUID, command, WriteType.WITH_RESPONSE)
}
/** /**
* Observe a specific mesasurement * Observe a specific mesasurement
*/ */
@@ -82,6 +60,7 @@ internal class BluetoothHandler private constructor(context: Context) {
Timber.d(measurement.toString()) Timber.d(measurement.toString())
} }
} }
dec.additionalSetup(this)
} }
suspend fun <T> BluePeri.observe(chan: Channel<T>, dec: IDecoder<T>) = suspend fun <T> BluePeri.observe(chan: Channel<T>, dec: IDecoder<T>) =
@@ -114,9 +93,6 @@ internal class BluetoothHandler private constructor(context: Context) {
} }
companion object { companion object {
val GLUCOSE_SERVICE_UUID = UUID.fromString("00001808-0000-1000-8000-00805f9b34fb")
val GLUCOSE_RECORD_ACCESS_POINT_CHARACTERISTIC_UUID = UUID.fromString("00002A52-0000-1000-8000-00805f9b34fb")
private var instance: BluetoothHandler? = null private var instance: BluetoothHandler? = null
val Context.ble get(): BluetoothHandler { val Context.ble get(): BluetoothHandler {
if (instance == null) { if (instance == null) {
@@ -5,10 +5,13 @@ import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_SFLOAT
import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_SINT16 import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_SINT16
import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_UINT16 import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_UINT16
import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_UINT8 import com.welie.blessed.BluetoothBytesParser.Companion.FORMAT_UINT8
import org.hydev.wearsync.bles.BluetoothHandler.Companion.GLUCOSE_SERVICE_UUID import com.welie.blessed.WriteType
import com.welie.blessed.asHexString
import org.hydev.wearsync.BluePeri
import org.hydev.wearsync.bles.ObservationUnit import org.hydev.wearsync.bles.ObservationUnit
import org.hydev.wearsync.bles.ObservationUnit.MiligramPerDeciliter import org.hydev.wearsync.bles.ObservationUnit.MiligramPerDeciliter
import org.hydev.wearsync.bles.ObservationUnit.MmolPerLiter import org.hydev.wearsync.bles.ObservationUnit.MmolPerLiter
import timber.log.Timber
import java.util.* import java.util.*
data class GlucoseMeasurement( data class GlucoseMeasurement(
@@ -22,7 +25,11 @@ data class GlucoseMeasurement(
class GlucoseDecoder : IDecoder<GlucoseMeasurement> class GlucoseDecoder : IDecoder<GlucoseMeasurement>
{ {
override val sid = GLUCOSE_SERVICE_UUID companion object {
val GLUCOSE_RECORD_AP_CID = UUID.fromString("00002A52-0000-1000-8000-00805f9b34fb")
}
override val sid = UUID.fromString("00001808-0000-1000-8000-00805f9b34fb")
override val cid = UUID.fromString("00002A18-0000-1000-8000-00805f9b34fb") override val cid = UUID.fromString("00002A18-0000-1000-8000-00805f9b34fb")
override fun decode(value: ByteArray): GlucoseMeasurement override fun decode(value: ByteArray): GlucoseMeasurement
@@ -52,4 +59,18 @@ class GlucoseDecoder : IDecoder<GlucoseMeasurement>
contextWillFollow = contextWillFollow contextWillFollow = contextWillFollow
) )
} }
override suspend fun additionalSetup(peri: BluePeri)
{
peri.getCharacteristic(sid, GLUCOSE_RECORD_AP_CID)?.let {
val result = peri.observe(it) { value ->
Timber.d("record access response: ${value.asHexString()}")
}
if (result) {
// command = (Op code report stored records = 1, operator all records = 1)
peri.writeCharacteristic(sid, GLUCOSE_RECORD_AP_CID, byteArrayOf(1, 1), WriteType.WITH_RESPONSE)
}
}
}
} }
@@ -1,5 +1,6 @@
package org.hydev.wearsync.bles.decoders package org.hydev.wearsync.bles.decoders
import org.hydev.wearsync.BluePeri
import java.util.* import java.util.*
interface IDecoder<T> interface IDecoder<T>
@@ -8,4 +9,9 @@ interface IDecoder<T>
val cid: UUID val cid: UUID
fun decode(value: ByteArray): T fun decode(value: ByteArray): T
/**
* Optional step to write data after read
*/
suspend fun additionalSetup(peri: BluePeri) {}
} }