Skip to Content

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

  1. Connect to the watch and discover its services.
  2. Create a WatchSession with the constructor for the watch's protocol, passing the sync settings and any saved state.
  3. Call handle(Connected { characteristics }) with every characteristic the watch has.
  4. 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.
  5. Keep the samples each call returns, and store saved_state() whenever it changes.
  6. Stop when finished is set, or when no event arrives within your own timeout.

Requests

RequestWhat your app does
SubscribeTurn on notifications for a characteristic.
ReadRead a characteristic.
WriteWrite value, with or without response as with_response says.
RequestMtuAsk for an MTU. On iOS, which picks the MTU itself, answer with maximumWriteValueLength(for: .withoutResponse) + 3.
PauseWait before the next request; no event follows.

Events

EventWhen to send it
ConnectedServices are discovered; list every characteristic the watch has.
SubscribedA subscribe completed.
ReadA read completed, with its value.
NotifiedThe watch sent a notification, with its value.
WrittenA write completed.
MtuChangedThe 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.

See also

On this page