samples icon indicating copy to clipboard operation
samples copied to clipboard

An example dealing with multiple write requests would be very helpful

Open DanielO opened this issue 8 years ago • 7 comments

Hi, I am a JS noob and would really appreciate a sample demonstrating how to implement queuing or retries for writeValue requests. I get "NetworkError: GATT operation already in progress." (i.e. https://github.com/WebBluetoothCG/web-bluetooth/issues/188) when trying to write a non-trivial app that has user driven writes.

DanielO avatar May 09 '17 04:05 DanielO

Hello @DanielO, here's below some JS snippet that might help you.

navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]})
.then(device => device.gatt.connect())
.then(server => server.getPrimaryService('heart_rate'))
.then(service => service.getCharacteristic('heart_rate_control_point'))
.then(characteristic => resetEnergy(characteristic, 3 /* tries */)))
.then(_ => {
  console.log('Energy expended has been reset.');
 })
.catch(error => {
  log('Argh! ' + error);
});

function resetEnergy(characteristic, leftTries) {
  // Writing 1 is the signal to reset energy expended.
  let resetEnergyExpended = Uint8Array.of(1);
  return characteristic.writeValue(resetEnergyExpended)
  .catch(error => {
     leftTries--;
     if (leftTries > 0) {
       return resetEnergy(characteristic, leftTries);
     }
     return Promise.reject(error);
  });
}

beaufortfrancois avatar May 09 '17 06:05 beaufortfrancois

Ahh, that looks good thanks!

I actually ended up creating a message queue using https://github.com/d3/d3-queue which is probably overkill..

Do you think it's worth adding to the docs? Or is it there and I missed it? (quite possible :)

DanielO avatar May 09 '17 07:05 DanielO

This example shows how to retry one write over and over. I'm struggling to see how to queue up multiple writes. Any chance of any updated example?

hardillb avatar Sep 06 '18 14:09 hardillb

@hardillb It's been a while so I've paged this out but my code is at https://bitbucket.org/DJOConnor/mooshiweb/src/default/ if you want to poke around.

DanielO avatar Sep 06 '18 14:09 DanielO

I'm facing the same problem, need to do a lot write operations and getting this error

ERROR Error: Uncaught (in promise): NetworkError: GATT operation already in progress.

@DanielO The link is broken, could you share the code again ?

conde2 avatar Sep 27 '20 16:09 conde2

How about - https://hg.sr.ht/~darius/mooshiweb/browse

DanielO avatar Sep 27 '20 23:09 DanielO

I manage doing it with a simple array and promises, thanks

  tasks: Function[] = [];

  async addTask(task) {
    const previousTaskCount = this.tasks.length;
    this.tasks.push(task);

    if (previousTaskCount === 0) {
      this.runTasks();
    }
  }

  async runTasks() {
    if (this.tasks.length <= 0) {
      return true;
    }

    while(this.tasks.length > 0) {
      const task: Function = this.tasks[0];
      try {
        await task.call(this);
        this.tasks.shift();
      } catch (error) {
        console.log("Error:", error);
      }
    }
  }

conde2 avatar Sep 29 '20 23:09 conde2