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

Change-Id: Icd87d7720189034946aaa98e1a6c5d03ef4219e5
This commit is contained in:
Jaikumar Ganesh
2010-10-26 16:02:26 -07:00
parent ee1dc6221f
commit c53cab20b2
3 changed files with 37 additions and 3 deletions

View File

@@ -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.

View File

@@ -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);

View File

@@ -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;