ESP32 App works fine with 'AI Companion' but not autonomously (.apk loaded) on Android

I have a simple 2 LEDs Arduino (ESP32) sketch controlled by by an App on my Android. It works fine while it is tethered via AI Companion - I can Scan and see the BLE addresses on a ListView, select and connect to ESP32, turn on/off LEDs etc. However when I build the .apk and load it to my cellphone the ListView does not display the BLE addresses, it is blank. All buttons show up and everything looks like in the AI Companion tethered mode.

Please help, I don’t have much hair left…
Jacob

Hi Jacob

It could be simple…

Two settings on your Android device are required:

  1. Bluetooth
  2. Location

Also, ensure you have the Latest BLE extension, release 20181124.

Both Device requirements can be triggered within your App code.

BLE_DeviceSettings.aia (159.5 KB)

Sorry to say but things got from bad to worse when I imported the latest BLE extension 20181124. Lots of errors. I tried to load back the earlier extensions, which I had saved, and no luck. I worked on this app in October 2019.
The Bluetooth and Location are turned on.

Could you take a look and suggest a fix?
Thank you so much,
Jacob

ESP32_BLE_UART_LED_ok4.aia (3.5 MB)

Hello Jacob - we would need to see your Sketch (.ino) too.

Concerning the errors, that has happened because the BLE Extension already installed must be deleted from the Project before the new Extension is installed. Before deletion, you can temporarily store your BLE Blocks in the Backpack.

The Cleaned Up and Downloaded Blocks Image looks pretty crumbly.
There must have been a big difference in the blocks across versions.
I hope you have a prior Downloaded Blocks Image to work from,
or are good at jigsaw puzzles.

ESP32-BLE_Appinventor2_relay_v4.txt (3.7 KB)
Here is the .ino Sketch (I changed it to .txt so I can upload it)

Thanks for your help!
Jacob

Yeah, I did not expect such mess with the new BLE extension. I fixed up some of the blocks but I’m not sure about the two 'X’s and what to do about the ‘utf16’ which were not in the earlier version. The block names have changed so I picked the closest that I found. I am not the original designer of this, I’m new to this, sorry…
ESP32_BLE_UART_LED_new_extensions.aia (3.5 MB)

Jacob

There are lots of good BLE examples in this site, if you use the
search term FAQ BLE

@Jacob If I had a guess, your project uses the very old BLE extension (version 2). You can’t really mix the old version and the newer versions, as they use different packages and have different names for a number of blocks. It’s going to be a headache but if you plan to use any of the newer versions of the BLE extension (version > 2), you’ll need to go through your project and update all of your blocks. We no longer provide support for v1 or v2 of the extension.

FYI, the reason why this fails in the compiled app is because starting in newer versions of Android (6.0 Marshmallow), Bluetooth low energy needs either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION to scan for devices. A workaround you could do if you really can’t update to the new version of the extension is add a LocationSensor to your project, set its Enabled property to false, and then in Screen1.Initialize call Screen1.AskForPermission with ACCESS_COARSE_LOCATION.

Odds are at some point you’ve granted location permissions to your companion app, which is why it works there.

Yes, you are right, ok so I switched to the latest BLE extension and updated all my blocks and sure enough the original problem got fixed. When I do a SCAN the BLE values get displayed and CONNECT seems to also work and no more errors. However, the TX/RX buttons don’t do what they are supposed to do.


Any ideas?

Hi Jacob, here are examples of BLE and ESP32, maybe you might be interested:

Thanks Juan, this was very helpful. I am attaching my version of a the project that is bi-directional controlling 2 LEDs and receiving data from the ESP32. Also the Arduino sketch(change the txt to .ino) ESP32_BLE_LEDs_V3.aia (211.1 KB)

ESP32-BLE_Appinventor2_relay_v7.txt (4.4 KB)

Hi few days ago my Application was still able to see my ESP32 but now not even 1 can be seen followed what you'd suggested but to no avail. The only thing I noticed is my phone now is using Android 10.0 other than that can't recall what changes i'd done. Any help please.

Ragna

The update of Android OS is a tiny clue, but how are we to help you when we cannot see your App Inventor Blocks and ESP32 script/sketch?

What changes have you made since the last time the App worked?

Change uuid characteristic.

https://www.uuidgenerator.net/

BLE ESP32. Bluetooth. Send. Receive. Arduino IDE

A post was split to a new topic: Image shows in companion app but not in apk file?