PureSwift / GATT

Bluetooth Generic Attribute Profile (GATT) for Swift (Supports Linux)
MIT License
64 stars 18 forks source link

Unable to subscribe to notifications for characteristics from different services on Darwin platforms #19

Closed wlisac closed 5 years ago

wlisac commented 5 years ago

Issue

Subscribing to notifications for characteristics from different services is not working as expected on Darwin platforms (iOS and macOS).

Expected Behavior

I'd expect for the notification handler to be triggered for all of the characteristics I subscribe to, even if those characteristics are from different services.

Observed Behavior

Only the last characteristics I subscribe to from a single service have the notification handlers called.

Possible Cause

I've done some initial investigation and the cause of this limitation may live in the Cache of DarwinCentral.swift: https://github.com/PureSwift/GATT/blob/5752c678e2c723d22112c04477157fc5116863dd/Sources/DarwinGATT/DarwinCentral.swift#L781-L823

The Cache stores Services and Characteristics as siblings. The Characteristics are stored by the index / order that they were discovered in.

This means that if you discover characteristics from Service A. Then discover characteristics from Service B. The characteristics from Service B overwrite the characteristics from Service A.

Additional Information

Depending on how the caller discovers / subscribes to notifications, this may result in an Invalid characteristic assertion failure when attempting to call the notification handlers: https://github.com/PureSwift/GATT/blob/5752c678e2c723d22112c04477157fc5116863dd/Sources/DarwinGATT/DarwinCentral.swift#L595-L598