How to write to gatt characteristic as fast as possible? #2664
Replies: 5 comments
-
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-06-16 by @gfwilliams Hi, It's not out of the question but I think it's probably a bit early to assume it's an error in the interpreter source code. You may be hitting issues with the connection interval. Espruino negotiates how fast it'll talk with another device, and it may end up (especially for the first few seconds) having only a few intervals a second (so not enough for 10Hz). When you connect to a device, you can set the connection interval you want as an option - see http://www.espruino.com/Reference#l_BluetoothRemoteGATTServer_connect - and setting that to something far below 100ms should really help. The code you have won't manage 10Hz because it's running the next step 100ms after the write completes, and the write definitely won't be instantaneous. Once you've got a high enough connection interval, something like this should be possible:
But you're right, writeValue may cause an exception if the data hasn't been sent yet (eg it fails first and has to retry). In that case it should be possible to do something like this:
So now the next send is always queued up behind the last, even if the last hasn't completed. Obviously you might want some checking here because if you end up with a low connection interval, the chain of promises will just keep getting longer until you run out of memory! If you want to push output as fast as possible you could also see what I do for the UART code: http://www.espruino.com/modules/ble_simple_uart.js |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-06-16 by John Thanks @gfwilliams . The connection interval helps immensely! Thank you for directing me to that. I've just numbered the below so that it's clear they are unrelated to each other! (1) I will work through how to add a some good error handling. Currently I think one of the "tasks" got "lost", so any
(2) I've held BTN3, launched my app again, but my target BLE device has maintained the connection state, yet my app doesn't know about that. For robustness, maybe I need to store (3) In http://www.espruino.com/modules/ble_simple_uart.js -
Why is it |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-06-16 by @gfwilliams
Ok, task 6 is From your previous link though:
So unless I'm mis-reading, we seem to be doing the right thing...
It really shouldn't have... long-press BTN3 tears down everything (including connection state) and restarts it from scratch, so I'm not really sure what's going on there.
You're passing a function into |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-06-16 by John
Oh yes, of course, my lack of JS familiarity forgot that I had this in my mind:
which, I suppose is actually the same thing as
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-06-15 by John
I have this so far, for writing as fast as possible to a gatt characteristic. The timing seems to vary though, and certainly the writing isn't 'continuous'. Is there a way to do better? I want to write 10 times per second, as stable as possible.
I'm most familiar with writing in C or Python, so unfamiliar with Promises. I think I need to use them here, since if I simply call
char.writeValue(calculateValue())
in a loop then often there's an error that BLE is busy (from https://github.com/espruino/Espruino/blob/31c67baa20302b80aafc5b355aade9028a37c471/libs/bluetooth/jswrap_bluetooth.c#L78 )Beta Was this translation helpful? Give feedback.
All reactions