diff options
author | Ugo Yu <ugoyu@google.com> | 2021-06-02 14:30:37 +0800 |
---|---|---|
committer | Myles Watson <mylesgw@google.com> | 2021-07-30 23:17:18 +0000 |
commit | 5666c0d4fe4785bc3049b660cdb167be29fc46e3 (patch) | |
tree | f9cbf00264867769251c9eca0a12013cb1f0607e /service/java/com/android/server/bluetooth/BluetoothManagerService.java | |
parent | 7ec057d06da6b93375be214190358dab4b21259a (diff) |
Broadcast Bluetooth state to OFF properly
In current logic, we broadcast Bluetooth state OFF at BLE_ON state.
However, if Bluetooth gets crashed, we won't have the chance to send OFF
intent as we never get to BLE_ON.
This change makes the BluetoothManagerService skips the OFF intent only
if the previous state is a BLE state.
Bug: 189271317
Bug: 195032127
Test: manual
Change-Id: I3936766ab8bdd45d59550dacb64bdc0323bb424d
(cherry picked from commit 1b2f8bc9843b2d6ba7ba199a89412c8fb632f508)
Diffstat (limited to 'service/java/com/android/server/bluetooth/BluetoothManagerService.java')
-rw-r--r-- | service/java/com/android/server/bluetooth/BluetoothManagerService.java | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/service/java/com/android/server/bluetooth/BluetoothManagerService.java b/service/java/com/android/server/bluetooth/BluetoothManagerService.java index 85ff2be43b..b7c61a0d25 100644 --- a/service/java/com/android/server/bluetooth/BluetoothManagerService.java +++ b/service/java/com/android/server/bluetooth/BluetoothManagerService.java @@ -2505,6 +2505,16 @@ class BluetoothManagerService extends IBluetoothManager.Stub { mContext.sendBroadcastAsUser(intent, UserHandle.ALL, null, getTempAllowlistBroadcastOptions()); } + private boolean isBleState(int state) { + switch (state) { + case BluetoothAdapter.STATE_BLE_ON: + case BluetoothAdapter.STATE_BLE_TURNING_ON: + case BluetoothAdapter.STATE_BLE_TURNING_OFF: + return true; + } + return false; + } + @RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED, @@ -2527,8 +2537,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub { sendBluetoothServiceDownCallback(); unbindAndFinish(); sendBleStateChanged(prevState, newState); - // Don't broadcast as it has already been broadcast before - isStandardBroadcast = false; + + /* Currently, the OFF intent is broadcasted externally only when we transition + * from TURNING_OFF to BLE_ON state. So if the previous state is a BLE state, + * we are guaranteed that the OFF intent has been broadcasted earlier and we + * can safely skip it. + * Conversely, if the previous state is not a BLE state, it indicates that some + * sort of crash has occurred, moving us directly to STATE_OFF without ever + * passing through BLE_ON. We should broadcast the OFF intent in this case. */ + isStandardBroadcast = !isBleState(prevState); } else if (!intermediate_off) { // connect to GattService @@ -2581,6 +2598,11 @@ class BluetoothManagerService extends IBluetoothManager.Stub { // Show prevState of BLE_ON as OFF to standard users prevState = BluetoothAdapter.STATE_OFF; } + if (DBG) { + Slog.d(TAG, + "Sending State Change: " + BluetoothAdapter.nameForState(prevState) + " > " + + BluetoothAdapter.nameForState(newState)); + } Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |