java.lang.Object | |
↳ | android.bluetooth.BluetoothGatt |
Public API for the Bluetooth GATT Profile.
This class provides Bluetooth GATT functionality to enable communication with Bluetooth Smart or Smart Ready devices.
To connect to a remote peripheral device, create a BluetoothGattCallback
and call connectGatt(Context, boolean, BluetoothGattCallback)
to get a instance of this class.
GATT capable devices can be discovered using the Bluetooth device discovery or BLE
scan process.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | CONNECTION_PRIORITY_BALANCED | Connection paramter update - Use the connection paramters recommended by the Bluetooth SIG. | |||||||||
int | CONNECTION_PRIORITY_HIGH | Connection paramter update - Request a high priority, low latency connection. | |||||||||
int | CONNECTION_PRIORITY_LOW_POWER | Connection paramter update - Request low power, reduced data rate connection parameters. | |||||||||
int | GATT_CONNECTION_CONGESTED | A remote device connection is congested. | |||||||||
int | GATT_FAILURE | A GATT operation failed, errors other than the above | |||||||||
int | GATT_INSUFFICIENT_AUTHENTICATION | Insufficient authentication for a given operation | |||||||||
int | GATT_INSUFFICIENT_ENCRYPTION | Insufficient encryption for a given operation | |||||||||
int | GATT_INVALID_ATTRIBUTE_LENGTH | A write operation exceeds the maximum length of the attribute | |||||||||
int | GATT_INVALID_OFFSET | A read or write operation was requested with an invalid offset | |||||||||
int | GATT_READ_NOT_PERMITTED | GATT read operation is not permitted | |||||||||
int | GATT_REQUEST_NOT_SUPPORTED | The given request is not supported | |||||||||
int | GATT_SUCCESS | A GATT operation completed successfully | |||||||||
int | GATT_WRITE_NOT_PERMITTED | GATT write operation is not permitted |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From interface
android.bluetooth.BluetoothProfile
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Cancels a reliable write transaction for a given device.
| |||||||||||
This method was deprecated
in API level 19.
Use
abortReliableWrite()
| |||||||||||
Initiates a reliable write transaction for a given remote device.
| |||||||||||
Close this Bluetooth GATT client.
| |||||||||||
Connect back to remote device.
| |||||||||||
Disconnects an established connection, or cancels a connection attempt
currently in progress.
| |||||||||||
Discovers services offered by a remote device as well as their
characteristics and descriptors.
| |||||||||||
Executes a reliable write transaction for a given remote device.
| |||||||||||
Not supported - please use
getConnectedDevices(int)
with GATT as argument
| |||||||||||
Not supported - please use
getConnectedDevices(int)
with GATT as argument
| |||||||||||
Return the remote bluetooth device this GATT client targets to
| |||||||||||
Not supported - please use
getDevicesMatchingConnectionStates(int, int[])
with GATT as first argument
| |||||||||||
Returns a
BluetoothGattService , if the requested UUID is
supported by the remote device.
| |||||||||||
Returns a list of GATT services offered by the remote device.
| |||||||||||
Reads the requested characteristic from the associated remote device.
| |||||||||||
Reads the value for a given descriptor from the associated remote device.
| |||||||||||
Read the RSSI for a connected remote device.
| |||||||||||
Request a connection parameter update.
| |||||||||||
Request an MTU size used for a given connection.
| |||||||||||
Enable or disable notifications/indications for a given characteristic.
| |||||||||||
Writes a given characteristic and its values to the associated remote device.
| |||||||||||
Write the value of a given descriptor to the associated remote device.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
android.bluetooth.BluetoothProfile
|
Connection paramter update - Use the connection paramters recommended by the Bluetooth SIG. This is the default value if no connection parameter update is requested.
Connection paramter update - Request a high priority, low latency connection.
An application should only request high priority connection paramters to transfer
large amounts of data over LE quickly. Once the transfer is complete, the application
should request CONNECTION_PRIORITY_BALANCED
connectoin parameters
to reduce energy use.
Connection paramter update - Request low power, reduced data rate connection parameters.
A remote device connection is congested.
A GATT operation failed, errors other than the above
Insufficient authentication for a given operation
Insufficient encryption for a given operation
A write operation exceeds the maximum length of the attribute
A read or write operation was requested with an invalid offset
GATT read operation is not permitted
The given request is not supported
A GATT operation completed successfully
GATT write operation is not permitted
Cancels a reliable write transaction for a given device.
Calling this function will discard all queued characteristic write operations for a given remote device.
Requires BLUETOOTH
permission.
Initiates a reliable write transaction for a given remote device.
Once a reliable write transaction has been initiated, all calls
to writeCharacteristic(BluetoothGattCharacteristic)
are sent to the remote device for
verification and queued up for atomic execution. The application will
receive an onCharacteristicWrite(BluetoothGatt, BluetoothGattCharacteristic, int)
callback
in response to every writeCharacteristic(BluetoothGattCharacteristic)
call and is responsible
for verifying if the value has been transmitted accurately.
After all characteristics have been queued up and verified,
executeReliableWrite()
will execute all writes. If a characteristic
was not written correctly, calling abortReliableWrite()
will
cancel the current transaction without commiting any values on the
remote device.
Requires BLUETOOTH
permission.
Close this Bluetooth GATT client. Application should call this method as early as possible after it is done with this GATT client.
Connect back to remote device.
This method is used to re-connect to a remote device after the connection has been dropped. If the device is not in range, the re-connection will be triggered once the device is back in range.
Disconnects an established connection, or cancels a connection attempt currently in progress.
Requires BLUETOOTH
permission.
Discovers services offered by a remote device as well as their characteristics and descriptors.
This is an asynchronous operation. Once service discovery is completed,
the onServicesDiscovered(BluetoothGatt, int)
callback is
triggered. If the discovery was successful, the remote services can be
retrieved using the getServices()
function.
Requires BLUETOOTH
permission.
Executes a reliable write transaction for a given remote device.
This function will commit all queued up characteristic write operations for a given remote device.
A onReliableWriteCompleted(BluetoothGatt, int)
callback is
invoked to indicate whether the transaction has been executed correctly.
Requires BLUETOOTH
permission.
Not supported - please use getConnectedDevices(int)
with GATT
as argument
Not supported - please use getConnectedDevices(int)
with GATT
as argument
device | Remote bluetooth device. |
---|
STATE_CONNECTED
, STATE_CONNECTING
,
STATE_DISCONNECTED
, STATE_DISCONNECTING
Return the remote bluetooth device this GATT client targets to
Not supported - please use
getDevicesMatchingConnectionStates(int, int[])
with GATT
as first argument
states | Array of states. States can be one of
STATE_CONNECTED , STATE_CONNECTING ,
STATE_DISCONNECTED , STATE_DISCONNECTING , |
---|
Returns a BluetoothGattService
, if the requested UUID is
supported by the remote device.
This function requires that service discovery has been completed for the given device.
If multiple instances of the same service (as identified by UUID) exist, the first instance of the service is returned.
Requires BLUETOOTH
permission.
uuid | UUID of the requested service |
---|
Returns a list of GATT services offered by the remote device.
This function requires that service discovery has been completed for the given device.
Requires BLUETOOTH
permission.
Reads the requested characteristic from the associated remote device.
This is an asynchronous operation. The result of the read operation
is reported by the onCharacteristicRead(BluetoothGatt, BluetoothGattCharacteristic, int)
callback.
Requires BLUETOOTH
permission.
characteristic | Characteristic to read from the remote device |
---|
Reads the value for a given descriptor from the associated remote device.
Once the read operation has been completed, the
onDescriptorRead(BluetoothGatt, BluetoothGattDescriptor, int)
callback is
triggered, signaling the result of the operation.
Requires BLUETOOTH
permission.
descriptor | Descriptor value to read from the remote device |
---|
Read the RSSI for a connected remote device.
The onReadRemoteRssi(BluetoothGatt, int, int)
callback will be
invoked when the RSSI value has been read.
Requires BLUETOOTH
permission.
Request a connection parameter update.
This function will send a connection parameter update request to the remote device.
connectionPriority | Request a specific connection priority. Must be one of
CONNECTION_PRIORITY_BALANCED ,
CONNECTION_PRIORITY_HIGH
or CONNECTION_PRIORITY_LOW_POWER . |
---|
IllegalArgumentException | If the parameters are outside of their specified range. |
---|
Request an MTU size used for a given connection.
When performing a write request operation (write without response), the data sent is truncated to the MTU size. This function may be used to request a larger MTU size to be able to send more data at once.
A onMtuChanged(BluetoothGatt, int, int)
callback will indicate
whether this operation was successful.
Requires BLUETOOTH
permission.
Enable or disable notifications/indications for a given characteristic.
Once notifications are enabled for a characteristic, a
onCharacteristicChanged(BluetoothGatt, BluetoothGattCharacteristic)
callback will be
triggered if the remote device indicates that the given characteristic
has changed.
Requires BLUETOOTH
permission.
characteristic | The characteristic for which to enable notifications |
---|---|
enable | Set to true to enable notifications/indications |
Writes a given characteristic and its values to the associated remote device.
Once the write operation has been completed, the
onCharacteristicWrite(BluetoothGatt, BluetoothGattCharacteristic, int)
callback is invoked,
reporting the result of the operation.
Requires BLUETOOTH
permission.
characteristic | Characteristic to write on the remote device |
---|
Write the value of a given descriptor to the associated remote device.
A onDescriptorWrite(BluetoothGatt, BluetoothGattDescriptor, int)
callback is
triggered to report the result of the write operation.
Requires BLUETOOTH
permission.
descriptor | Descriptor to write to the associated remote device |
---|