improve beacon ui
This commit is contained in:
parent
c19417e00f
commit
2e16c81a5e
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="temurin-11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
|
|
|||
|
|
@ -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<Float, BleException> {
|
||||
|
||||
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))
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()) {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue