From 5edeedff83cd29af01b30551eeedaf86cbd1145a Mon Sep 17 00:00:00 2001 From: Etienne Ruffieux Date: Fri, 18 Mar 2022 16:20:49 +0000 Subject: Add Bluetooth state check as service null isn't sufficient. When verifying if Bluetooth is up in some APIs, BluetoothDevice was only checking if the service is null. When the adapter turns off, getState can return STATE_OFF before the callback to inform the Adapter of the new state is received, so the service isn't null but the state is off for a brief period of time. Adding the state checks along with the service checks prevents being able to call an API when Bluetooth is off. Bug: 214056525 Tag: #feature Test: atest BluetoothDeviceTest Ignore-AOSP-First: Merge conflict Change-Id: Ic387a19402ec6cd3e06677f554da8a8de695b4fc --- .../java/android/bluetooth/BluetoothDevice.java | 62 +++++++++++----------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothDevice.java') diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index 4d416f2052..222df3286a 100644 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/java/android/bluetooth/BluetoothDevice.java @@ -1490,7 +1490,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getIdentityAddress()"); final IBluetooth service = sService; final String defaultValue = null; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get identity address"); } else { try { @@ -1520,7 +1520,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getName()"); final IBluetooth service = sService; final String defaultValue = null; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1555,7 +1555,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getType()"); final IBluetooth service = sService; final int defaultValue = DEVICE_TYPE_UNKNOWN; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1584,7 +1584,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getAlias()"); final IBluetooth service = sService; final String defaultValue = null; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1645,7 +1645,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setAlias(" + alias + ")"); final IBluetooth service = sService; final int defaultValue = BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1679,7 +1679,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getBatteryLevel()"); final IBluetooth service = sService; final int defaultValue = BATTERY_LEVEL_BLUETOOTH_OFF; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1774,7 +1774,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("createBondOutOfBand()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "BT not enabled, createBondOutOfBand failed"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else if (NULL_MAC_ADDRESS.equals(mAddress)) { @@ -1807,7 +1807,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("isBondingInitiatedLocally()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "BT not enabled, isBondingInitiatedLocally failed"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1834,7 +1834,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("cancelBondProcess()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1867,7 +1867,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("removeBond()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1914,7 +1914,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getBondState() uncached"); final IBluetooth service = sService; final int defaultValue = BOND_NONE; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get bond state"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -1986,7 +1986,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("canBondWithoutDialog, device: " + this); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot check if we can skip pairing dialog"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2040,7 +2040,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { } final IBluetooth service = sService; final int defaultValue = BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot connect to remote device."); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2087,7 +2087,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { } final IBluetooth service = sService; final int defaultValue = BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot disconnect to remote device."); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2119,7 +2119,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("isConnected()"); final IBluetooth service = sService; final int defaultValue = CONNECTION_STATE_DISCONNECTED; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2151,7 +2151,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("isEncrypted()"); final IBluetooth service = sService; final int defaultValue = CONNECTION_STATE_DISCONNECTED; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2180,7 +2180,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getBluetoothClass()"); final IBluetooth service = sService; final int defaultValue = 0; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2323,7 +2323,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("sdpSearch()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot query remote device sdp records"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2350,7 +2350,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setPin()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2396,7 +2396,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setPairingConfirmation()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2435,7 +2435,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getPhonebookAccessPermission()"); final IBluetooth service = sService; final int defaultValue = ACCESS_UNKNOWN; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2482,7 +2482,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setSilenceMode()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { throw new IllegalStateException("Bluetooth is not turned ON"); } else { try { @@ -2512,7 +2512,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("isInSilenceMode()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { throw new IllegalStateException("Bluetooth is not turned ON"); } else { try { @@ -2543,7 +2543,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setPhonebookAccessPermission()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2572,7 +2572,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getMessageAccessPermission()"); final IBluetooth service = sService; final int defaultValue = ACCESS_UNKNOWN; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2609,7 +2609,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setMessageAccessPermission()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2638,7 +2638,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getSimAccessPermission()"); final IBluetooth service = sService; final int defaultValue = ACCESS_UNKNOWN; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -2671,7 +2671,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setSimAccessPermission()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.w(TAG, "Proxy not attached to service"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -3182,7 +3182,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setMetadata()"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "Bluetooth is not enabled. Cannot set metadata"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else if (value.length > METADATA_MAX_LENGTH) { @@ -3217,7 +3217,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("getMetadata()"); final IBluetooth service = sService; final byte[] defaultValue = null; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "Bluetooth is not enabled. Cannot get metadata"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { @@ -3260,7 +3260,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { if (DBG) log("setLowLatencyAudioAllowed(" + allowed + ")"); final IBluetooth service = sService; final boolean defaultValue = false; - if (service == null) { + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "Bluetooth is not enabled. Cannot allow low latency"); if (DBG) log(Log.getStackTraceString(new Throwable())); } else { -- cgit v1.2.3