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

Commit 96a79830 authored by Jaikumar Ganesh's avatar Jaikumar Ganesh
Browse files

Update code for new BT public APIs.

Change-Id: Id730fb6226db59f3a0416111c4790653c2fccb0b
parent c9cc487b
Loading
Loading
Loading
Loading
+57 −50
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.os.Message;
import android.os.Message;
import android.bluetooth.BluetoothAdapter;
import android.server.BluetoothA2dpService;
import android.server.BluetoothA2dpService;
import android.server.BluetoothService;
import android.server.BluetoothService;
import android.util.Log;
import android.util.Log;
@@ -28,6 +29,8 @@ import android.util.Log;
import com.android.internal.util.HierarchicalState;
import com.android.internal.util.HierarchicalState;
import com.android.internal.util.HierarchicalStateMachine;
import com.android.internal.util.HierarchicalStateMachine;


import java.util.Set;

/**
/**
 * This class is the Profile connection state machine associated with a remote
 * This class is the Profile connection state machine associated with a remote
 * device. When the device bonds an instance of this class is created.
 * device. When the device bonds an instance of this class is created.
@@ -91,12 +94,11 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
    private BluetoothService mService;
    private BluetoothService mService;
    private BluetoothA2dpService mA2dpService;
    private BluetoothA2dpService mA2dpService;
    private BluetoothHeadset  mHeadsetService;
    private BluetoothHeadset  mHeadsetService;
    private boolean mHeadsetServiceConnected;


    private BluetoothDevice mDevice;
    private BluetoothDevice mDevice;
    private int mHeadsetState;
    private int mHeadsetState = BluetoothProfile.STATE_DISCONNECTED;
    private int mA2dpState;
    private int mA2dpState = BluetoothProfile.STATE_DISCONNECTED;
    private int mHidState;
    private int mHidState = BluetoothProfile.STATE_DISCONNECTED;


    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        @Override
@@ -105,32 +107,29 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (!device.equals(mDevice)) return;
            if (!device.equals(mDevice)) return;


            if (action.equals(BluetoothHeadset.ACTION_STATE_CHANGED)) {
            if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
                int newState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0);
                int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
                int oldState = intent.getIntExtra(BluetoothHeadset.EXTRA_PREVIOUS_STATE, 0);
                int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
                int initiator = intent.getIntExtra(
                    BluetoothHeadset.EXTRA_DISCONNECT_INITIATOR,
                    BluetoothHeadset.LOCAL_DISCONNECT);
                mHeadsetState = newState;
                if (newState == BluetoothHeadset.STATE_DISCONNECTED &&
                    initiator == BluetoothHeadset.REMOTE_DISCONNECT) {
                    sendMessage(DISCONNECT_HFP_INCOMING);
                }
                if (newState == BluetoothHeadset.STATE_CONNECTED ||
                    newState == BluetoothHeadset.STATE_DISCONNECTED) {
                    sendMessage(TRANSITION_TO_STABLE);
                }
            } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
                int newState = intent.getIntExtra(BluetoothA2dp.EXTRA_SINK_STATE, 0);
                int oldState = intent.getIntExtra(BluetoothA2dp.EXTRA_PREVIOUS_SINK_STATE, 0);
                mA2dpState = newState;
                mA2dpState = newState;
                if ((oldState == BluetoothA2dp.STATE_CONNECTED ||
                if (oldState == BluetoothA2dp.STATE_CONNECTED &&
                           oldState == BluetoothA2dp.STATE_PLAYING) &&
                    newState == BluetoothA2dp.STATE_DISCONNECTED) {
                    newState == BluetoothA2dp.STATE_DISCONNECTED) {
                    sendMessage(DISCONNECT_A2DP_INCOMING);
                    sendMessage(DISCONNECT_A2DP_INCOMING);
                }
                }
                if (newState == BluetoothA2dp.STATE_CONNECTED ||
                if (newState == BluetoothProfile.STATE_CONNECTED ||
                    newState == BluetoothA2dp.STATE_DISCONNECTED) {
                    newState == BluetoothProfile.STATE_DISCONNECTED) {
                    sendMessage(TRANSITION_TO_STABLE);
                }
            } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
                int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
                int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);

                mHeadsetState = newState;
                if (oldState == BluetoothHeadset.STATE_CONNECTED &&
                    newState == BluetoothHeadset.STATE_DISCONNECTED) {
                    sendMessage(DISCONNECT_HFP_INCOMING);
                }
                if (newState == BluetoothProfile.STATE_CONNECTED ||
                    newState == BluetoothProfile.STATE_DISCONNECTED) {
                    sendMessage(TRANSITION_TO_STABLE);
                    sendMessage(TRANSITION_TO_STABLE);
                }
                }
            } else if (action.equals(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED)) {
            } else if (action.equals(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED)) {
@@ -192,31 +191,31 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine


        IntentFilter filter = new IntentFilter();
        IntentFilter filter = new IntentFilter();
        // Fine-grained state broadcasts
        // Fine-grained state broadcasts
        filter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED);
        filter.addAction(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED);


        mContext.registerReceiver(mBroadcastReceiver, filter);
        mContext.registerReceiver(mBroadcastReceiver, filter);


        HeadsetServiceListener l = new HeadsetServiceListener();
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
                                BluetoothProfile.HEADSET);
    }
    }


    private class HeadsetServiceListener implements BluetoothHeadset.ServiceListener {
    private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
        public HeadsetServiceListener() {
        new BluetoothProfile.ServiceListener() {
            mHeadsetService = new BluetoothHeadset(mContext, this);
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
        }
        public void onServiceConnected() {
            synchronized(BluetoothDeviceProfileState.this) {
            synchronized(BluetoothDeviceProfileState.this) {
                mHeadsetServiceConnected = true;
                mHeadsetService = (BluetoothHeadset) proxy;
            }
            }
        }
        }
        public void onServiceDisconnected() {
        public void onServiceDisconnected(int profile) {
            synchronized(BluetoothDeviceProfileState.this) {
            synchronized(BluetoothDeviceProfileState.this) {
                mHeadsetServiceConnected = false;
                mHeadsetService = null;
            }
            }
            }
        }
        }
    };


    private class BondedDevice extends HierarchicalState {
    private class BondedDevice extends HierarchicalState {
        @Override
        @Override
@@ -276,19 +275,25 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
                    if (isPhoneDocked(mDevice)) {
                    if (isPhoneDocked(mDevice)) {
                        // Don't auto connect to docks.
                        // Don't auto connect to docks.
                        break;
                        break;
                    } else if (!mHeadsetServiceConnected) {
                    } else if (mHeadsetService == null) {
                        deferMessage(message);
                        deferMessage(message);
                    } else {
                    } else {
                        if (mHeadsetService.getPriority(mDevice) ==
                        if (mHeadsetService.getPriority(mDevice) ==
                              BluetoothHeadset.PRIORITY_AUTO_CONNECT &&
                              BluetoothHeadset.PRIORITY_AUTO_CONNECT &&
                              !mHeadsetService.isConnected(mDevice)) {
                              mHeadsetService.getDevicesMatchingConnectionStates(
                            mHeadsetService.connectHeadset(mDevice);
                                  new int[] {BluetoothProfile.STATE_CONNECTED,
                                             BluetoothProfile.STATE_CONNECTING,
                                             BluetoothProfile.STATE_DISCONNECTING}).size() == 0) {
                            mHeadsetService.connect(mDevice);
                        }
                        }
                        if (mA2dpService != null &&
                        if (mA2dpService != null &&
                              mA2dpService.getSinkPriority(mDevice) ==
                              mA2dpService.getPriority(mDevice) ==
                              BluetoothA2dp.PRIORITY_AUTO_CONNECT &&
                              BluetoothA2dp.PRIORITY_AUTO_CONNECT &&
                              mA2dpService.getConnectedSinks().length == 0) {
                              mA2dpService.getDevicesMatchingConnectionStates(
                            mA2dpService.connectSink(mDevice);
                                  new int[] {BluetoothA2dp.STATE_CONNECTED,
                                             BluetoothProfile.STATE_CONNECTING,
                                             BluetoothProfile.STATE_DISCONNECTING}).length == 0) {
                            mA2dpService.connect(mDevice);
                        }
                        }
                        if (mService.getInputDevicePriority(mDevice) ==
                        if (mService.getInputDevicePriority(mDevice) ==
                              BluetoothInputDevice.PRIORITY_AUTO_CONNECT) {
                              BluetoothInputDevice.PRIORITY_AUTO_CONNECT) {
@@ -805,7 +810,7 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
    synchronized void cancelCommand(int command) {
    synchronized void cancelCommand(int command) {
        if (command == CONNECT_HFP_OUTGOING ) {
        if (command == CONNECT_HFP_OUTGOING ) {
            // Cancel the outgoing thread.
            // Cancel the outgoing thread.
            if (mHeadsetServiceConnected) {
            if (mHeadsetService != null) {
                mHeadsetService.cancelConnectThread();
                mHeadsetService.cancelConnectThread();
            }
            }
            // HeadsetService is down. Phone process most likely crashed.
            // HeadsetService is down. Phone process most likely crashed.
@@ -823,12 +828,14 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
        log("Processing command:" + command);
        log("Processing command:" + command);
        switch(command) {
        switch(command) {
            case  CONNECT_HFP_OUTGOING:
            case  CONNECT_HFP_OUTGOING:
                if (mHeadsetService != null) {
                if (mHeadsetService == null) {
                    deferHeadsetMessage(command);
                } else {
                    return mHeadsetService.connectHeadsetInternal(mDevice);
                    return mHeadsetService.connectHeadsetInternal(mDevice);
                }
                }
                break;
                break;
            case CONNECT_HFP_INCOMING:
            case CONNECT_HFP_INCOMING:
                if (!mHeadsetServiceConnected) {
                if (mHeadsetService == null) {
                    deferHeadsetMessage(command);
                    deferHeadsetMessage(command);
                } else if (mHeadsetState == BluetoothHeadset.STATE_CONNECTING) {
                } else if (mHeadsetState == BluetoothHeadset.STATE_CONNECTING) {
                    return mHeadsetService.acceptIncomingConnect(mDevice);
                    return mHeadsetService.acceptIncomingConnect(mDevice);
@@ -849,7 +856,7 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
            case CONNECT_HID_INCOMING:
            case CONNECT_HID_INCOMING:
                return true;
                return true;
            case DISCONNECT_HFP_OUTGOING:
            case DISCONNECT_HFP_OUTGOING:
                if (!mHeadsetServiceConnected) {
                if (mHeadsetService == null) {
                    deferHeadsetMessage(command);
                    deferHeadsetMessage(command);
                } else {
                } else {
                    if (mHeadsetService.getPriority(mDevice) ==
                    if (mHeadsetService.getPriority(mDevice) ==
@@ -867,9 +874,9 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine
                return true;
                return true;
            case DISCONNECT_A2DP_OUTGOING:
            case DISCONNECT_A2DP_OUTGOING:
                if (mA2dpService != null) {
                if (mA2dpService != null) {
                    if (mA2dpService.getSinkPriority(mDevice) ==
                    if (mA2dpService.getPriority(mDevice) ==
                        BluetoothA2dp.PRIORITY_AUTO_CONNECT) {
                        BluetoothA2dp.PRIORITY_AUTO_CONNECT) {
                        mA2dpService.setSinkPriority(mDevice, BluetoothHeadset.PRIORITY_ON);
                        mA2dpService.setPriority(mDevice, BluetoothHeadset.PRIORITY_ON);
                    }
                    }
                    return mA2dpService.disconnectSinkInternal(mDevice);
                    return mA2dpService.disconnectSinkInternal(mDevice);
                }
                }
+10 −10
Original line number Original line Diff line number Diff line
@@ -59,16 +59,16 @@ public class BluetoothProfileState extends HierarchicalStateMachine {
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String action = intent.getAction();


            if (action.equals(BluetoothHeadset.ACTION_STATE_CHANGED)) {
            if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
                int newState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0);
                int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
                if (mProfile == HFP && (newState == BluetoothHeadset.STATE_CONNECTED ||
                if (mProfile == HFP && (newState == BluetoothProfile.STATE_CONNECTED ||
                    newState == BluetoothHeadset.STATE_DISCONNECTED)) {
                    newState == BluetoothProfile.STATE_DISCONNECTED)) {
                    sendMessage(TRANSITION_TO_STABLE);
                    sendMessage(TRANSITION_TO_STABLE);
                }
                }
            } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
            } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
                int newState = intent.getIntExtra(BluetoothA2dp.EXTRA_SINK_STATE, 0);
                int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
                if (mProfile == A2DP && (newState == BluetoothA2dp.STATE_CONNECTED ||
                if (mProfile == A2DP && (newState == BluetoothProfile.STATE_CONNECTED ||
                    newState == BluetoothA2dp.STATE_DISCONNECTED)) {
                    newState == BluetoothProfile.STATE_DISCONNECTED)) {
                    sendMessage(TRANSITION_TO_STABLE);
                    sendMessage(TRANSITION_TO_STABLE);
                }
                }
            } else if (action.equals(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED)) {
            } else if (action.equals(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED)) {
@@ -89,8 +89,8 @@ public class BluetoothProfileState extends HierarchicalStateMachine {
        setInitialState(mStableState);
        setInitialState(mStableState);


        IntentFilter filter = new IntentFilter();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED);
        filter.addAction(BluetoothInputDevice.ACTION_INPUT_DEVICE_STATE_CHANGED);
        context.registerReceiver(mBroadcastReceiver, filter);
        context.registerReceiver(mBroadcastReceiver, filter);
    }
    }
+14 −10
Original line number Original line Diff line number Diff line
@@ -19,21 +19,25 @@ package android.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;


/**
/**
 * System private API for Bluetooth A2DP service
 * APIs for Bluetooth A2DP service
 *
 *
 * {@hide}
 * @hide
 */
 */
