I have a BLE Characteristic configured as Notify, which doesn't change value very often. I'd like to detect when a central has subscribed to notifications, to force setting the value so it can read it once initially.
Is there some way to detect that a central has subscribed to be notified for a Characteristic?
Out of curiosity, is there a reason you cannot have both READ and NOTIFY properties on the characteristic? The central device can read it on connection rather than waiting for a notification to be sent from the peripheral.
That's a good question, because in the docs it recommends to use either NOTIFY or WRITE_WO_RSP, and to not use them at the same time. But it doesn't explain why...
I tried using them together, and it seems to work. I also added READ as you suggest and it works too. Not sure if I'm setting the ground for some magical future problem because the docs are not really clear about the reasons for this recommendation.
The problem I have with this approach is that I don't have control of the central device. I need to implement a predefined interface that works like this. This interface is already used with other hardware, and it expects this behaviour.
Shouldn't be so complex, I just need to have a loop checking the CCCD and detect when someone has enabled notifications, but the library is abstracting this, and I didn't find any mention in the docs that allow me to read if the central subscribed.
Thanks for the suggestion, though! If I had control of the central, this would be a good solution.
There isn't a technical reason for not using both notify and read. It's just that notify is usually used for push (from the peripheral) and read is used for pull (from the central) but you can do both.
There is no way to be notified on the peripheral when the central subscribes to a notify characteristic.
If you don't want to do read, best approximation would be to use an onConnect handler and then do a notify a little while after the central connects, allowing enough time for the central to subscribe to the characteristic.
Thanks for clarifying that, a pity that it's not possible, but thanks for proposing a pragmatic workaround, will give it a thought and most likely do it that way, since it's the only real way forward if I can't control the central's behaviour.
Also appreciate the clarification about the Properties, I guess you can really use any combination without any problem, right?