From 32bdc496cb1d1c63530ac8c6ee375803333b7fcd Mon Sep 17 00:00:00 2001 From: Vineyro Date: Mon, 26 Jun 2023 14:47:36 +0700 Subject: [PATCH] Add accelerometer descriptor --- .../llc/arma/ble/data/BleRepositoryImpl.kt | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/llc/arma/ble/data/BleRepositoryImpl.kt b/app/src/main/java/llc/arma/ble/data/BleRepositoryImpl.kt index 824a17c..03aff5b 100644 --- a/app/src/main/java/llc/arma/ble/data/BleRepositoryImpl.kt +++ b/app/src/main/java/llc/arma/ble/data/BleRepositoryImpl.kt @@ -32,6 +32,8 @@ import kotlin.random.Random val serviceUUID: UUID = UUID.fromString("a77db03a-9bc4-11ed-a8fc-0242ac120002") +val accelerometerDescriptorUUID: UUID = UUID.fromString("a77db6f2-9bc4-11ed-a8fc-0242ac120002") +val accelerometerReadUUID: UUID = UUID.fromString("00002713-0000-1000-8000-00805f9b34fb") val temperatureHistoryReadUUID: UUID = UUID.fromString("a77db2d8-9bc4-11ed-a8fc-0242ac120002") val temperatureReadUUID: UUID = UUID.fromString("00002a6e-0000-1000-8000-00805f9b34fb") val intervalReadUUID: UUID = UUID.fromString("a77db2d8-9bc4-11ed-a8fc-0242ac120002") @@ -99,6 +101,7 @@ class BleRepositoryImpl @Inject constructor( private val deviceCache = mutableMapOf() val resultList: MutableMap = Collections.synchronizedMap(mutableMapOf()) + override fun getConnectedBle(): List { if(checkPermission()) { @@ -138,13 +141,13 @@ class BleRepositoryImpl @Inject constructor( if (checkPermission()) { - //if (result.scanRecord?.deviceName?.contains("ArmA") == true) { + if (result.scanRecord?.deviceName?.contains("ArmA") == true) { resultList[result.device.address] = result.info deviceCache[result.device.address] = result - //} + } } else { CoroutineScope(Dispatchers.IO).launch { @@ -617,15 +620,56 @@ class BleRepositoryImpl @Inject constructor( override fun getAccelerometerMeasureBySerialFlow(serial: String): Flow> { - return flow { + return callbackFlow { + + deviceCache[serial]?.let { + + it.device.connectGatt(app, false, object : BluetoothGattCallback() { + + override fun onConnectionStateChange( + gatt: BluetoothGatt, + status: Int, + newState: Int + ) { + gatt.discoverServices() + } + + override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { + super.onServicesDiscovered(gatt, status) + gatt.getService(serviceUUID)?.getCharacteristic(accelerometerReadUUID)?.let { + gatt.setCharacteristicNotification(it, true) + gatt.writeDescriptor(it.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")), BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) + } + } + + override fun onCharacteristicChanged( + gatt: BluetoothGatt, + characteristic: BluetoothGattCharacteristic, + value: ByteArray + ) { + + Log.d("new", value.toString()) + + CoroutineScope(Dispatchers.IO).launch { + + send( + Result.success(((-256)..256).random().toFloat()) + ) + + } + + } + + }) - while (true){ - delay(1000) - emit( - Result.success(((-256)..256).random().toFloat()) - ) } + awaitClose { + + } + } + + } private suspend fun readCharacteristic(