This commit is contained in:
Vineyro 2023-06-29 09:10:57 +07:00
commit c47c371530
1 changed files with 52 additions and 8 deletions

View File

@ -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<String, ScanResult>()
val resultList: MutableMap<String, BleInfo> = Collections.synchronizedMap(mutableMapOf<String, BleInfo>())
override fun getConnectedBle(): List<ConnectedBleInfo> {
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<Result<Float, BleException>> {
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(