FileDocCategorySizeDatePackage
BluetoothManager.javaAPI DocAndroid 5.1 API9629Thu Mar 12 22:22:10 GMT 2015android.bluetooth

BluetoothManager

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

see
Context#getSystemService
see
BluetoothAdapter#getDefaultAdapter()

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)

hide


          
       
        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 BluetoothAdaptergetAdapter()
Get the default BLUETOOTH Adapter for this device.

return
the default BLUETOOTH Adapter

        return mAdapter;
    
public java.util.ListgetConnectedDevices(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.

param
profile GATT or GATT_SERVER
return
List of devices. The list will be empty on error.

        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 intgetConnectionState(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.

param
device Remote bluetooth device.
param
profile GATT or GATT_SERVER
return
State of the profile connection. One of {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, {@link BluetoothProfile#STATE_DISCONNECTING}

        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.ListgetDevicesMatchingConnectionStates(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.

param
profile GATT or GATT_SERVER
param
states Array of states. States can be one of {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, {@link BluetoothProfile#STATE_DISCONNECTING},
return
List of devices. The list will be empty on error.

        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 BluetoothGattServeropenGattServer(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.

param
context App context
param
callback GATT server callback handler that will receive asynchronous callbacks.
return
BluetoothGattServer instance


        return (openGattServer (context, callback, BluetoothDevice.TRANSPORT_AUTO));
    
public BluetoothGattServeropenGattServer(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.

param
context App context
param
callback GATT server callback handler that will receive asynchronous callbacks.
param
transport preferred transport for GATT connections to remote dual-mode devices {@link BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE}
return
BluetoothGattServer instance
hide

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