How can I catch bluetooth errors (Error 515 f.e.)

I am working on a remote app for an Arduino bluetooth device. Everything works great except two things that are a bit annoying:

First:

When I turn off the bluetooth device on the Arduino, the app pops up lots of "Error 515: Not connected to a Bluetooth device" messages (every 2 seconds or so). This is very annoying so I would like to be able to catch this error and let the user know ONCE that there is no connection anymore.

Is there a way to check if the connection is still alive - or to internally catch the "Error 515" so the app could close the connection to prevent this error message from showing up again and again?

Second:

If the app was suspended for a longer time and then resumed the BT connection no longer works. Is there some kind of "onAppResume" event that I can use to catch this event to automatically disconnect, then re-connect again?

Hello MauMau

You can catch the error (there is more than one that indicates a loss of connection) in a Screen Error Block.
See my website for App Inventor Error Messages:
https://www.professorcad.co.uk/appinventorerrormsgs

Screen Error Block (Bluetooth error block no longer required)
https://www.professorcad.co.uk/appinventorsnippets#GetErrorInfo

If the connection no longer works, the reason should be a disconnection, so essentially the answer is the same.

1 Like

For the first problem, consider having each device say "Goodbye" to the other side when it decides to shut down?
Conversely, when receiving the message "Goodbye', stop expecting input.

you could use the ActivityStateChanged event from the tools extension like this

if state = resume
then ...

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

1 Like

Thanks Taifun and ChrisWard, your solutions solved both issues for me.

I am able to catch the 515 error now (in this case the app will terminate the BT connection and disable all associated timers).
detectError

Additionally, I am now using the ActivityStateChanged extension to detect if the app was paused. If so, the current timestamp will be saved. When a resume event occurs, the app checks how many seconds it was paused. If it was paused for more than 5 minutes, the BT connection will be terminated and the app tries to auto-reconnect to the last remembered BT address.

Both work fine so far :relaxed:

@ABG: a "goodby" solution would not work since you never know when a BT connection breaks (app was paused for too long etc.)

EDIT:
The "stop" event seems to be triggered as soon as the app is sent to back. That's a bit suprising because there is no difference between the "stop" and the "pause" event then :thinking:

1 Like

Yes, see here:

1 Like

Hm, interesting :thinking:

Luckily, this does not make a huge difference for my current project since I just need some way to detect if the app is sent to the back or resumed (and being able to measure the time between both). So in my case "stop" works as well as "pause" as far as I tested yet.

I just updated my block to:

Yes, and as you can see in my tests, OnStop is triggered about 40ms after OnPause.

grafik

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.