interface IBluetoothA2dp {
interface IBluetoothA2dp {
    boolean connectSink(in BluetoothDevice device);
    // Public API
    boolean disconnectSink(in BluetoothDevice device);
    boolean connect(in BluetoothDevice device);
    boolean disconnect(in BluetoothDevice device);
    // change to Set<> once AIDL supports
    BluetoothDevice[] getConnectedDevices();
    BluetoothDevice[] getDevicesMatchingConnectionStates(in int[] states);
    int getConnectionState(in BluetoothDevice device);
    boolean setPriority(in BluetoothDevice device, int priority);
    int getPriority(in BluetoothDevice device);
    boolean isA2dpPlaying(in BluetoothDevice device);

    // Internal APIs
    boolean suspendSink(in BluetoothDevice device);
    boolean suspendSink(in BluetoothDevice device);
    boolean resumeSink(in BluetoothDevice device);
    boolean resumeSink(in BluetoothDevice device);
    BluetoothDevice[] getConnectedSinks();  // change to Set<> once AIDL supports
    BluetoothDevice[] getNonDisconnectedSinks();  // change to Set<> once AIDL supports
    int getSinkState(in BluetoothDevice device);
    boolean setSinkPriority(in BluetoothDevice device, int priority);
    int getSinkPriority(in BluetoothDevice device);

