How are frequent writeCharactristic() calls handled?

Ask, and ye shall receive.
sarkreth
Posts: 8
Joined: 00:04, 12 Nov 2015

How are frequent writeCharactristic() calls handled?

Postby sarkreth » 23:01, 21 Dec 2015

I'm working on an app that communicates the position of a quickly changing "slider" button (a single byte) over BLE. I haven't tested it yet, but I sense trouble depending on how writeCharacteristic() behaves at the interface to the actual Bluetooth stack implementation.

Bluetooth communications happen in regular intervals, but touch events fire as often as a new thing happens. What exactly happens if these events happen more frequently than the communications?

Do they get buffered? If so, how does the data get written? Are there just a bunch of overwrites? What I'm mainly afraid of is only one value being written for every communication, which would cause fast changes in the slider's position to be communicated and read in the peripheral device too slowly.

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: How are frequent writeCharactristic() calls handled?

Postby Fredrik » 00:04, 22 Dec 2015

The cordova-ble plugin queues up all writes. The next pending write is executed when the remote device's response arrives. Queing more writes over time than the system can process will cause increasingly old data to be written to the remote, and eventually cause an out-of-memory crash.

I suggest you use the writeCharacteristic callback to keep track of pending writes, and write a new value only when the previous write is finished.

sarkreth
Posts: 8
Joined: 00:04, 12 Nov 2015

Re: How are frequent writeCharactristic() calls handled?

Postby sarkreth » 20:39, 22 Dec 2015

You may be on to something there, but I'm still a bit unclear on how to use the writeCharacteristic() success callback. Does wC() write the value to a buffer, then register the callback to be called when the value actually gets written, then return asynchronously? This would be perfect!

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: How are frequent writeCharactristic() calls handled?

Postby Fredrik » 20:47, 22 Dec 2015

Does wC() return asynchronously, and register the callback to be called when the value actually gets written?
Yes.

It also depends a bit on the remote device. There are two types of characteristic write settings: with and without response. The remote decides which one must be used.

In the first case, the plugin waits for the remote to acknowledge the write, then calls the callback.

In the second case, the plugin calls the callback as soon as the data packet has been transmitted.

Even in the second case, the queing effect may appear, since each packet must wait for a transmission time slot, and those are separated by 7 to 30 milliseconds (iirc).

sarkreth
Posts: 8
Joined: 00:04, 12 Nov 2015

Re: How are frequent writeCharactristic() calls handled?

Postby sarkreth » 10:05, 23 Dec 2015

You just caused me to have an epiphany about Javascript, dude! Thanks!


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 2 guests