OSDN Git Service

Bluetooth: fix GATT race conditions when using Handler
authorJakub Pawlowski <jpawlowski@google.com>
Tue, 12 Sep 2017 21:48:30 +0000 (14:48 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Wed, 13 Sep 2017 22:43:09 +0000 (22:43 +0000)
Bug: 65596701
Test: manual
Change-Id: Id703cea0543626bdd5a583da95615b650bbcc331
Merged-In: Id703cea0543626bdd5a583da95615b650bbcc331

core/java/android/bluetooth/BluetoothGatt.java

index 678159b..2c3aa32 100644 (file)
@@ -350,12 +350,11 @@ public final class BluetoothGatt implements BluetoothProfile {
                     return;
                 }
 
-                if (status == 0) characteristic.setValue(value);
-
                 runOrQueueCallback(new Runnable() {
                     @Override
                     public void run() {
                         if (mCallback != null) {
+                            if (status == 0) characteristic.setValue(value);
                             mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic,
                                                            status);
                         }
@@ -428,12 +427,11 @@ public final class BluetoothGatt implements BluetoothProfile {
                 BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle);
                 if (characteristic == null) return;
 
-                characteristic.setValue(value);
-
                 runOrQueueCallback(new Runnable() {
                     @Override
                     public void run() {
                         if (mCallback != null) {
+                            characteristic.setValue(value);
                             mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
                         }
                     }
@@ -459,7 +457,6 @@ public final class BluetoothGatt implements BluetoothProfile {
                 BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle);
                 if (descriptor == null) return;
 
-                if (status == 0) descriptor.setValue(value);
 
                 if ((status == GATT_INSUFFICIENT_AUTHENTICATION
                   || status == GATT_INSUFFICIENT_ENCRYPTION)
@@ -481,6 +478,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                     @Override
                     public void run() {
                         if (mCallback != null) {
+                            if (status == 0) descriptor.setValue(value);
                             mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
                         }
                     }