OSDN Git Service

Properly check if BT is off when shutting down radios
authorChristine Hallstrom <hallstrom@google.com>
Wed, 25 May 2016 22:49:08 +0000 (15:49 -0700)
committerChristine Hallstrom <hallstrom@google.com>
Thu, 26 May 2016 20:20:56 +0000 (13:20 -0700)
Use BluetoothAdapter#getState() instead of
BluetoothAdapter#isEnabled() when checking if Bluetooth has turned
off, as isEnabled() is set to false early in the shutdown process
and getState() provides a better indication of the state of BT.

Bug: 27354612
Change-Id: Ic7828f0726491d49c9a14ba5d654b24f66743662

core/java/android/bluetooth/IBluetoothManager.aidl
services/core/java/com/android/server/BluetoothManagerService.java
services/core/java/com/android/server/power/ShutdownThread.java

index 0b81ee8..2b853a3 100644 (file)
@@ -37,6 +37,7 @@ interface IBluetoothManager
     boolean enable();
     boolean enableNoAutoConnect();
     boolean disable(boolean persist);
+    int getState();
     IBluetoothGatt getBluetoothGatt();
 
     boolean bindBluetoothProfileService(int profile, IBluetoothProfileServiceConnection proxy);
index 1f88be5..831ce01 100644 (file)
@@ -425,6 +425,24 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
         return false;
     }
 
+    public int getState() {
+        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
+                (!checkIfCallerIsForegroundUser())) {
+            Slog.w(TAG, "getState(): not allowed for non-active and non system user");
+            return BluetoothAdapter.STATE_OFF;
+        }
+
+        try {
+            mBluetoothLock.readLock().lock();
+            if (mBluetooth != null) return mBluetooth.getState();
+        } catch (RemoteException e) {
+            Slog.e(TAG, "getState()", e);
+        } finally {
+            mBluetoothLock.readLock().unlock();
+        }
+        return BluetoothAdapter.STATE_OFF;
+    }
+
     class ClientDeathRecipient implements IBinder.DeathRecipient {
         public void binderDied() {
             if (DBG) Slog.d(TAG, "Binder is dead -  unregister Ble App");
index 5b9d139..8ce2fd9 100644 (file)
@@ -543,7 +543,8 @@ public final class ShutdownThread extends Thread {
                 }
 
                 try {
-                    bluetoothOff = bluetooth == null || !bluetooth.isEnabled();
+                    bluetoothOff = bluetooth == null ||
+                            bluetooth.getState() == BluetoothAdapter.STATE_OFF;
                     if (!bluetoothOff) {
                         Log.w(TAG, "Disabling Bluetooth...");
                         bluetooth.disable(false);  // disable but don't persist new state
@@ -577,7 +578,7 @@ public final class ShutdownThread extends Thread {
 
                     if (!bluetoothOff) {
                         try {
-                            bluetoothOff = !bluetooth.isEnabled();
+                            bluetoothOff = bluetooth.getState() == BluetoothAdapter.STATE_OFF;
                         } catch (RemoteException ex) {
                             Log.e(TAG, "RemoteException during bluetooth shutdown", ex);
                             bluetoothOff = true;