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?
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
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.
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).
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.
@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
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.