diff options
author | Johanna Ye <xincheny@google.com> | 2021-06-22 15:23:03 +0200 |
---|---|---|
committer | Johanna Ye <xincheny@google.com> | 2021-07-02 10:44:42 +0000 |
commit | 0140aae62534c1c6f15edb3d32193a0a70b8ef45 (patch) | |
tree | c7f9bdb152c6873f6fe927ae8aae0008da9dbbfb /framework/java/android/bluetooth/BluetoothGatt.java | |
parent | 8553bf4231434ce8e6240dff2de2b716b831d960 (diff) |
Fix long characteristic write concurrency bug.
Tag: #stability
Test: ADT3 with test app
Bug: 169559728
Ignore-AOSP-First: Current security annotation parity
Merged-In: I3e3a5ff0965c1ca4f79e70e6163bc5d6917edbd1
Change-Id: I3e3a5ff0965c1ca4f79e70e6163bc5d6917edbd1
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothGatt.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothGatt.java | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/framework/java/android/bluetooth/BluetoothGatt.java b/framework/java/android/bluetooth/BluetoothGatt.java index aea82102ca..e6457f7140 100644 --- a/framework/java/android/bluetooth/BluetoothGatt.java +++ b/framework/java/android/bluetooth/BluetoothGatt.java @@ -82,6 +82,9 @@ public final class BluetoothGatt implements BluetoothProfile { private static final int CONN_STATE_DISCONNECTING = 3; private static final int CONN_STATE_CLOSED = 4; + private static final int WRITE_CHARACTERISTIC_MAX_RETRIES = 5; + private static final int WRITE_CHARACTERISTIC_TIME_TO_WAIT = 1000; // milliseconds + private List<BluetoothGattService> mServices; /** A GATT operation completed successfully */ @@ -134,6 +137,27 @@ public final class BluetoothGatt implements BluetoothProfile { public static final int CONNECTION_PRIORITY_LOW_POWER = 2; /** + * A GATT writeCharacteristic request is started successfully. + * + * @hide + */ + public static final int GATT_WRITE_REQUEST_SUCCESS = 0; + + /** + * A GATT writeCharacteristic request failed to start. + * + * @hide + */ + public static final int GATT_WRITE_REQUEST_FAIL = 1; + + /** + * A GATT writeCharacteristic request is issued to a busy remote device. + * + * @hide + */ + public static final int GATT_WRITE_REQUEST_BUSY = 2; + + /** * No authentication required. * * @hide @@ -440,9 +464,19 @@ public final class BluetoothGatt implements BluetoothProfile { try { final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.writeCharacteristic(mClientIf, address, handle, - characteristic.getWriteType(), authReq, - characteristic.getValue(), mAttributionSource); + int requestStatus = GATT_WRITE_REQUEST_FAIL; + for (int i = 0; i < WRITE_CHARACTERISTIC_MAX_RETRIES; i++) { + requestStatus = mService.writeCharacteristic(mClientIf, address, + handle, characteristic.getWriteType(), authReq, + characteristic.getValue(), mAttributionSource); + if (requestStatus != GATT_WRITE_REQUEST_BUSY) { + break; + } + try { + Thread.sleep(WRITE_CHARACTERISTIC_TIME_TO_WAIT); + } catch (InterruptedException e) { + } + } mAuthRetryState++; return; } catch (RemoteException e) { @@ -1264,21 +1298,35 @@ public final class BluetoothGatt implements BluetoothProfile { if (device == null) return false; synchronized (mDeviceBusyLock) { - if (mDeviceBusy) return false; + if (mDeviceBusy) { + return false; + } mDeviceBusy = true; } + int requestStatus = GATT_WRITE_REQUEST_FAIL; try { - mService.writeCharacteristic(mClientIf, device.getAddress(), + for (int i = 0; i < WRITE_CHARACTERISTIC_MAX_RETRIES; i++) { + requestStatus = mService.writeCharacteristic(mClientIf, device.getAddress(), characteristic.getInstanceId(), characteristic.getWriteType(), AUTHENTICATION_NONE, characteristic.getValue(), mAttributionSource); + if (requestStatus != GATT_WRITE_REQUEST_BUSY) { + break; + } + try { + Thread.sleep(WRITE_CHARACTERISTIC_TIME_TO_WAIT); + } catch (InterruptedException e) { + } + } } catch (RemoteException e) { Log.e(TAG, "", e); - mDeviceBusy = false; + synchronized (mDeviceBusyLock) { + mDeviceBusy = false; + } return false; } - return true; + return requestStatus == GATT_WRITE_REQUEST_SUCCESS; } /** |