Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 681ae7fc authored by Zhihai Xu's avatar Zhihai Xu
Browse files

Cannot enable Bluetooth after using airplane and Bluetooth tethering sequentially

The root cause is we can't unbind blue service when bluetooth isnot disbaled
Otherwise the bluedroid stack will be out of sync with bluetooth service
only unbind bluetoothservice, when bluetooth is at OFF state.

bug 7376846

Change-Id: If5a11926f77a1ac29e75cdddbf5e90d492179f43
parent eb3aa44c
Loading
Loading
Loading
Loading
+21 −8
Original line number Diff line number Diff line
@@ -982,16 +982,11 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                sendBluetoothStateCallback(isUp);

                //If Bluetooth is off, send service down event to proxy objects, and unbind
                if (!isUp) {
                    //Only unbind with mEnable flag not set
                    //For race condition: disable and enable back-to-back
                    //Avoid unbind right after enable due to callback from disable
                    if ((!mEnable) && (mBluetooth != null)) {
                if (!isUp && canUnbindBluetoothService()) {
                    sendBluetoothServiceDownCallback();
                    unbindAndFinish();
                }
            }
            }

            //Send broadcast message to everyone else
            Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
@@ -1037,4 +1032,22 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        Log.e(TAG,"waitForOnOff time out");
        return false;
    }

    private boolean canUnbindBluetoothService() {
        synchronized(mConnection) {
            //Only unbind with mEnable flag not set
            //For race condition: disable and enable back-to-back
            //Avoid unbind right after enable due to callback from disable
            //Only unbind with Bluetooth at OFF state
            //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
            try {
                if (mEnable || (mBluetooth == null)) return false;
                if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
                return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
            } catch (RemoteException e) {
                Log.e(TAG, "getState()", e);
            }
        }
        return false;
    }
}