I'm implementing android bluetooth in Flutter and I have some performance issues. I'm doing a few writings to bluetooth characteristics in a row to upload device configuration. Unfortunately, I need to wait some time before each command, to be certain that all of them will be received by the device. I came up with simple solution to use Thread.sleep
to wait around 200 ms between each write. However it's bit bothering as it also freezes Flutter platform. Does anyone have better solution that would avoid making UI not responding? Thanks for all the help :)
Asked
Active
Viewed 66 times
1

Julian Modlinski
- 134
- 11
-
you mean native java code? – pskink Jan 25 '22 at 08:54
-
Yes, all the bluetooth functionality is done in Java on Android platform – Julian Modlinski Jan 25 '22 at 08:59
-
use `Handler.postDelayed` then – pskink Jan 25 '22 at 09:00
-
Can you tell me if I have 3 handlers in a row with 200 ms delay. The second one will start only after first is finished or all 3 will be run in parallel only with delay that was introduced by calling separate lines of code? – Julian Modlinski Jan 25 '22 at 09:03
-
the second case, so either call `postDelayed` when the previous callback is called (preferred way as you can call infinite number of callbacks that way) or call `postDelayed` 3 times with 200, 400 and 600 (bad, ugly way but if you have just 3 of them ...) – pskink Jan 25 '22 at 09:07
-
If I understand correctly I will have to nest function 2 handler inside handler of function 1? Could you provide me with some draft code, it doesn't have to be compileable just to check if we both have the same thing in mind – Julian Modlinski Jan 25 '22 at 09:54
-
https://www.programcreek.com/java-api-examples/?class=android.os.Handler&method=postDelayed – pskink Jan 25 '22 at 11:02
-
btw you can also use one custom `Handler` and override `handleMessage` so that you dont need any callbacks, something like: `Handler h = new Handler() { public void handleMessage(Message msg) { Log.d(TAG, "handleMessage " + msg.what); }; };` them you can "kick" your handler with `sendEmptyMessageDelayed` or some other "delayed" `Handler` method - for more see https://stackoverflow.com/a/25096981/2252830 – pskink Jan 25 '22 at 11:19
-
Great, thank you :) I will try this solution – Julian Modlinski Jan 25 '22 at 11:34