-
Notifications
You must be signed in to change notification settings - Fork 512
Device Service Discovery
Dariusz Seweryn edited this page Feb 17, 2020
·
5 revisions
To interact with Bluetooth characteristics (read/write/notify) one must first discover what characteristics are available (even if you know what you are connecting to and what properties it has, the BLE stack underneath does not). To do so one must call:
device.discoverAllServicesAndCharacteristics(): Promise<Device>
or
bleManager.discoverAllServicesAndCharacteristicsForDevice(
deviceIdentifier: DeviceId,
): Promise<Device>
-
deviceIdentifier: DeviceId
—is obtained fromdevice.id
In both situations the promise will resolve after all services and characteristics are discovered.
-
device.serviceUUIDs
anddevice.serviceData
are related to BLE Scan Advertisement, not to Service Discovery. They will not be populated/updated in result ofdevice.discoverAllServicesAndCharacteristics()
- We are aware that vanilla iOS API makes it possible to discover services/characteristics in a more granular way. Unfortunately—for now the library is trying to find a least common denominator for all supported OSes and Android allows only to discover everything at once
- On some older Android devices there is a known bug. The service discovery is resolved with an empty array of services without any error. What usually helps is to disconnect/powercycle bluetooth adapter. We do not have a pair of device/peripheral that would reliably show this behaviour to workaround it—if someone would be able to reproduce this feel free to open an issue and help us fix it.
When the service discovery was already performed after the last connection has been established it is possible to retrieve the cached services by calling:
device.services(): Promise<Service[]>
or
bleManager.servicesForDevice(deviceIdentifier: DeviceId): Promise<Array<Service>>
-
deviceIdentifier: DeviceId
—is obtained fromdevice.id
The promise will resolve with services that were discovered for a particular device
// assuming the 'device' is already connected
await device.discoverAllServicesAndCharacteristics();
const services = await device.services();
// do your thing with the services