Class BlePeripheral
- Direct Known Subclasses:
HelperBlePeripheral
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.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedPorts construct subclasses; application code receives instances from scans andBluetoothLE. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidRegisters a listener notified on the EDT of every connection state transition.final AsyncResource<BlePeripheral> connect()Connects with defaultConnectionOptions.final AsyncResource<BlePeripheral> connect(ConnectionOptions options) Establishes a connection.final AsyncResource<Boolean> Initiates bonding/pairing.final voidDisconnects.final AsyncResource<List<GattService>> Discovers the peripheral's services.protected abstract voiddoConnect(ConnectionOptions options, AsyncResource<BlePeripheral> out) Establishes the platform connection and completes/failsout.protected abstract voiddoCreateBond(AsyncResource<Boolean> out) Initiates bonding and completesoutwhen the bond state settles.protected abstract voidTears down the platform connection;fireConnectionStateChanged(ConnectionState, BluetoothException)reports the resultingDISCONNECTEDtransition.protected abstract voidPerforms service discovery and completesoutwith the discoveredGattServicelist (constructed via the public gatt-package constructors).protected abstract voiddoOpenL2cap(int psm, boolean secure, AsyncResource<L2capChannel> out) Opens an L2CAP channel and completesoutwith it.protected abstract voiddoReadCharacteristic(GattCharacteristic c, AsyncResource<byte[]> out) Reads a characteristic and completesoutwith its value.protected abstract voiddoReadDescriptor(GattDescriptor d, AsyncResource<byte[]> out) Reads a descriptor and completesoutwith its value.protected abstract voiddoReadRssi(AsyncResource<Integer> out) Reads the connection RSSI and completesout.protected abstract voiddoRequestConnectionPriority(ConnectionPriority priority, AsyncResource<Boolean> out) Requests a connection-interval preference and completesout.protected abstract voiddoRequestMtu(int mtu, AsyncResource<Integer> out) Requests an MTU and completesoutwith the granted value.protected abstract voiddoSetNotifications(GattCharacteristic c, boolean enable, boolean indication, AsyncResource<Boolean> out) Arms or disarms notifications/indications (including the CCCD write) and completesoutonce done.protected abstract voiddoWriteCharacteristic(GattCharacteristic c, byte[] value, boolean withResponse, AsyncResource<Boolean> out) Writes a characteristic and completesoutonce acknowledged (withResponse) or queued to the controller.protected abstract voiddoWriteDescriptor(GattDescriptor d, byte[] value, AsyncResource<Boolean> out) Writes a descriptor and completesoutonce acknowledged.protected final voidfireConnectionStateChanged(ConnectionState newState, BluetoothException reason) Reports an (unsolicited) connection state transition -- link loss, remote disconnect, or connection established outside a pending connect call.protected final voidfireNotification(GattCharacteristic c, byte[] value) Delivers a notification/indication value to the subscribed listeners on the EDT.protected final voidInvalidates the cached service database (iOSdidModifyServices); the app should re-rundiscoverServices().final GattCharacteristicgetCharacteristic(BluetoothUuid service, BluetoothUuid characteristic) Convenience forgetService(service).getCharacteristic(characteristic)that returnsnullinstead of throwing when either is absent.final ConnectionStateThe current connection state.final intgetMtu()The current MTU of the connection;23(the BLE default) until a larger value was negotiated.final GattServicegetService(BluetoothUuid uuid) The first cached service with the given UUID, ornull.final List<GattService> The cached service list from the lastdiscoverServices()call; empty before discovery.final booleantruewhile notifications are armed for the characteristic.final AsyncResource<L2capChannel> openL2capChannel(int psm, boolean secure) Opens an L2CAP connection-oriented channel to the given PSM.final AsyncResource<byte[]> Reads a characteristic value; prefer the convenienceGattCharacteristic.read().final AsyncResource<byte[]> Reads a descriptor value; prefer the convenienceGattDescriptor.read().final AsyncResource<Integer> readRssi()Reads the current RSSI of the connection.final voidRemoves a listener added viaaddConnectionListener(ConnectionListener).final AsyncResource<Boolean> requestConnectionPriority(ConnectionPriority priority) Requests a connection-interval preference; a successful no-op on platforms that manage intervals themselves (iOS).final AsyncResource<Integer> requestMtu(int mtu) Requests an MTU; resolves with the granted value (which may be smaller).protected final voidsetMtu(int mtu) Records the negotiated MTU -- for ports whose platform reports MTU changes outside arequestMtu(int)call.protected final voidsetOperationTimeout(int millis) Adjusts the safety timeout applied to each queued GATT operation (default 30000ms;0disables).final AsyncResource<Boolean> Subscribes a listener to a characteristic's notifications; prefer the convenienceGattCharacteristic.subscribe(GattNotificationListener).final AsyncResource<Boolean> Removes a notification listener; prefer the convenienceGattCharacteristic.unsubscribe(GattNotificationListener).final AsyncResource<Boolean> writeCharacteristic(GattCharacteristic c, byte[] value, boolean withResponse) Writes a characteristic value; prefer the convenience [GattCharacteristic#write(byte[])] / [GattCharacteristic#writeWithoutResponse(byte[])].final AsyncResource<Boolean> writeDescriptor(GattDescriptor d, byte[] value) Writes a descriptor value; prefer the convenience [GattDescriptor#write(byte[])].Methods inherited from class BluetoothDevice
equals, getAddress, getBondState, getName, getType, hashCode, toString
-
Constructor Details
-
BlePeripheral
protected BlePeripheral()Ports construct subclasses; application code receives instances from scans andBluetoothLE.
-
-
Method Details
-
connect
Connects with defaultConnectionOptions. -
connect
Establishes a connection. Resolves with this peripheral once connected or fails with aBluetoothException. Callingconnectwhile alreadyConnectionState.CONNECTINGreturns the existing attempt's handle; whileConnectionState.CONNECTEDit resolves immediately. -
disconnect
public final void disconnect()Disconnects. A no-op while already disconnected; an in-flight connect attempt is aborted withBluetoothError.USER_CANCELED. -
getConnectionState
The current connection state. -
addConnectionListener
Registers a listener notified on the EDT of every connection state transition. -
removeConnectionListener
Removes a listener added viaaddConnectionListener(ConnectionListener). -
discoverServices
Discovers the peripheral's services. Resolves with the service list (also cached -- seegetServices()) or fails with aBluetoothException. -
getServices
The cached service list from the lastdiscoverServices()call; empty before discovery. -
getService
The first cached service with the given UUID, ornull. -
getCharacteristic
public final GattCharacteristic getCharacteristic(BluetoothUuid service, BluetoothUuid characteristic) Convenience forgetService(service).getCharacteristic(characteristic)that returnsnullinstead of throwing when either is absent. -
readCharacteristic
Reads a characteristic value; prefer the convenienceGattCharacteristic.read(). -
writeCharacteristic
public final AsyncResource<Boolean> writeCharacteristic(GattCharacteristic c, byte[] value, boolean withResponse) Writes a characteristic value; prefer the convenience [GattCharacteristic#write(byte[])] / [GattCharacteristic#writeWithoutResponse(byte[])]. -
readDescriptor
Reads a descriptor value; prefer the convenienceGattDescriptor.read(). -
writeDescriptor
Writes a descriptor value; prefer the convenience [GattDescriptor#write(byte[])]. -
subscribe
Subscribes a listener to a characteristic's notifications; prefer the convenienceGattCharacteristic.subscribe(GattNotificationListener). The CCCD is written only on the transition from zero to one listener. -
unsubscribe
Removes a notification listener; prefer the convenienceGattCharacteristic.unsubscribe(GattNotificationListener). The CCCD is disarmed when the last listener is removed. -
isSubscribed
truewhile notifications are armed for the characteristic. -
readRssi
Reads the current RSSI of the connection. -
requestMtu
Requests an MTU; resolves with the granted value (which may be smaller). iOS negotiates the MTU itself -- there the request resolves immediately with the current value. -
getMtu
public final int getMtu()The current MTU of the connection;23(the BLE default) until a larger value was negotiated. -
requestConnectionPriority
Requests a connection-interval preference; a successful no-op on platforms that manage intervals themselves (iOS). -
createBond
Initiates bonding/pairing. On iOS bonding is OS-managed (triggered by encrypted characteristics), so the request resolvestruewithout user interaction. -
openL2capChannel
Opens an L2CAP connection-oriented channel to the given PSM. Not serialized with GATT operations. On Android this establishes its own link and does not requireconnect()first; on iOS the peripheral must be connected. -
doConnect
Establishes the platform connection and completes/failsout. -
doDisconnect
protected abstract void doDisconnect()Tears down the platform connection;fireConnectionStateChanged(ConnectionState, BluetoothException)reports the resultingDISCONNECTEDtransition. -
doDiscoverServices
Performs service discovery and completesoutwith the discoveredGattServicelist (constructed via the public gatt-package constructors). -
doReadCharacteristic
Reads a characteristic and completesoutwith its value. -
doWriteCharacteristic
protected abstract void doWriteCharacteristic(GattCharacteristic c, byte[] value, boolean withResponse, AsyncResource<Boolean> out) Writes a characteristic and completesoutonce acknowledged (withResponse) or queued to the controller. -
doReadDescriptor
Reads a descriptor and completesoutwith its value. -
doWriteDescriptor
protected abstract void doWriteDescriptor(GattDescriptor d, byte[] value, AsyncResource<Boolean> out) Writes a descriptor and completesoutonce acknowledged. -
doSetNotifications
protected abstract void doSetNotifications(GattCharacteristic c, boolean enable, boolean indication, AsyncResource<Boolean> out) Arms or disarms notifications/indications (including the CCCD write) and completesoutonce done. -
doReadRssi
Reads the connection RSSI and completesout. -
doRequestMtu
Requests an MTU and completesoutwith the granted value. -
doRequestConnectionPriority
protected abstract void doRequestConnectionPriority(ConnectionPriority priority, AsyncResource<Boolean> out) Requests a connection-interval preference and completesout. -
doCreateBond
Initiates bonding and completesoutwhen the bond state settles. -
doOpenL2cap
Opens an L2CAP channel and completesoutwith it. Called directly (not through the operation queue). -
fireConnectionStateChanged
protected final void fireConnectionStateChanged(ConnectionState newState, BluetoothException reason) Reports an (unsolicited) connection state transition -- link loss, remote disconnect, or connection established outside a pending connect call.reasonisnullfor app-requested transitions. -
fireNotification
Delivers a notification/indication value to the subscribed listeners on the EDT. Ports must pass the canonicalGattCharacteristicinstance from the discovered database. -
fireServicesInvalidated
protected final void fireServicesInvalidated()Invalidates the cached service database (iOSdidModifyServices); the app should re-rundiscoverServices(). -
setOperationTimeout
protected final void setOperationTimeout(int millis) Adjusts the safety timeout applied to each queued GATT operation (default 30000ms;0disables). -
setMtu
protected final void setMtu(int mtu) Records the negotiated MTU -- for ports whose platform reports MTU changes outside arequestMtu(int)call.
-