Serial not wanting to open if device got disconnected when connection open

Hi dear app inventors. I have an app that reads data from an ESP32 with the serial block.

I got everything to work fine, but am stuck with one issue. If the usb cable gets disconnected from the phone while the serial connection is active there are only two ways to get it to connect again: restart the phone, reinstall the app.

I am looking for a more graceful recovery. I tried all ways of closing the connection before opening, but still the same.

This is how I have it now

Screenshot from 2024-06-04 10-43-18

Any suggestions would be welcome!

Well, we can only see a fraction of your Blocks so no comment on your code. However, you should have a button to Close Serial if the cable gets disconnected, then you should be able to safely close the App and run it again with the cable reconnected.

That's correct. If I disable the serial connection before I close the app everything works fine. If for some reason the app is closed with the serial connection button on (so serial connection still open) when I get back to the app, there is no way to turn serial on again. Only reinstalling the app or restarting the phone. I imagine when the app closes and serial is still open that serial stays in the background or something. Honestly, no idea. Appart from that it works correctly. It is just annoying that the user would have to restart the phone if the app accidentally closes.

I wonder if your Screen Init code tries to Initialize Serial too soon after it was closed. Try moving Initialize Serial to a Clock Timer and give Close Serial a second to complete.

I have tried that too. It just looks like Serial doesn't close at all if the app closes when the connection is still active.

I guess what we would need is something like a terminate existing connections block or something. This is a quite annoying problem though, as it requires restarting the phone when the user forgets to close serial, or when some error closes or restarts the app.

I can suggest it, but no doubt it is difficult to deliver, otherwise I'm sure it would already be available.

I imagine. A bit of a dealbreaker though, for any app that needs the usb connectivity.