From 2e16c81a5ea0a9c7e9db6a6d5216b17e368e072d Mon Sep 17 00:00:00 2001 From: Vineyro Date: Fri, 21 Apr 2023 12:01:05 +0700 Subject: [PATCH] improve beacon ui --- .idea/misc.xml | 3 +- .../llc/arma/ble/data/BleRepositoryImpl.kt | 41 +++++++++++++++---- .../llc/arma/ble/data/ReadHistoryCallback.kt | 17 ++++---- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 54d5acd..4c5e777 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,6 @@ - - + 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 539ab11..d47a521 100644 --- a/app/src/main/java/llc/arma/ble/data/BleRepositoryImpl.kt +++ b/app/src/main/java/llc/arma/ble/data/BleRepositoryImpl.kt @@ -4,7 +4,6 @@ import android.Manifest import android.app.Application import android.bluetooth.* import android.bluetooth.le.ScanCallback -import android.bluetooth.le.ScanFilter import android.bluetooth.le.ScanResult import android.bluetooth.le.ScanSettings import android.content.pm.PackageManager @@ -24,13 +23,10 @@ import llc.arma.ble.domain.model.Ble import llc.arma.ble.domain.model.BleInfo import llc.arma.ble.domain.model.ConnectedBleInfo import llc.arma.ble.domain.repository.BleRepository -import llc.arma.ble.domain.usecase.GetBleBySerial -import java.nio.charset.Charset import java.util.* import javax.inject.Inject import javax.inject.Singleton import kotlin.coroutines.resume -import kotlin.coroutines.suspendCoroutine val serviceUUID: UUID = UUID.fromString("a77db03a-9bc4-11ed-a8fc-0242ac120002") @@ -43,6 +39,19 @@ val passwordWriteUUID: UUID = UUID.fromString("a77db6f2-9bc4-11ed-a8fc-0242ac120 val txWriteUUID: UUID = UUID.fromString("00002a07-0000-1000-8000-00805f9b34fb") val flashWriteUUID: UUID = UUID.fromString("a77db6f2-9bc4-11ed-a8fc-0242ac120002") +@OptIn(ExperimentalUnsignedTypes::class) +fun UByteArray.toTemperature(): Float { + val uShort = (this[0] + this[1] * 256u).toUShort() + + val result = if (uShort > Short.MAX_VALUE.toUShort()) { + ((uShort.inv() + 1u).toFloat().unaryMinus()) / 100f + } else { + uShort.toFloat() / 100f + } + + return result +} + @Singleton class BleRepositoryImpl @Inject constructor( private val app: Application @@ -342,10 +351,22 @@ class BleRepositoryImpl @Inject constructor( } + @OptIn(ExperimentalUnsignedTypes::class) private suspend fun readTemperature( record: ScanResult ): Result { + writeCharacteristic( + device = record.device, + serviceId = serviceUUID, + characteristicId = temperatureReadUUID, + writeData = byteArrayOf(1, 1) + ).onFailure { + return Result.failure(it) + } + + delay(1_000) + val dataResult = readCharacteristic( device = record.device, serviceId = serviceUUID, @@ -357,7 +378,7 @@ class BleRepositoryImpl @Inject constructor( onSuccess = { return@fold it } ) - return Result.success((dataResult[0] + dataResult[1] * 256).toFloat() / 100f) + return Result.success(dataResult.toUByteArray().toTemperature()) } @@ -701,21 +722,22 @@ class BleRepositoryImpl @Inject constructor( } else { + bleGatt?.close() it.resume(Result.failure(BleException.PermissionDenied)) } } else { - it.resume(Result.success(Unit)) bleGatt?.close() + it.resume(Result.success(Unit)) } } else { - it.resume(Result.failure(BleException.UnexpectedResponse)) bleGatt?.close() + it.resume(Result.failure(BleException.UnexpectedResponse)) } @@ -743,6 +765,7 @@ class BleRepositoryImpl @Inject constructor( } else { + bleGatt?.close() it.resume(Result.failure(BleException.PermissionDenied)) } @@ -753,12 +776,12 @@ class BleRepositoryImpl @Inject constructor( Log.d("write", "service not found") - gatt.disconnect() + bleGatt?.close() it.resume(Result.failure(BleException.UnexpectedResponse)) } else { - gatt.disconnect() + bleGatt?.close() it.resume(Result.failure(BleException.UnexpectedResponse)) } diff --git a/app/src/main/java/llc/arma/ble/data/ReadHistoryCallback.kt b/app/src/main/java/llc/arma/ble/data/ReadHistoryCallback.kt index ce51983..a8fcb0a 100644 --- a/app/src/main/java/llc/arma/ble/data/ReadHistoryCallback.kt +++ b/app/src/main/java/llc/arma/ble/data/ReadHistoryCallback.kt @@ -7,6 +7,7 @@ import android.bluetooth.BluetoothGattCallback import android.bluetooth.BluetoothGattCharacteristic import android.content.pm.PackageManager import android.os.Build +import android.util.Log import androidx.core.app.ActivityCompat import llc.arma.ble.domain.Result import llc.arma.ble.domain.common.BleException @@ -132,6 +133,8 @@ class ReadHistoryCallback( value: ByteArray, status: Int ){ + Log.d("read", value[0].toString()) + if(status == BluetoothGatt.GATT_SUCCESS){ when(readProperty){ Property.DATA_SIZE -> { @@ -175,17 +178,15 @@ class ReadHistoryCallback( resultTemperaturePackage.addAll( temperatureDataArray.chunked(2).map { - (it[0] + it[1] * 256u).toFloat() / 100f + it.toUByteArray().toTemperature() }.toMutableList() ) - val totalDataSize = value.get2byteUIntAt(2).toInt() + temperatureDataArray.size / 2 - val nextPackageDataCount = value.get2byteUIntAt(2) expectedDataSize = nextPackageDataCount.toInt() + resultTemperaturePackage.size - - onResult(Result.success(ProgressState.Progress(0f / totalDataSize.toFloat()))) - onResult(Result.success(ProgressState.Progress(nextPackageDataCount.toFloat() / totalDataSize.toFloat()))) + Log.d("read", expectedDataSize.toString()) + onResult(Result.success(ProgressState.Progress(0f / expectedDataSize!!.toFloat()))) + onResult(Result.success(ProgressState.Progress(resultTemperaturePackage.size.toFloat() / expectedDataSize!!.toFloat()))) if(nextPackageDataCount != 0.toUInt()){ @@ -226,11 +227,11 @@ class ReadHistoryCallback( resultTemperaturePackage.addAll( temperatureDataArray.chunked(2).map { - (it[0] + it[1] * 256u).toFloat() / 100f + it.toUByteArray().toTemperature() } ) - onResult(Result.success(ProgressState.Progress(expectedDataSize!!.toFloat() / resultTemperaturePackage.size.toFloat()))) + onResult(Result.success(ProgressState.Progress(resultTemperaturePackage.size.toFloat() / expectedDataSize!!.toFloat()))) if (nextPackageDataCount != 0.toUInt()) {