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

Commit e12774d4 authored by Jaikumar Ganesh's avatar Jaikumar Ganesh Committed by Android (Google) Code Review
Browse files

Merge "Add an API call to get the ConnectionState of the Bluetooth Adapter."

parents b0cc50de c53cab20
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -737,6 +737,27 @@ public final class BluetoothAdapter {
        return null;
    }

    /**
     * Get the current connection state of the local Bluetooth adapter.
     * This can be used to check whether the local Bluetooth adapter is connected
     * to any profile of any other remote Bluetooth Device.
     *
     * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED}
     * intent to get the connection state of the adapter.
     *
     * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED},
     * {@link #STATE_CONNECTING} or {@link #STATE_DISCONNECTED}
     *
     * @hide
     */
    public int getConnectionState() {
        if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED;
        try {
            return mService.getAdapterConnectionState();
        } catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);}
        return BluetoothAdapter.STATE_DISCONNECTED;
    }

    /**
     * Picks RFCOMM channels until none are left.
     * Avoids reserved channels.
@@ -879,6 +900,7 @@ public final class BluetoothAdapter {
        return socket;
    }


    /**
     * Construct an unencrypted, unauthenticated, RFCOMM server socket.
     * Call #accept to retrieve connections to this socket.
+2 −0
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ interface IBluetooth
    boolean isDiscovering();
    byte[] readOutOfBandData();

    int getAdapterConnectionState();

    boolean createBond(in String address);
    boolean createBondOutOfBand(in String address, in byte[] hash, in byte[] randomizer);
    boolean cancelBondProcess(in String address);
+13 −3
Original line number Diff line number Diff line
@@ -167,6 +167,8 @@ public class BluetoothService extends IBluetooth.Stub {
    private static String mDockAddress;
    private String mDockPin;

    private int mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED;

    private static class RemoteService {
        public String address;
        public ParcelUuid uuid;
@@ -415,6 +417,7 @@ public class BluetoothService extends IBluetooth.Stub {
        mProfilesConnected = 0;
        mProfilesConnecting = 0;
        mProfilesDisconnecting = 0;
        mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED;

        if (saveSetting) {
            persistBluetoothOnSetting(false);
@@ -2737,11 +2740,18 @@ public class BluetoothService extends IBluetooth.Stub {
        }
    }

    public int getAdapterConnectionState() {
        return mAdapterConnectionState;
    }

    public synchronized void sendConnectionStateChange(BluetoothDevice device, int state,
                                                        int prevState) {
        if (updateCountersAndCheckForConnectionStateChange(device, state, prevState)) {
            state = getAdapterConnectionState(state);
            prevState = getAdapterConnectionState(prevState);
            state = translateToAdapterConnectionState(state);
            prevState = translateToAdapterConnectionState(prevState);

            mAdapterConnectionState = state;

            Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
            intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, state);
@@ -2751,7 +2761,7 @@ public class BluetoothService extends IBluetooth.Stub {
        }
    }

    private int getAdapterConnectionState(int state) {
    private int translateToAdapterConnectionState(int state) {
        switch (state) {
            case BluetoothProfile.STATE_CONNECTING:
                return BluetoothAdapter.STATE_CONNECTING;