Staying connected to Bluetooth when changing screens?

1,580 views
Skip to first unread message

Ronster

unread,
Feb 13, 2018, 12:29:23 AM2/13/18
to MIT App Inventor Forum
I've worked on this app for weeks and was just polishing it off when I realised that the phone does not remain connected via Bluetooth to a device when I change screens.
I have an initial "Connect" screen with various housekeeping tasks like "Rename device", etc, but when the app changes to another screen which interacts with the device (logging, changing settings, etc), the connection is lost.
The second screen is chock-a-block with controls and textboxes, so there's no room there to add anything else.
If this can't be worked around, it would be a huge obstacle.
Is there any way of keeping alive the Bluetooth connection when changing to another screen?
Any advice/comments welcome.

SteveJG

unread,
Feb 13, 2018, 8:50:20 AM2/13/18
to MIT App Inventor Forum
You would simplify and get fewer run time errors by using virtual screens.  Use virtual screens and no Bluetooth connection issues.  Not enough room?  Set Screen to scroll or use a scrolling layout?


The best you can do when using multiple screens with a bluetooth device is discussed in this Forum thread  https://groups.google.com/forum/#!searchin/mitappinventortest/bluetooth$20screens%7Csort:date/mitappinventortest/AoV5MB5CTLs/jzvcLKw_BgAJ  

A bluetooth  device disconnects when a developer switches screens  That is the way bluetooth works with App Inventor.  There is a partial work around for passing data between screens discussed in the above thread that might work for you.  The real solution is to use a virtual screen instead of actually changing Screens.  When you change actual screens the bluetooth disconnects from the first screen.
  • Building Apps with Many Screens explains how to use a virtual screen (simply using a two or more horizontal or vertical layout controls as screens and turning them on or off using their Visible property from true to false or vice versa).  If you use virtual screens, your bluetooth will not disconnect.
Regards,
Steve

Ronster

unread,
Feb 13, 2018, 9:32:10 PM2/13/18
to MIT App Inventor Forum
Ah thanks, Steve, that does seem to be a good solution, although with 3 'screens', one with multiple arrangements being hidden/unhidden, I have an interesting coding time ahead of me.
(Is 'coding' the right term to use when we're using blocks?).

I now see that many others have come up against this problem.

Thanks again for your help.

Ron

Larry Deack

unread,
Feb 14, 2018, 1:51:39 AM2/14/18
to MIT App Inventor Forum
  Using the layout stuff makes it trivial to swap out screens by hiding groups components with one call. There are some very elegant "code" blocks by some very good coders if you look for them. I think it may help beginners avoid the mess all too common in environments like Arduino and they work well together.
Reply all
Reply to author
Forward
0 new messages