BLE extension disconnect not working

Hello.
I am trying to send and receive data via BLE UART from App Inventor to LEGO MINDSTORMS (SPIKE) programmed in Python.

I carefully read the topic that was marked as solved by @Gerriko_io and closed on september 17, 2022.

but still I can't make my app disconnect from the BLE peripheral device, in my case a LEGO MINDSTORMS hub.

It's not possible to connect using Connect with listpicker index, (possible solution found by who posted the original topic ) as it returns OS error 133. Passing the MAC address to the Connect with address works fine, and it quickly connects and start exchanging data. I am not using timers and Reads, but just Register for Strings and I can send data by pressing a button, so very sporadically.

There's no way to trigger the IRQ_CENTRAL_DISCONNECT event in the python script BLEHandler. The LEGO hub won't disconnect.

The python library is

and the script I'm running is

from projects.mpy_robot_tools.bt import UARTPeripheral
from utime import sleep_ms

server = UARTPeripheral(name="server")
print("Waiting for connection")
while 1:
    if server.is_connected():
        data = server.readline()
        if data:
            print(data)
            server.write(data+b" echo\n")
    else:
        sleep_ms(200)

I am using the latest build by @ewpatton (2022-09-02) that forces disconnection.
The other extension build I tried was BLE-device-sort.aix (2020-12-23) but didn't work either.

I remark that with the nRF connect app, I can connect, exchange data and disconnect correctly from the LEGO peripheral being controlled by the very same python script.

The project is posted below.

Thanks!

And can you screenshot your blocks and post it here?

How big is your aia?
Upload it to Google Drive and post the link (and also the relevant blocks as @gordonlu310 mentioned).

MINDSTORMS_BLE_UART.aia (194.8 KB)

It says the Android version is 11, it shows the LEGO hub after the scan.
But I don't see the point of this test, as your project misses the connection and disconnection logic.

I think I know the problem, mostly due to a bug.
I was selecting the device to connect to through a ListPicker rand then using Connect with Address.
It could not disconnect neither with Disconnect with address nor with Disconnect.

Now, thanks to your example, I tried to select the device using ListView and using Connect (with Index). This block accepts the listview index as valid, but didn't work with the index of the listpicker (gave OS error 133). Once connected with Connect (with index), I can disconnect every time with Disconnect (not Disconnect with Adress, which I now don't need to store in a variable).

Please @ewpatton take a look at this specific behavior of ListPicker vs ListView (in particular to the index they return) , and Connect with Address VS Connect

Thanks!

BLE-device-sort.aix (2020-12-23) is the latest version and the one you should use.

I am using that version indeed.
Why the IOT wiki is not reporting that as the latest version, but the previous one?

Hello Prof Bricks

Niggles in your Project - for example, if using a ListPicker, manually stopping the Scan is haphazard since you can't at the time see the List building. You can either automate the scan within a timer or use a ListView.

Stop Scanning does not need a while test - that is repeatedly sending the command and interrupting itself - no point in doing that.

There are a number of unattached Blocks.

'IsDeviceConnected' delivers true or false, just work with that, no need to impose a 'not'. Similarly, 'not' is not needed when using 'is list empty'.

When asking for permissions, you need to check what the response was. You can switch Bluetooth on using a BT Classic Client and Activity Starter. We usually use Fine Location but I don't know if that actually makes a difference in all cases.

I assume your App is for your own use only? Fixed GUI size only fits your phone and buttons that disappear/reappear are confusing (imagine that happening on your car console).

Concerning ListView v ListPicker, yours is the first report I have seen where there has been an issue with the Picker, but it is also the first BLE project I have seen using the Picker in App Inventor 190a. It's possible that there is a new App Inventor List Picker bug, but it's also possible that one of your devices listed has an illegitimate character in the device name (possibly a non-printing character). To test, populate a List Picker and a List View, then test the length of each respective row to see if they are truly identical.

Note that when you Connect with Address, the function does not check your List Picker or List View lists for the MAC address, it only uses the BLE Address list. Same goes for Connect With Name.

An example that you can use or abuse:
BLE_RECEIVE_STRINGS.aia (192.4 KB)

Hi, ProfBricks I'm really sorry to contact you. I m actually stuck on a problem I want to make an app to control my spike Prime with mit app inventor just like ev3. I have downgraded and installed the firmware code is uploading but I'm unable to create a code for it. And I'm a noob myself also I'm not being able to understand your whole code. Can you please guide me to create an app and code for the same. In Lego mindstorm spike Prime with app inventor firmware and mit app inventor. Please🥺

Sir ChrisWard, can you help me create an app in mit to remote control my spike Prime with ble. Also python code for the same. I'm beginner and not being able to achive it.:smiling_face_with_tear:

Sorry, I'm not available.

I suggest you start your own post, describe your project and the associated hardware. Explain where you have an issue you need help with, or specific things you don't understand. There are many other Power Users that can help.