Add accelerometer descriptor
This commit is contained in:
parent
1270f48422
commit
32bdc496cb
|
|
@ -32,6 +32,8 @@ import kotlin.random.Random
|
||||||
|
|
||||||
val serviceUUID: UUID = UUID.fromString("a77db03a-9bc4-11ed-a8fc-0242ac120002")
|
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 temperatureHistoryReadUUID: UUID = UUID.fromString("a77db2d8-9bc4-11ed-a8fc-0242ac120002")
|
||||||
val temperatureReadUUID: UUID = UUID.fromString("00002a6e-0000-1000-8000-00805f9b34fb")
|
val temperatureReadUUID: UUID = UUID.fromString("00002a6e-0000-1000-8000-00805f9b34fb")
|
||||||
val intervalReadUUID: UUID = UUID.fromString("a77db2d8-9bc4-11ed-a8fc-0242ac120002")
|
val intervalReadUUID: UUID = UUID.fromString("a77db2d8-9bc4-11ed-a8fc-0242ac120002")
|
||||||
|
|
@ -99,6 +101,7 @@ class BleRepositoryImpl @Inject constructor(
|
||||||
|
|
||||||
private val deviceCache = mutableMapOf<String, ScanResult>()
|
private val deviceCache = mutableMapOf<String, ScanResult>()
|
||||||
val resultList: MutableMap<String, BleInfo> = Collections.synchronizedMap(mutableMapOf<String, BleInfo>())
|
val resultList: MutableMap<String, BleInfo> = Collections.synchronizedMap(mutableMapOf<String, BleInfo>())
|
||||||
|
|
||||||
override fun getConnectedBle(): List<ConnectedBleInfo> {
|
override fun getConnectedBle(): List<ConnectedBleInfo> {
|
||||||
|
|
||||||
if(checkPermission()) {
|
if(checkPermission()) {
|
||||||
|
|
@ -138,13 +141,13 @@ class BleRepositoryImpl @Inject constructor(
|
||||||
|
|
||||||
if (checkPermission()) {
|
if (checkPermission()) {
|
||||||
|
|
||||||
//if (result.scanRecord?.deviceName?.contains("ArmA") == true) {
|
if (result.scanRecord?.deviceName?.contains("ArmA") == true) {
|
||||||
|
|
||||||
resultList[result.device.address] = result.info
|
resultList[result.device.address] = result.info
|
||||||
|
|
||||||
deviceCache[result.device.address] = result
|
deviceCache[result.device.address] = result
|
||||||
|
|
||||||
//}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
|
@ -617,15 +620,56 @@ class BleRepositoryImpl @Inject constructor(
|
||||||
|
|
||||||
override fun getAccelerometerMeasureBySerialFlow(serial: String): Flow<Result<Float, BleException>> {
|
override fun getAccelerometerMeasureBySerialFlow(serial: String): Flow<Result<Float, BleException>> {
|
||||||
|
|
||||||
return flow {
|
return callbackFlow {
|
||||||
|
|
||||||
while (true){
|
deviceCache[serial]?.let {
|
||||||
delay(1000)
|
|
||||||
emit(
|
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())
|
Result.success(((-256)..256).random().toFloat())
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
awaitClose {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun readCharacteristic(
|
private suspend fun readCharacteristic(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue