BluetoothManagerpublic final class BluetoothManager extends Object High level manager used to obtain an instance of an {@link BluetoothAdapter}
and to conduct overall Bluetooth Management.
Use {@link android.content.Context#getSystemService(java.lang.String)}
with {@link Context#BLUETOOTH_SERVICE} to create an {@link BluetoothManager},
then call {@link #getAdapter} to obtain the {@link BluetoothAdapter}.
Alternately, you can just call the static helper
{@link BluetoothAdapter#getDefaultAdapter()}.
Developer Guides
For more information about using BLUETOOTH, read the
Bluetooth developer guide.
|
Fields Summary |
---|
private static final String | TAG | private static final boolean | DBG | private static final boolean | VDBG | private final BluetoothAdapter | mAdapter |
Constructors Summary |
---|
public BluetoothManager(android.content.Context context)
context = context.getApplicationContext();
if (context == null) {
throw new IllegalArgumentException(
"context not associated with any application (using a mock context?)");
}
// Legacy api - getDefaultAdapter does not take in the context
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
Methods Summary |
---|
public BluetoothAdapter | getAdapter()Get the default BLUETOOTH Adapter for this device.
return mAdapter;
| public java.util.List | getConnectedDevices(int profile)Get connected devices for the specified profile.
Return the set of devices which are in state {@link BluetoothProfile#STATE_CONNECTED}
This is not specific to any application configuration but represents
the connection state of Bluetooth for this profile.
This can be used by applications like status bar which would just like
to know the state of Bluetooth.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (DBG) Log.d(TAG,"getConnectedDevices");
if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) {
throw new IllegalArgumentException("Profile not supported: " + profile);
}
List<BluetoothDevice> connectedDevices = new ArrayList<BluetoothDevice>();
try {
IBluetoothManager managerService = mAdapter.getBluetoothManager();
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
if (iGatt == null) return connectedDevices;
connectedDevices = iGatt.getDevicesMatchingConnectionStates(
new int[] { BluetoothProfile.STATE_CONNECTED });
} catch (RemoteException e) {
Log.e(TAG,"",e);
}
return connectedDevices;
| public int | getConnectionState(BluetoothDevice device, int profile)Get the current connection state of the profile to the remote device.
This is not specific to any application configuration but represents
the connection state of the local Bluetooth adapter for certain profile.
This can be used by applications like status bar which would just like
to know the state of Bluetooth.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (DBG) Log.d(TAG,"getConnectionState()");
List<BluetoothDevice> connectedDevices = getConnectedDevices(profile);
for(BluetoothDevice connectedDevice : connectedDevices) {
if (device.equals(connectedDevice)) {
return BluetoothProfile.STATE_CONNECTED;
}
}
return BluetoothProfile.STATE_DISCONNECTED;
| public java.util.List | getDevicesMatchingConnectionStates(int profile, int[] states)Get a list of devices that match any of the given connection
states.
If none of the devices match any of the given states,
an empty list will be returned.
This is not specific to any application configuration but represents
the connection state of the local Bluetooth adapter for this profile.
This can be used by applications like status bar which would just like
to know the state of the local adapter.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (DBG) Log.d(TAG,"getDevicesMatchingConnectionStates");
if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) {
throw new IllegalArgumentException("Profile not supported: " + profile);
}
List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
try {
IBluetoothManager managerService = mAdapter.getBluetoothManager();
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
if (iGatt == null) return devices;
devices = iGatt.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) {
Log.e(TAG,"",e);
}
return devices;
| public BluetoothGattServer | openGattServer(android.content.Context context, BluetoothGattServerCallback callback)Open a GATT Server
The callback is used to deliver results to Caller, such as connection status as well
as the results of any other GATT server operations.
The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
to conduct GATT server operations.
return (openGattServer (context, callback, BluetoothDevice.TRANSPORT_AUTO));
| public BluetoothGattServer | openGattServer(android.content.Context context, BluetoothGattServerCallback callback, int transport)Open a GATT Server
The callback is used to deliver results to Caller, such as connection status as well
as the results of any other GATT server operations.
The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
to conduct GATT server operations.
if (context == null || callback == null) {
throw new IllegalArgumentException("null parameter: " + context + " " + callback);
}
// TODO(Bluetooth) check whether platform support BLE
// Do the check here or in GattServer?
try {
IBluetoothManager managerService = mAdapter.getBluetoothManager();
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
if (iGatt == null) {
Log.e(TAG, "Fail to get GATT Server connection");
return null;
}
BluetoothGattServer mGattServer = new BluetoothGattServer(context, iGatt,transport);
Boolean regStatus = mGattServer.registerCallback(callback);
return regStatus? mGattServer : null;
} catch (RemoteException e) {
Log.e(TAG,"",e);
return null;
}
|
|