BLECommonService improvement

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

View file

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