diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt index cdb8c89053..c49367ddef 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt @@ -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