services

fun services(uuids: List<Uuid> = emptyList()): StateFlow<RemoteServices>

Returns a flow emitting RemoteServices events.

Overview

This method is the main entry point to discover GATT services on the peripheral and observe changes in the services. It returns a flow that emits the current state of service discovery process, which can be observed to get the list of services when they are discovered, or to handle errors when service discovery fails.

Note, that the flow may emit RemoteServices.Discovered state multiple times, for example when the peripheral reconnects or when the services get invalidated and rediscovered. This is not a one-time operation, but a continuous observation of the services on the peripheral.

State machine

Initially, the state is Unknown. Shortly after calling services the flow will emit Discovering state, followed by Discovered state, or (unlikely) Failed state, where the reason of the failure is given as a parameter Failed.reason.

When in Discovered or Failed, and the service will get invalidated, either due to a disconnection or Service Change indication sent from the remote peripheral, the state will transition to Unknown.

Service change will automatically request service discovery, which, when complete, will emit Discovered state with new set of services.

                          (Initial state)

┌───────────────> Unknown <────────────────┐
│ │ │
(disconnection) (service discovery started) (disconnection)
│ ↓ │
Failed <── (failure) ─── Discovering ─── (done) ──> Discovered

Return

A state flow with the current state of service discovery process. If the method was called with a uuids filter, the RemoteServices.Discovered state will contain only services with given UUIDs, otherwise it will contain all services returned by the system. If the returned list is empty, the service was not found on the peripheral.

Parameters

uuids

An optional list of service UUID to filter the results. If empty, all services will be returned. Some platforms may do partial service discovery and return only services with given UUIDs.