How to Disconnect BLE After Companion Screen Refresh

I'm using the official BLE extension. No issue connecting or sending/receiving data. But, whenever the Companion App Screen refreshes I can no longer interact with the BLE device. This occurs with every Design change.

Understandable, however... the connection persists. The device still thinks it is connected and if I close the companion app the connection does terminate.

If I try disconnecting, I get the following error:

Error 3000: Error 9005 in extension BluetoothLE: Disconnect cannot be called before you are currently connected.

Do you see the same behavior if you compile the apk rather than live test with the Companion?
Do you change real Screens in your app? You might want to show an image of your relevant Blocks.

You may have to provide a StayAwake routine or extension to maintain the connection.

By screen refresh I mean choosing Refresh Companion Screen in App Inventor's Connect menu or from making any changes to the design in Design view.

I expect a refresh to start everything over, but a BLE connection doesn't appear to be fully terminating.

Fortunetely, that is the way it works. If you refresh the companion screen (and that happens automatically when you make a change in the design), then everything is reset. Otherwise it would be really difficult to know what to initialize again etc.

@Ghica. I am expecting everything to reset when the screen refreshes, but...

It doesn't appear that the BLE Extension is fully resetting. I would think it should be terminating its connections upon a reset. It is not, and it leaves connection un-disconnect-able.

Extensions are not an integral part of the app inventor and I don't think there's anything we can implement in extensions that would prevent this problem. Just disconnect before making any changes in the designer, or don't make changes while bluetooth is connected.

I am wondering about this, because I never had this problem with my micro:bits. What is the BLE device you are using?

Communicating with VEX over Bluetooth LE (BLE). My school already has these and I thought students would be excited and inspired to combine them with app development :smiley:.

Is it possible the extension management in App Inventor proper is missing a step (for all extensions)? This situation sounds like resources are being leaked.

I am disciplined enough to connect/disconnect/connect/disconnect, but I can't say the same for high school students who are focusing on their learning.

I think the extension should support the activity lifecycle, and in the onDestroy method it should disconnect the obtained bluetooth connection. I haven't looked into the ble extension sources, so I don't know if it does that.
In fact, I don't even know if the BLE extension has an open source codebase.

1 Like

The BLE extension source code is here: appinventor-extensions/appinventor/components/src/edu/mit/appinventor/ble at extension/bluetoothle · mit-cml/appinventor-extensions · GitHub

1 Like

Thanks, I was looking in this repository but I didn't know that extensions are in different branches. If you want I can make changes, compile and send it to you for testing.

1 Like

@Patryk_F I will gladly test any changes you make. Thank you!!

If you're looking to submit a patch, you'll want to have BluetoothLE implement OnClearListener and then have its constructor call registerForOnClear with the form. The onClear method is called when the form is refreshed.

2 Likes

I didn't even know there was an interface for refreshing the screen in Companion. Thanks.

The repository probably contains an old version of the extension v20200828

What branch are you on? The tip of the extension/bluetoothle branch shows 20240822:

1 Like

Yes, I had an outdated fork created a few years ago.

I sent a PM

Amazing! Patryk_F's changes work as expected. If I make a change to the Screen1 design and the companion app refreshes, the connection closes.

App Inventor + BLE Remote Control has been a huge hit with my students as an activity to do during this (crazy) week before winter break.

Thank you all for helping end the year on a high note! Hopefully this change meets ewpatton's approval and can be included for others.

1 Like