Added in API level 18

BluetoothGattServer

class BluetoothGattServer : BluetoothProfile
kotlin.Any
   ↳ android.bluetooth.BluetoothGattServer

Public API for the Bluetooth GATT Profile server role.

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 to get an instance of this class.

Summary

Inherited constants
Public methods
Boolean

Add a service to the list of services to be hosted.

Unit

Disconnects an established connection, or cancels a connection attempt currently in progress.

Unit

Remove all services from the list of provided services.

Unit

Close this GATT server instance.

Boolean
connect(device: BluetoothDevice!, autoConnect: Boolean)

Initiate a connection to a Bluetooth GATT capable device.

MutableList<BluetoothDevice!>!

Not supported - please use BluetoothManager#getConnectedDevices(int) with android.bluetooth.BluetoothProfile#GATT as argument

Int

Not supported - please use BluetoothManager#getConnectedDevices(int) with android.bluetooth.BluetoothProfile#GATT as argument

MutableList<BluetoothDevice!>!

Not supported - please use BluetoothManager#getDevicesMatchingConnectionStates(int, with BluetoothProfile#GATT as first argument

BluetoothGattService!
getService(uuid: UUID!)

Returns a BluetoothGattService from the list of services offered by this device.

MutableList<BluetoothGattService!>!

Returns a list of GATT services offered by this device.

Boolean

Send a notification or indication that a local characteristic has been updated.

Int

Send a notification or indication that a local characteristic has been updated.

Unit

Read the current transmitter PHY and receiver PHY of the connection.

Boolean

Removes a service from the list of services to be provided.

Boolean
sendResponse(device: BluetoothDevice!, requestId: Int, status: Int, offset: Int, value: ByteArray!)

Send a response to a read or write request to a remote device.

Unit
setPreferredPhy(device: BluetoothDevice!, txPhy: Int, rxPhy: Int, phyOptions: Int)

Set the preferred connection PHY for this app.

Public methods

addService

Added in API level 18
fun addService(service: BluetoothGattService!): Boolean

Add a service to the list of services to be hosted.

Once a service has been added to 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.

The BluetoothGattServerCallback#onServiceAdded callback will indicate whether this service has been added successfully. Do not add another service before this callback.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
service BluetoothGattService!: Service to be added to the list of services provided by this device.
Return
Boolean true, if the request to add service has been initiated

cancelConnection

Added in API level 18
fun cancelConnection(device: BluetoothDevice!): Unit

Disconnects an established connection, or cancels a connection attempt currently in progress.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
device BluetoothDevice!: Remote device

clearServices

Added in API level 18
fun clearServices(): Unit

Remove all services from the list of provided services.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

close

Added in API level 18
fun close(): Unit

Close this GATT server instance.

Application should call this method as early as possible after it is done with this GATT server.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

connect

Added in API level 18
fun connect(
    device: BluetoothDevice!,
    autoConnect: Boolean
): Boolean

Initiate a connection to a Bluetooth GATT capable device.

The connection may not be established right away, but will be completed when the remote device is available. A BluetoothGattServerCallback#onConnectionStateChange callback will be invoked when the connection state changes as a result of this function.

The autoConnect parameter 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.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
autoConnect Boolean: Whether to directly connect to the remote device (false) or to automatically connect as soon as the remote device becomes available (true).
Return
Boolean true, if the connection attempt was initiated successfully

getConnectedDevices

Added in API level 18
fun getConnectedDevices(): MutableList<BluetoothDevice!>!

Not supported - please use BluetoothManager#getConnectedDevices(int) with android.bluetooth.BluetoothProfile#GATT as argument

Return
MutableList<BluetoothDevice!>! List of devices. The list will be empty on error.
Exceptions
java.lang.UnsupportedOperationException on every call

getConnectionState

Added in API level 18
fun getConnectionState(device: BluetoothDevice!): Int

Not supported - please use BluetoothManager#getConnectedDevices(int) with android.bluetooth.BluetoothProfile#GATT as argument

Parameters
device BluetoothDevice!: Remote bluetooth device.
Return
Int State of the profile connection. One of STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING Value is android.bluetooth.BluetoothProfile#STATE_DISCONNECTED, android.bluetooth.BluetoothProfile#STATE_CONNECTING, android.bluetooth.BluetoothProfile#STATE_CONNECTED, or android.bluetooth.BluetoothProfile#STATE_DISCONNECTING
Exceptions
java.lang.UnsupportedOperationException on every call

getDevicesMatchingConnectionStates

Added in API level 18
fun getDevicesMatchingConnectionStates(states: IntArray!): MutableList<BluetoothDevice!>!

Not supported - please use BluetoothManager#getDevicesMatchingConnectionStates(int, with BluetoothProfile#GATT as first argument

Parameters
states IntArray!: Array of states. States can be one of STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING,
Return
MutableList<BluetoothDevice!>! List of devices. The list will be empty on error.
Exceptions
java.lang.UnsupportedOperationException on every call

getService

Added in API level 18
fun getService(uuid: UUID!): BluetoothGattService!

Returns a 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.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.