    boolean connectSinkInternal(in BluetoothDevice device);
    boolean connectSinkInternal(in BluetoothDevice device);
    boolean disconnectSinkInternal(in BluetoothDevice device);
    boolean disconnectSinkInternal(in BluetoothDevice device);
}
}
+16 −9
Original line number Original line Diff line number Diff line
@@ -19,25 +19,32 @@ package android.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;


/**
/**
 * System private API for Bluetooth Headset service
 * API for Bluetooth Headset service
 *
 *
 * {@hide}
 * {@hide}
 */
 */
interface IBluetoothHeadset {
interface IBluetoothHeadset {
    int getState(in BluetoothDevice device);
    // Public API
    BluetoothDevice getCurrentHeadset();
    boolean connect(in BluetoothDevice device);
    boolean connectHeadset(in BluetoothDevice device);
    boolean disconnect(in BluetoothDevice device);
    void disconnectHeadset(in BluetoothDevice device);
    // Change to Set<> when AIDL supports
    boolean isConnected(in BluetoothDevice device);
    BluetoothDevice[] getConnectedDevices();
    boolean startVoiceRecognition();
    BluetoothDevice[] getDevicesMatchingConnectionStates(in int[] states);
    boolean stopVoiceRecognition();
    int getConnectionState(in BluetoothDevice device);
    boolean setPriority(in BluetoothDevice device, int priority);
    boolean setPriority(in BluetoothDevice device, int priority);
    int getPriority(in BluetoothDevice device);
    int getPriority(in BluetoothDevice device);
    int getBatteryUsageHint();
    boolean startVoiceRecognition(in BluetoothDevice device);
    boolean stopVoiceRecognition(in BluetoothDevice device);
    boolean isAudioConnected(in BluetoothDevice device);


    // APIs that can be made public in future
    int getBatteryUsageHint(in BluetoothDevice device);

    // Internal functions, not be made public
    boolean createIncomingConnect(in BluetoothDevice device);
    boolean createIncomingConnect(in BluetoothDevice device);
    boolean acceptIncomingConnect(in BluetoothDevice device);
    boolean acceptIncomingConnect(in BluetoothDevice device);
    boolean cancelConnectThread();
    boolean cancelConnectThread();
    boolean connectHeadsetInternal(in BluetoothDevice device);
    boolean connectHeadsetInternal(in BluetoothDevice device);
    boolean disconnectHeadsetInternal(in BluetoothDevice device);
    boolean disconnectHeadsetInternal(in BluetoothDevice device);
    boolean setAudioState(in BluetoothDevice device, int state);
}
}
+88 −73

File changed.

Preview size limit exceeded, changes collapsed.

Loading