public final class BluetoothGattServer extends Object implements BluetoothProfile
This class provides Bluetooth GATT server role functionality, allowing applications to create Bluetooth Smart services and characteristics.
BluetoothGattServer is a proxy object for controlling the Bluetooth Service
via IPC. Use BluetoothManager.openGattServer(android.content.Context, android.bluetooth.BluetoothGattServerCallback)
to get an instance
of this class.
BluetoothProfile.ServiceListener
A2DP, A2DP_SINK, AVRCP_CONTROLLER, EXTRA_PREVIOUS_STATE, EXTRA_STATE, GATT, GATT_SERVER, HEADSET, HEADSET_CLIENT, HEALTH, INPUT_DEVICE, MAP, PAN, PBAP, PBAP_CLIENT, PRIORITY_AUTO_CONNECT, PRIORITY_OFF, PRIORITY_ON, PRIORITY_UNDEFINED, SAP, STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING
Modifier and Type | Method and Description |
---|---|
boolean |
addService(BluetoothGattService service)
Add a service to the list of services to be hosted.
|
void |
cancelConnection(BluetoothDevice device)
Disconnects an established connection, or cancels a connection attempt
currently in progress.
|
void |
clearServices()
Remove all services from the list of provided services.
|
void |
close()
Close this GATT server instance.
|
boolean |
connect(BluetoothDevice device,
boolean autoConnect)
Initiate a connection to a Bluetooth GATT capable device.
|
List<BluetoothDevice> |
getConnectedDevices()
Not supported - please use
BluetoothManager.getConnectedDevices(int)
with BluetoothProfile.GATT as argument |
int |
getConnectionState(BluetoothDevice device)
Not supported - please use
BluetoothManager.getConnectedDevices(int)
with BluetoothProfile.GATT as argument |
List<BluetoothDevice> |
getDevicesMatchingConnectionStates(int[] states)
Not supported - please use
BluetoothManager.getDevicesMatchingConnectionStates(int, int[])
with BluetoothProfile.GATT as first argument |
BluetoothGattService |
getService(UUID uuid)
Returns a
BluetoothGattService from the list of services offered
by this device. |
List<BluetoothGattService> |
getServices()
Returns a list of GATT services offered by this device.
|
boolean |
notifyCharacteristicChanged(BluetoothDevice device,
BluetoothGattCharacteristic characteristic,
boolean confirm)
Send a notification or indication that a local characteristic has been
updated.
|
boolean |
removeService(BluetoothGattService service)
Removes a service from the list of services to be provided.
|
boolean |
sendResponse(BluetoothDevice device,
int requestId,
int status,
int offset,
byte[] value)
Send a response to a read or write request to a remote device.
|
public void close()
public boolean connect(BluetoothDevice device, boolean autoConnect)
The connection may not be established right away, but will be
completed when the remote device is available. A
BluetoothGattServerCallback.onConnectionStateChange(android.bluetooth.BluetoothDevice, int, int)
callback will be
invoked when the connection state changes as a result of this function.
The autoConnect paramter determines whether to actively connect to the remote device, or rather passively scan and finalize the connection when the remote device is in range/available. Generally, the first ever connection to a device should be direct (autoConnect set to false) and subsequent connections to known devices should be invoked with the autoConnect parameter set to true.
Requires android.Manifest.permission#BLUETOOTH
permission.
autoConnect
- Whether to directly connect to the remote device (false)
or to automatically connect as soon as the remote
device becomes available (true).public void cancelConnection(BluetoothDevice device)
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- Remote devicepublic boolean sendResponse(BluetoothDevice device, int requestId, int status, int offset, byte[] value)
This function must be invoked in when a remote read/write request is received by one of these callback methods:
BluetoothGattServerCallback.onCharacteristicReadRequest(android.bluetooth.BluetoothDevice, int, int, android.bluetooth.BluetoothGattCharacteristic)
BluetoothGattServerCallback.onCharacteristicWriteRequest(android.bluetooth.BluetoothDevice, int, android.bluetooth.BluetoothGattCharacteristic, boolean, boolean, int, byte[])
BluetoothGattServerCallback.onDescriptorReadRequest(android.bluetooth.BluetoothDevice, int, int, android.bluetooth.BluetoothGattDescriptor)
BluetoothGattServerCallback.onDescriptorWriteRequest(android.bluetooth.BluetoothDevice, int, android.bluetooth.BluetoothGattDescriptor, boolean, boolean, int, byte[])
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- The remote device to send this response torequestId
- The ID of the request that was received with the callbackstatus
- The status of the request to be sent to the remote devicesoffset
- Value offset for partial read/write responsevalue
- The value of the attribute that was read/written (optional)public boolean notifyCharacteristicChanged(BluetoothDevice device, BluetoothGattCharacteristic characteristic, boolean confirm)
A notification or indication is sent to the remote device to signal that the characteristic has been updated. This function should be invoked for every client that requests notifications/indications by writing to the "Client Configuration" descriptor for the given characteristic.
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- The remote device to receive the notification/indicationcharacteristic
- The local characteristic that has been updatedconfirm
- true to request confirmation from the client (indication),
false to send a notificationIllegalArgumentException
public boolean addService(BluetoothGattService service)
Once a service has been addded to the the list, the service and its included characteristics will be provided by the local device.
If the local device has already exposed services when this function is called, a service update notification will be sent to all clients.
Requires android.Manifest.permission#BLUETOOTH
permission.
service
- Service to be added to the list of services provided
by this device.public boolean removeService(BluetoothGattService service)
Requires android.Manifest.permission#BLUETOOTH
permission.
service
- Service to be removed.public void clearServices()
Requires android.Manifest.permission#BLUETOOTH
permission.
public List<BluetoothGattService> getServices()
An application must call addService(android.bluetooth.BluetoothGattService)
to add a serice to the
list of services offered by this device.
Requires android.Manifest.permission#BLUETOOTH
permission.
public BluetoothGattService getService(UUID uuid)
BluetoothGattService
from the list of services offered
by this device.
If multiple instances of the same service (as identified by UUID) exist, the first instance of the service is returned.
Requires android.Manifest.permission#BLUETOOTH
permission.
uuid
- UUID of the requested servicepublic int getConnectionState(BluetoothDevice device)
BluetoothManager.getConnectedDevices(int)
with BluetoothProfile.GATT
as argumentgetConnectionState
in interface BluetoothProfile
device
- Remote bluetooth device.BluetoothProfile.STATE_CONNECTED
, BluetoothProfile.STATE_CONNECTING
,
BluetoothProfile.STATE_DISCONNECTED
, BluetoothProfile.STATE_DISCONNECTING
UnsupportedOperationException
public List<BluetoothDevice> getConnectedDevices()
BluetoothManager.getConnectedDevices(int)
with BluetoothProfile.GATT
as argumentgetConnectedDevices
in interface BluetoothProfile
UnsupportedOperationException
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states)
BluetoothManager.getDevicesMatchingConnectionStates(int, int[])
with BluetoothProfile.GATT
as first argumentgetDevicesMatchingConnectionStates
in interface BluetoothProfile
states
- Array of states. States can be one of
BluetoothProfile.STATE_CONNECTED
, BluetoothProfile.STATE_CONNECTING
,
BluetoothProfile.STATE_DISCONNECTED
, BluetoothProfile.STATE_DISCONNECTING
,UnsupportedOperationException