How to do multiple successive BTLE.Characteristic.requestWrite()

Hi,

I'm fairly new to ConnectIQ and totally new to BTLE.

I need to do two requestWrite(...) to a BTLE Characteristic. I don't know when/how to do the second one.

If i do it on the BleDelegate.onCharacteristicWrite() of the first write, I get an exception (Something like "Operation already in progress"). Am I right in assuming that an event triggered from a BTLE write/read/etc (basically all BleDelegate.onXYZ() callbacks) can not immediately trigger another read/write?

For now I have solved this with doing both my writes triggered as a callback from a Toybox.Timer.Timer and allowing for some ample time between.
Is that the proper solution? Or can I somehow more explicitly create threads?

thx,

  Florian

  • You need to use a queue for the requests.  Not a timer.

    do first write and when it completes, do the second. 

    The nordicthingy52 sample in the SDK uses a queue, and I do it too in the apps for a pi here: https://forums.garmin.com/developer/connect-iq/b/news-announcements/posts/would-you-like-some-raspberry-pi-with-your-connect-iq

    See commqueue.mc in one of the ciq projects.  In the ble delegate, when something like onCharacteristicWrite occurs, take the next thing off the queue if there is something.

    From that code, here I queue up 3 reads

    char = (piService!=null) ? piService.getCharacteristic(profileManager.PI_MEMORY) : null;
    queue.add(self,[char,queue.C_READ,null],profileManager.PI_MEMORY);
    char = (piService!=null) ? piService.getCharacteristic(profileManager.PI_UPTIME) : null;
    queue.add(self,[char,queue.C_READ,null],profileManager.PI_UPTIME);
    char = (piService!=null) ? piService.getCharacteristic(profileManager.PI_LOAD) : null;
    queue.add(self,[char,queue.C_READ,null],profileManager.PI_LOAD);

    if there's noting in the queue when something is added, perform that action.

    When onCharacteristicRead occurs, run the next in the queue (if there is something) with

    queue.run(theView);

    There's also some logic that adds a timeout, so if a read is requested, and onCharacteristicRead doesn't occure for X seconds, an error is displayed and the connection gets reset.