Parameters
uuid UUID!: UUID of the requested service
Return
BluetoothGattService! BluetoothGattService if supported, or null if the requested service is not offered by this device.

getServices

Added in API level 18
fun getServices(): MutableList<BluetoothGattService!>!

Returns a list of GATT services offered by this device.

An application must call addService to add a service to the list of services offered by this device.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.

Return
MutableList<BluetoothGattService!>! List of services. Returns an empty list if no services have been added yet.

notifyCharacteristicChanged

Added in API level 18
Deprecated in API level 33
fun notifyCharacteristicChanged(
    device: BluetoothDevice!,
    characteristic: BluetoothGattCharacteristic!,
    confirm: Boolean
): Boolean

Deprecated: Use BluetoothGattServer#notifyCharacteristicChanged(BluetoothDevice, as this is not memory safe.

Send a notification or indication that a local characteristic has been updated.

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.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
device BluetoothDevice!: The remote device to receive the notification/indication
characteristic BluetoothGattCharacteristic!: The local characteristic that has been updated
confirm Boolean: true to request confirmation from the client (indication), false to send a notification
Return
Boolean true, if the notification has been triggered successfully
Exceptions
java.lang.IllegalArgumentException if the characteristic value or service is null

notifyCharacteristicChanged

Added in API level 33
fun notifyCharacteristicChanged(
    device: BluetoothDevice,
    characteristic: BluetoothGattCharacteristic,
    confirm: Boolean,
    value: ByteArray
): Int

Send a notification or indication that a local characteristic has been updated.

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.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
device BluetoothDevice: the remote device to receive the notification/indication This value cannot be null.
characteristic BluetoothGattCharacteristic: the local characteristic that has been updated This value cannot be null.
confirm Boolean: true to request confirmation from the client (indication) or false to send a notification
value ByteArray: the characteristic value This value cannot be null.
Return
Int whether the notification has been triggered successfully Value is android.bluetooth.BluetoothStatusCodes#SUCCESS, android.bluetooth.BluetoothStatusCodes#ERROR_MISSING_BLUETOOTH_CONNECT_PERMISSION, android.bluetooth.BluetoothStatusCodes.ERROR_DEVICE_NOT_CONNECTED, android.bluetooth.BluetoothStatusCodes#ERROR_PROFILE_SERVICE_NOT_BOUND, or android.bluetooth.BluetoothStatusCodes#ERROR_UNKNOWN
Exceptions
java.lang.IllegalArgumentException if the characteristic value or service is null

readPhy

Added in API level 26
fun readPhy(device: BluetoothDevice!): Unit

Read the current transmitter PHY and receiver PHY of the connection. The values are returned in BluetoothGattServerCallback#onPhyRead
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
device BluetoothDevice!: The remote device to send this response to

removeService

Added in API level 18
fun removeService(service: BluetoothGattService!): Boolean

Removes a service from the list of services to be provided.
For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
service BluetoothGattService!: Service to be removed.
Return
Boolean true, if the service has been removed

sendResponse

Added in API level 18
fun sendResponse(
    device: BluetoothDevice!,
    requestId: Int,
    status: Int,
    offset: Int,
    value: ByteArray!
): Boolean

Send a response to a read or write request to a remote device.

This function must be invoked in when a remote read/write request is received by one of these callback methods:


For apps targeting Build.VERSION_CODES#R or lower, this requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
Parameters
device BluetoothDevice!: The remote device to send this response to
requestId Int: The ID of the request that was received with the callback
status Int: The status of the request to be sent to the remote devices
offset Int: Value offset for partial read/write response
value ByteArray!: The value of the attribute that was read/written (optional)

setPreferredPhy

Added in API level 26
fun setPreferredPhy(
    device: BluetoothDevice!,
    txPhy: Int,
    rxPhy: Int,
    phyOptions: Int
): Unit

Set the preferred connection PHY for this app. Please note that this is just a recommendation, whether the PHY change will happen depends on other applications preferences, local and remote controller capabilities. Controller can override these settings.

BluetoothGattServerCallback#onPhyUpdate will be triggered as a result of this call, even if no PHY change happens. It is also triggered when remote device updates the PHY.
For apps targeting Build.VERSION_CODES#S or or higher, this requires the Manifest.permission#BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT

Parameters
device BluetoothDevice!: The remote device to send this response to
txPhy Int: preferred transmitter PHY. Bitwise OR of any of android.bluetooth.BluetoothDevice#PHY_LE_1M_MASK, BluetoothDevice#PHY_LE_2M_MASK, and android.bluetooth.BluetoothDevice#PHY_LE_CODED_MASK.
rxPhy Int: preferred receiver PHY. Bitwise OR of any of android.bluetooth.BluetoothDevice#PHY_LE_1M_MASK, BluetoothDevice#PHY_LE_2M_MASK, and android.bluetooth.BluetoothDevice#PHY_LE_CODED_MASK.
phyOptions Int: preferred coding to use when transmitting on the LE Coded PHY. Can be one of BluetoothDevice#PHY_OPTION_NO_PREFERRED, BluetoothDevice#PHY_OPTION_S2 or BluetoothDevice#PHY_OPTION_S8