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

Commit 8594394a authored by Matthew Xie's avatar Matthew Xie
Browse files

Move mBluetoothService.runBluetooth after broadcasting the STATE_ON intent.

This is because broadcastState method move the Bluetooth adapter state
to ON. The mBluetoothService.runBluetooth should be called only in the ON state.
Remove mIsDiscovering variable in BluetoothService.
Fix a bug in BluetoothA2dpService that caused 2 times of state change
from DISCONNECTED to CONNECTING that messed up BluetoothService's
connection state count.
Change-Id: Ifb782a845ae70f007d2e036d930bb55f445d68b5
parent c98c3597
Loading
Loading
Loading
Loading
+5 −20
Original line number Diff line number Diff line
@@ -187,25 +187,11 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
        return false;
    }

    private synchronized boolean addAudioSink(BluetoothDevice device) {
        String path = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        String propValues[] = (String []) getSinkPropertiesNative(path);
        if (propValues == null) {
            Log.e(TAG, "Error while getting AudioSink properties for device: " + device);
            return false;
        }
        Integer state = null;
        // Properties are name-value pairs
        for (int i = 0; i < propValues.length; i+=2) {
            if (propValues[i].equals(PROPERTY_STATE)) {
                state = new Integer(convertBluezSinkStringToState(propValues[i+1]));
                break;
    private synchronized void addAudioSink(BluetoothDevice device) {
        if (mAudioDevices.get(device) == null) {
            mAudioDevices.put(device, BluetoothA2dp.STATE_DISCONNECTED);
        }
    }
        mAudioDevices.put(device, state);
        handleSinkStateChange(device, BluetoothA2dp.STATE_DISCONNECTED, state);
        return true;
    }

    private synchronized void onBluetoothEnable() {
        String devices = mBluetoothService.getProperty("Devices");
@@ -259,9 +245,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
            return false;
        }

        if (mAudioDevices.get(device) == null && !addAudioSink(device)) {
            return false;
        }
        addAudioSink(device);

        String path = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        if (path == null) {
@@ -495,6 +479,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
                // This is for an incoming connection for a device not known to us.
                // We have authorized it and bluez state has changed.
                addAudioSink(device);
                handleSinkStateChange(device, BluetoothA2dp.STATE_DISCONNECTED, state);
            } else {
                if (state == BluetoothA2dp.STATE_PLAYING && mPlayingA2dpDevice == null) {
                   mPlayingA2dpDevice = device;
+13 −19
Original line number Diff line number Diff line
@@ -156,7 +156,7 @@ final class BluetoothAdapterStateMachine extends StateMachine {
    private class PowerOff extends State {
        @Override
        public void enter() {
            if (DBG) log("Enter PowerOff: ");
            if (DBG) log("Enter PowerOff: " + getCurrentMessage().what);
        }
        @Override
        public boolean processMessage(Message message) {
@@ -280,7 +280,7 @@ final class BluetoothAdapterStateMachine extends StateMachine {

        @Override
        public void enter() {
            if (DBG) log("Enter WarmUp");
            if (DBG) log("Enter WarmUp: " + getCurrentMessage().what);
        }

        @Override
@@ -319,7 +319,7 @@ final class BluetoothAdapterStateMachine extends StateMachine {
    private class HotOff extends State {
        @Override
        public void enter() {
            if (DBG) log("Enter HotOff:");
            if (DBG) log("Enter HotOff: " + getCurrentMessage().what);
        }

        @Override
@@ -380,8 +380,7 @@ final class BluetoothAdapterStateMachine extends StateMachine {

        @Override
        public void enter() {
            int what = getCurrentMessage().what;
            if (DBG) log("Enter Switching: " + what);
            if (DBG) log("Enter Switching: " + getCurrentMessage().what);
        }
        @Override
        public boolean processMessage(Message message) {
@@ -390,13 +389,12 @@ final class BluetoothAdapterStateMachine extends StateMachine {
            boolean retValue = HANDLED;
            switch(message.what) {
                case BECAME_PAIRABLE:
                    String[] propVal = {"Pairable", mBluetoothService.getProperty("Pairable")};
                    mEventLoop.onPropertyChanged(propVal);

                    // run bluetooth now that it's turned on
                    mBluetoothService.runBluetooth();
                    mBluetoothService.initBluetoothAfterTurningOn();
                    transitionTo(mBluetoothOn);
                    broadcastState(BluetoothAdapter.STATE_ON);
                    // run bluetooth now that it's turned on
                    // Note runBluetooth should be called only in adapter STATE_ON
                    mBluetoothService.runBluetooth();
                    break;
                case BECAME_NON_PAIRABLE:
                    if (mBluetoothService.getAdapterConnectionState() ==
@@ -438,11 +436,10 @@ final class BluetoothAdapterStateMachine extends StateMachine {
    }

    private class BluetoothOn extends State {
        private boolean mPersistBluetoothOff = false;

        @Override
        public void enter() {
            if (DBG) log("Enter BluetoothOn: " + mPersistBluetoothOff);
            if (DBG) log("Enter BluetoothOn: " + getCurrentMessage().what);
        }
        @Override
        public boolean processMessage(Message message) {
@@ -500,7 +497,7 @@ final class BluetoothAdapterStateMachine extends StateMachine {

        @Override
        public void enter() {
            if (DBG) log("Enter PerProcessState");
            if (DBG) log("Enter PerProcessState: " + getCurrentMessage().what);
        }

        @Override
@@ -523,14 +520,11 @@ final class BluetoothAdapterStateMachine extends StateMachine {
                case USER_TURN_ON:
                    broadcastState(BluetoothAdapter.STATE_TURNING_ON);
                    persistSwitchSetting(true);

                    String[] propVal = {"Pairable", mBluetoothService.getProperty("Pairable")};
                    mEventLoop.onPropertyChanged(propVal);

                    // run bluetooth now that it's turned on
                    mBluetoothService.runBluetooth();
                    mBluetoothService.initBluetoothAfterTurningOn();
                    transitionTo(mBluetoothOn);
                    broadcastState(BluetoothAdapter.STATE_ON);
                    // run bluetooth now that it's turned on
                    mBluetoothService.runBluetooth();
                    break;
               case USER_TURN_OFF:
                    broadcastState(BluetoothAdapter.STATE_TURNING_OFF);
+1 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ class BluetoothBondState {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
        mContext.registerReceiver(mReceiver, filter);
        readAutoPairingData();
    }

    synchronized void setPendingOutgoingBonding(String address) {
+0 −2
Original line number Diff line number Diff line
@@ -359,12 +359,10 @@ class BluetoothEventLoop {
            Intent intent;
            adapterProperties.setProperty(name, propValues[1]);
            if (propValues[1].equals("true")) {
                mBluetoothService.setIsDiscovering(true);
                intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
            } else {
                // Stop the discovery.
                mBluetoothService.cancelDiscovery();
                mBluetoothService.setIsDiscovering(false);
                intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            }
            mContext.sendBroadcast(intent, BLUETOOTH_PERM);
+20 −17
Original line number Diff line number Diff line
@@ -96,7 +96,6 @@ public class BluetoothService extends IBluetooth.Stub {
    private boolean mIsAirplaneToggleable;
    private BluetoothAdapterStateMachine mBluetoothState;
    private boolean mRestart = false;  // need to call enable() after disable()
    private boolean mIsDiscovering;
    private int[] mAdapterSdpHandles;
    private ParcelUuid[] mAdapterUuids;

@@ -213,8 +212,6 @@ public class BluetoothService extends IBluetooth.Stub {
            disableNative();
        }

        mIsDiscovering = false;

        mBondState = new BluetoothBondState(context, this);
        mAdapterProperties = new BluetoothAdapterProperties(context, this);
        mDeviceProperties = new BluetoothDeviceProperties(this);
@@ -411,7 +408,6 @@ public class BluetoothService extends IBluetooth.Stub {
        intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);

        mIsDiscovering = false;
        mAdapterProperties.clear();
        mServiceRecordToPid.clear();

@@ -482,8 +478,6 @@ public class BluetoothService extends IBluetooth.Stub {
        if (!setupNativeDataNative()) {
            return false;
        }
        mIsDiscovering = false;

        switchConnectable(false);
        updateSdpRecords();
        return true;
@@ -586,17 +580,22 @@ public class BluetoothService extends IBluetooth.Stub {
        }
    }

    /**
     * This method is called immediately before Bluetooth module is turned on after
     * the adapter became pariable.
     * It inits bond state and profile state before STATE_ON intent is broadcasted.
     */
    /*package*/ void initBluetoothAfterTurningOn() {
        mBondState.initBondState();
        initProfileState();
    }

    /**
     * This method is called immediately after Bluetooth module is turned on.
     * It starts auto-connection and places bluetooth on sign onto the battery
     * stats
     */
    /*package*/ void runBluetooth() {
        mIsDiscovering = false;
        mBondState.readAutoPairingData();
        mBondState.initBondState();
        initProfileState();

        autoConnect();

        // Log bluetooth on to battery stats.
@@ -787,12 +786,14 @@ public class BluetoothService extends IBluetooth.Stub {
        }

        if (allowOnlyInOnState) {
            setPropertyBoolean("Pairable", pairable);
            setPropertyBoolean("Discoverable", discoverable);
            setPropertyBoolean("Pairable", pairable);
        } else {
            // allowed to set the property through native layer directly
            setAdapterPropertyBooleanNative("Pairable", pairable ? 1 : 0);
            setAdapterPropertyBooleanNative("Discoverable", discoverable ? 1 : 0);
            // do setting pairable after setting discoverable since the adapter
            // state machine uses pairable event for state change
            setAdapterPropertyBooleanNative("Pairable", pairable ? 1 : 0);
        }
        return true;
    }
@@ -949,11 +950,13 @@ public class BluetoothService extends IBluetooth.Stub {

    public synchronized boolean isDiscovering() {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        return mIsDiscovering;

        String discoveringProperty = mAdapterProperties.getProperty("Discovering");
        if (discoveringProperty == null) {
            return false;
        }

    /* package */ void setIsDiscovering(boolean isDiscovering) {
        mIsDiscovering = isDiscovering;
        return discoveringProperty.equals("true");
    }

    private boolean isBondingFeasible(String address) {