This is a refresher exploration of BLE utilizing patterns I have used in the past, but adding new types for managing received data such as Kotlin Flows. I have been tinkering with this on and off for a few years. This is currently a work-in-progress. I have been updating the codebase as libraries have been updated.
NOTE This is definitely not a hardened app.
NOTE This is not to demonstrate best practices of UI/UX design nor a model app for Jetpack Compose usage; just enough to demonstrate the BLE functionality.
NOTE There is no road map for this project.
As of right now it:
- Scans for devices using BleScan
- GATT Status Codes
- Advertisements
- Connects to devices using BleConnection
- Supports MTU negotiation
- Handles connection state changes
- Handles disconnections
- Handles connection timeouts
- Discovers services and characteristics
- BLE Requests
- Manages a queue of requests to the device, processing them one at a time
- Synchronous blocking requests see
BleConnection.readCharacteristicBleConnection.readDescriptorBleConnection.writeCharacteristicBleConnection.writeDescriptor
- Asynchronous requests processed via callbacks. See
BleConnection.submitBleRequest - BleRequest
- BleRequestResult
- Reads characteristics
- Writes characteristics
- CharacteristicWriteRequest
- Supports chunking for large writes
- Supports resending on failure up to a configurable max retry count
- Reads descriptors
- Writes descriptors
- DescriptorWriteRequest
- Supports chunking for large writes
- Supports resending on failure up to a configurable max retry count
- Subscribes to notifications
- BLE Device representation (BleDevice) - the Central's representation of
a Peripheral; this is the place to start exploring as any interaction with a
Peripheral should be done through a subclass of
BleDevice. - BLE Peripheral support - allows Android device to act as a BLE Peripheral (GATT Server)
- BaseBlePeripheral - Abstract base class for creating a BLE Peripheral
- Handles BLE advertising with customizable settings
- Manages GATT server setup and lifecycle
- Supports multiple client connections
- Handles characteristic read/write requests
- Supports notifications/indications to subscribed clients
- Performs MTU negotiation
- Permission checking
- PeripheralService - Service definition for peripherals
- PeripheralCharacteristic - Characteristic with managed state
- PeripheralLog - Tracks peripheral operations for debugging
- SendDataRequest - Handles chunking for large notification payloads
- BaseBlePeripheral - Abstract base class for creating a BLE Peripheral
- Provides some common BLE Attributes (see attributes)
- A test app UI to interact with devices that demonstrates the following
functionality:
- Scanning
- Scan results
- Connecting to a device
- Connection state changes
- MTU changes
- Services discovered
- Sample Communication between a
SampleBlePeripheral
(extends
BaseBlePeripheral) via the Peripheral screen and a SampleBleDevice (extendsBleDevice) via the Central screen. This demonstrates:- Scanning for a specific device type using a service UUID filter
- Connection
- Read Characteristics
- Write Characteristics
- Notifications using the SamplePeripheralProtocol
- Disconnecting
ANOTHER NOTE Development is slow moving here, not everything is perfect