public class BluetoothConnection extends Object
BluetoothConnection
class represents a connection with a remote Bluetooth device.
Connections may be retrieved by listening to
connection events
while
connecting
to a device or while
advertising
. The GAP events related to a device may be handled by
setting the connection listener
of the adapter.
This class provides a method to get the address of the remote device. It also allows to disconnect to the device, to manage pairing and to discover the services that the device provides.
Modifier and Type | Method and Description |
---|---|
boolean |
disconnect()
Initiates a disconnection from the device.
|
boolean |
discoverService(BluetoothUuid uuid)
Discovers the service with the given UUID provided by the device.
|
boolean |
discoverServices()
Discovers all the services provided by the device.
|
BluetoothAddress |
getAddress()
Returns the Bluetooth address of the device.
|
boolean |
sendExecuteWriteResponse(BluetoothAttribute attribute,
byte status)
Sends the response to an execute write request sent by a device on the given attribute.
|
boolean |
sendNotification(BluetoothCharacteristic characteristic,
byte[] value,
boolean confirm)
Sends a notification or indication on the given characteristic to a device.
|
boolean |
sendPairRequest()
Initiates pairing with the device.
|
boolean |
sendPairResponse(boolean accept)
Replies to the pairing request of the device.
|
boolean |
sendPasskeyResponse(boolean accept,
int passkey)
Replies to the passkey request of the device.
|
boolean |
sendPrepareWriteResponse(BluetoothAttribute attribute,
byte status,
byte[] value,
int offset)
Sends the response to a prepare write request sent by a device on the given attribute.
|
boolean |
sendReadRequest(BluetoothAttribute attribute)
Sends a read request on the given attribute.
|
boolean |
sendReadResponse(BluetoothAttribute attribute,
byte status,
byte[] value)
Sends the response to a read request sent by a device on the given attribute.
|
boolean |
sendWriteRequest(BluetoothAttribute attribute,
byte[] value)
Sends a write request on the given attribute.
|
boolean |
sendWriteResponse(BluetoothAttribute attribute,
byte status)
Sends the response to a write request sent by a device on the given attribute.
|
public boolean disconnect()
ConnectionListener.onDisconnected(BluetoothConnection)
public boolean discoverService(BluetoothUuid uuid)
This operation may only be performed while the adapter is still connected to the device. In order to discover all
services, use discoverServices()
instead.
uuid
- the UUID of the service to discover.discoverServices()
,
ConnectionListener.onDiscoveryResult(BluetoothConnection, BluetoothService)
,
ConnectionListener.onDiscoveryCompleted(BluetoothConnection)
public boolean discoverServices()
This operation may only be performed while the adapter is still connected to the device. In order to discover
specific services only, use discoverService(BluetoothUuid)
instead.
discoverService(BluetoothUuid)
,
ConnectionListener.onDiscoveryResult(BluetoothConnection, BluetoothService)
,
ConnectionListener.onDiscoveryCompleted(BluetoothConnection)
public BluetoothAddress getAddress()
public boolean sendExecuteWriteResponse(BluetoothAttribute attribute, byte status)
attribute
- the attribute on which the request was sent.status
- the status to send back (see BluetoothStatus
).IllegalArgumentException
- if the attribute is not a local attribute.public boolean sendNotification(BluetoothCharacteristic characteristic, byte[] value, boolean confirm)
characteristic
- the characteristic to send a notification on.value
- the value to send.confirm
- true to request a confirmation (GATT indication), false otherwise (GATT notification).IllegalArgumentException
- if the characteristic is not a local characteristic.LocalServiceListener.onNotificationSent(BluetoothConnection, BluetoothCharacteristic,
boolean)
public boolean sendPairRequest()
This operation may only be performed while the adapter is still connected to the device.
ConnectionListener.onPairCompleted(BluetoothConnection, boolean)
,
ConnectionListener.onPasskeyRequest(BluetoothConnection)
public boolean sendPairResponse(boolean accept)
This operation may only be performed after receiving a pairing request.
accept
- true if the pairing request should be accepted, false otherwise.ConnectionListener.onPairCompleted(BluetoothConnection, boolean)
,
ConnectionListener.onPasskeyGenerated(BluetoothConnection, int)
public boolean sendPasskeyResponse(boolean accept, int passkey)
This operation may only be performed after receiving a passkey request.
accept
- true if the passkey request should be accepted, false otherwise.passkey
- the passkey value (6-digits PIN code). This parameter is used only if the request is accepted.ConnectionListener.onPairCompleted(BluetoothConnection, boolean)
public boolean sendPrepareWriteResponse(BluetoothAttribute attribute, byte status, byte[] value, int offset)
attribute
- the attribute on which the request was sent.status
- the status to send back (see BluetoothStatus
).value
- the value of the prepare write request.offset
- the offset of the prepare write request.IllegalArgumentException
- if the attribute is not a local attribute.public boolean sendReadRequest(BluetoothAttribute attribute)
attribute
- the attribute to send a read request on.IllegalArgumentException
- if the attribute is not a remote attribute.IllegalArgumentException
- if the attribute does not belong to the device.RemoteServiceListener.onReadCompleted(BluetoothConnection, BluetoothAttribute, byte,
byte[])
public boolean sendReadResponse(BluetoothAttribute attribute, byte status, byte[] value)
attribute
- the attribute on which the request was sent.status
- the status to send back (see BluetoothStatus
).value
- the value to send back.IllegalArgumentException
- if the attribute is not a local attribute.public boolean sendWriteRequest(BluetoothAttribute attribute, byte[] value)
attribute
- the attribute to send a write request on.value
- the value to write.IllegalArgumentException
- if the attribute is not a remote attribute.IllegalArgumentException
- if the attribute does not belong to the device.RemoteServiceListener.onWriteCompleted(BluetoothConnection, BluetoothAttribute, byte)
public boolean sendWriteResponse(BluetoothAttribute attribute, byte status)
attribute
- the attribute on which the request was sent.status
- the status to send back (see BluetoothStatus
).IllegalArgumentException
- if the attribute is not a local attribute.