BLECommonService improvement

This commit is contained in:
Milos Kozak 2021-10-24 13:09:20 +02:00
parent 22cad1a761
commit b16318da2c

View file

@ -167,16 +167,18 @@ class BLECommonService @Inject internal constructor(
@Synchronized
private fun writeCharacteristicNoResponse(characteristic: BluetoothGattCharacteristic, data: ByteArray) {
Thread(Runnable {
SystemClock.sleep(WRITE_DELAY_MILLIS)
if (bluetoothAdapter == null || bluetoothGatt == null) {
aapsLogger.error("BluetoothAdapter not initialized_ERROR")
isConnecting = false
isConnected = false
return@Runnable
synchronized(this) {
SystemClock.sleep(WRITE_DELAY_MILLIS)
if (bluetoothAdapter == null || bluetoothGatt == null) {
aapsLogger.error("BluetoothAdapter not initialized_ERROR")
isConnecting = false
isConnected = false
return@Runnable
}
characteristic.value = data
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
bluetoothGatt?.writeCharacteristic(characteristic)
}
characteristic.value = data
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
bluetoothGatt?.writeCharacteristic(characteristic)
}).start()
SystemClock.sleep(50)
}
@ -200,6 +202,7 @@ class BLECommonService @Inject internal constructor(
return bluetoothGatt?.services
}
@Synchronized
private fun findCharacteristic() {
val gattServices = getSupportedGattServices() ?: return
var uuid: String
@ -214,7 +217,7 @@ class BLECommonService @Inject internal constructor(
// nRF Connect 참고하여 추가함
val descriptor: BluetoothGattDescriptor = uartIndicate!!.getDescriptor(UUID.fromString(CHARACTERISTIC_CONFIG_UUID))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
bluetoothGatt!!.writeDescriptor(descriptor)
bluetoothGatt?.writeDescriptor(descriptor)
}
if (WRITE_UUID == uuid) {
uartWrite = gattCharacteristic