Sessions
Learn how a sans-IO session tells your app what to send and turns replies into readings.
A session is one conversation with one watch, with no I/O inside. Your app owns the Bluetooth connection. The session tells it what to send and turns what comes back into readings.
The loop
- Connect to the watch and discover its services.
- Create a
WatchSessionwith the constructor for the watch's protocol, passing the sync settings and any saved state. - Call
handle(Connected { characteristics })with every characteristic the watch has. - Keep one queue. Run one request at a time, in order. Feed each completion, and every notification, back to
handle, and append the requests that come back. - Keep the
sampleseach call returns, and storesaved_state()whenever it changes. - Stop when
finishedis set, or when no event arrives within your own timeout.
Requests
| Request | What your app does |
|---|---|
Subscribe | Turn on notifications for a characteristic. |
Read | Read a characteristic. |
Write | Write value, with or without response as with_response says. |
RequestMtu | Ask for an MTU. On iOS, which picks the MTU itself, answer with maximumWriteValueLength(for: .withoutResponse) + 3. |
Pause | Wait before the next request; no event follows. |
Events
| Event | When to send it |
|---|---|
Connected | Services are discovered; list every characteristic the watch has. |
Subscribed | A subscribe completed. |
Read | A read completed, with its value. |
Notified | The watch sent a notification, with its value. |
Written | A write completed. |
MtuChanged | The MTU is known. |
Characteristic UUIDs come back in the 36-character form and are accepted in 4, 8 or 36 characters.
Settings
Every constructor takes SyncSettings: the phone's wall-clock time, the UTC offset in force now (daylight saving
included), and how many days before today to read. Drivers whose watch decides what it sends ignore the day count.
Errors
handle and the constructors fail with a SessionError:
Watch: the watch replied with something the session cannot use.InvalidUuid: a UUID the app passed is not a UUID.InvalidSavedState: the bytes are not a state this driver saved; forget them and connect without.InvalidBluetoothAddress: the watch needs its 6-byte Bluetooth address, which iOS cannot read.RandomnessUnavailable: the system could not supply the random bytes a first bind needs.Interrupted: an earlier call panicked; start a new session.