public abstract class BlePeripheral
- Object
- BluetoothDevice
- BlePeripheral
A remote BLE peripheral: connection lifecycle, GATT client operations
and L2CAP channels. Obtained from scans
(ScanResult.getPeripheral()) or from
BluetoothLE.getPeripheral(String).
All operations return independent AsyncResource handles and may be
issued concurrently – an internal per-peripheral queue serializes them
toward the platform stack (which only allows one in-flight GATT request
per connection) and applies a safety timeout so a lost platform
callback can never wedge the queue. Operations on different
peripherals run fully concurrently.
Ports subclass this and implement the protected do* methods; they
report events through the fire* methods, which are safe to call from
any thread. All application-facing callbacks are delivered on the EDT.
Constructors
protected BlePeripheral() | Ports construct subclasses; application code receives instances from scans and BluetoothLE. |
Methods
Inherited methods
Constructor details
BlePeripheral
protected BlePeripheral()BluetoothLE.Method details
connect
public final AsyncResource<BlePeripheral> connect()ConnectionOptions.connect
public final AsyncResource<BlePeripheral> connect(ConnectionOptions options)BluetoothException. Calling connect
while already ConnectionState.CONNECTING returns the existing
attempt’s handle; while ConnectionState.CONNECTED it resolves
immediately.disconnect
public final void disconnect()BluetoothError.USER_CANCELED.getConnectionState
public final ConnectionState getConnectionState()addConnectionListener
public final void addConnectionListener(ConnectionListener l)removeConnectionListener
public final void removeConnectionListener(ConnectionListener l)addConnectionListener(ConnectionListener).discoverServices
public final AsyncResource<List<GattService>> discoverServices()getServices()) or fails with a
BluetoothException.getServices
public final List<GattService> getServices()discoverServices() call;
empty before discovery.getService
public final GattService getService(BluetoothUuid uuid)null.getCharacteristic
public final GattCharacteristic getCharacteristic(BluetoothUuid service, BluetoothUuid characteristic)getService(service).getCharacteristic(characteristic)
that returns null instead of throwing when either is absent.readCharacteristic
public final AsyncResource<byte[]> readCharacteristic(GattCharacteristic c)GattCharacteristic.read().writeCharacteristic
public final AsyncResource<Boolean> writeCharacteristic(GattCharacteristic c, byte[] value, boolean withResponse)readDescriptor
public final AsyncResource<byte[]> readDescriptor(GattDescriptor d)GattDescriptor.read().writeDescriptor
public final AsyncResource<Boolean> writeDescriptor(GattDescriptor d, byte[] value)subscribe
public final AsyncResource<Boolean> subscribe(GattCharacteristic c, GattNotificationListener l)GattCharacteristic.subscribe(GattNotificationListener). The CCCD
is written only on the transition from zero to one listener.unsubscribe
public final AsyncResource<Boolean> unsubscribe(GattCharacteristic c, GattNotificationListener l)GattCharacteristic.unsubscribe(GattNotificationListener). The
CCCD is disarmed when the last listener is removed.isSubscribed
public final boolean isSubscribed(GattCharacteristic c)true while notifications are armed for the characteristic.readRssi
public final AsyncResource<Integer> readRssi()requestMtu
public final AsyncResource<Integer> requestMtu(int mtu)getMtu
public final int getMtu()23 (the BLE default) until a
larger value was negotiated.requestConnectionPriority
public final AsyncResource<Boolean> requestConnectionPriority(ConnectionPriority priority)createBond
public final AsyncResource<Boolean> createBond()true
without user interaction.openL2capChannel
public final AsyncResource<L2capChannel> openL2capChannel(int psm, boolean secure)connect() first; on iOS the
peripheral must be connected.doConnect
protected abstract void doConnect(ConnectionOptions options, AsyncResource<BlePeripheral> out)out.doDisconnect
protected abstract void doDisconnect()fireConnectionStateChanged(ConnectionState, BluetoothException)
reports the resulting DISCONNECTED transition.doDiscoverServices
protected abstract void doDiscoverServices(AsyncResource<List<GattService>> out)out with the discovered
GattService list (constructed via the public gatt-package
constructors).doReadCharacteristic
protected abstract void doReadCharacteristic(GattCharacteristic c, AsyncResource<byte[]> out)out with its value.doWriteCharacteristic
protected abstract void doWriteCharacteristic(GattCharacteristic c, byte[] value, boolean withResponse, AsyncResource<Boolean> out)out once acknowledged
(withResponse) or queued to the controller.doReadDescriptor
protected abstract void doReadDescriptor(GattDescriptor d, AsyncResource<byte[]> out)out with its value.doWriteDescriptor
protected abstract void doWriteDescriptor(GattDescriptor d, byte[] value, AsyncResource<Boolean> out)out once acknowledged.doSetNotifications
protected abstract void doSetNotifications(GattCharacteristic c, boolean enable, boolean indication, AsyncResource<Boolean> out)out once done.doReadRssi
protected abstract void doReadRssi(AsyncResource<Integer> out)out.doRequestMtu
protected abstract void doRequestMtu(int mtu, AsyncResource<Integer> out)out with the granted value.doRequestConnectionPriority
protected abstract void doRequestConnectionPriority(ConnectionPriority priority, AsyncResource<Boolean> out)out.doCreateBond
protected abstract void doCreateBond(AsyncResource<Boolean> out)out when the bond state settles.doOpenL2cap
protected abstract void doOpenL2cap(int psm, boolean secure, AsyncResource<L2capChannel> out)out with it. Called
directly (not through the operation queue).fireConnectionStateChanged
protected final void fireConnectionStateChanged(ConnectionState newState, BluetoothException reason)reason is null for app-requested transitions.fireNotification
protected final void fireNotification(GattCharacteristic c, byte[] value)GattCharacteristic instance from the discovered database.fireServicesInvalidated
protected final void fireServicesInvalidated()didModifyServices); the app should re-run
discoverServices().setOperationTimeout
protected final void setOperationTimeout(int millis)0 disables).setMtu
protected final void setMtu(int mtu)requestMtu(int) call.