diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index c66b2dec9e28d..556fb10fb9ca9 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -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. + * + *

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. diff --git a/core/java/android/bluetooth/IBluetooth.aidl b/core/java/android/bluetooth/IBluetooth.aidl index 1eb269dd57210..aefb3f296ee88 100644 --- a/core/java/android/bluetooth/IBluetooth.aidl +++ b/core/java/android/bluetooth/IBluetooth.aidl @@ -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); diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index 65047c0159888..72890120914a5 100644 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -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;