BLE connection failure esp32 Arduino 10

I get inconsistent connection behaviour in between android 10 on Samsung galaxy S9 and ESP32 Wroom 32D.
See attached files for code.

I use Arduino IDE V 2.3.2 with latest BLE version (I think it is ble V3.0.3 as showm while compiling code.
On the App Inventor side i use BLE extension 20230728.
Most of the time connection fails OS code 133.

If I use nRF connection to ESP32 always succed.

I have a few other BLE devices, One (and only one) can be successfully connected using the attached aia application. Unfortunately this is a proprietary device and I have no info on the way it is programmed. Anyhow this make me believe that my ai aoolication has appropriate permission to establish a connection.

I get the same behaviour on an android 11 on a Xiaomi 11lite 5G NE,

Thank you for any help.

Server.ino (1.5 KB)
Test_BLE_connect_copy.aia (197.5 KB)

There is more than one BLE library, make sure you use the one specifically required by your board. Code 133 is normally associated with a different type of failure, for example the size of the bluetooth packet exceeding the size of the MTU, which by default is about 23 bytes.

So your BLE devices are listed, ready to select? Note that you cannot necessarily just connect to a listed device - that depends on how the manufacturer has set it up.

You should not stop scanning after picking - during picking, other devices could be added to the List. Stop Scanning, then pick.

I see you have the BLE Connection Failed Block - add the Screen Error Block.

Drag-drop a location sensor into the Designer.

You have a null request for a "Bluetooth" permission. Remove that, but ensure both devices have Bluetooth switched on and both actually support BLE.

Concerning permissions, they are related to the Android version, so you should only apply them when appropriate. Simplest way to do that is to test the version first, using Anke's GetApiLevel extension:

What is your actual App Code going to do to interact with the ESP32?

Thank you for your fast answer, it really help me solving the problem.
First I implemented all your recommendations in app inventor application. That did not change anything to the problem but I will keep them in my final app as "best practices".
Next, I thought about your comment on the MTU size. On the ESP32 code, I changed the "advertizing" long name (19 ascii bytes) to fairly short name (3 char). Connection is now successfull both on the small Arduino IDE demo app as well as my quite larger app. So far so good since I get a working environment, nevertheless I would like to use larger name, and will try negotiating larger MTU size.

The way I plan to use BLE is to exchange strings between client and server (sending quieries from client and receiving corresponding replies, sending configuration parameters ) and also sending integers (values read from peripherals like temperature an humidity sensor) from server.
again big thanks for your detailed answer.

1 Like

unfortunately my app fails to connect again with Os err 133 after working for about a week. I did neither rebuild/install the apk, nor the arduino demo app during that time!
I stop/restart bluetoth on the phone with no effect on my problem.
I strop/start Android still with no effect.
I deleted the Android app and recreated it. Problem still present.

Note that I have a second phone (on android 11) that I almost do not use. The connection establishment (still) works on that phone, by the way indicating that the arduino app is ok.

So to me it looks like the android app is accumulating data up to a point where it cannot establih connection anymore,

Any idea of what I could do next to get around this weird problem?

Thanks

1 Like

It either has a connection or it hasn't - has something changed in the local environment? You do stop scanning before picking the device? (if you are only ever going to use the one device, it's address can be hardcoded).

Where is the data stored?

HI ChrisWard,
I did not changed purposidely anything in the environment, but I use the smartphone and not sure if android or some app made change behind the scene.
Yes I stop scanning before picking ( I created a stop scan button and call stop scan when button hit).

I plan using multiple devices, neverhteless I tried "connect with adress" and used the MAC address displayed during a scan. Still fails with OS code 133

Thanks

I do not store any data in my application, only using local and global variables. I was just thinking that the App inventor environment or application could save some data and not cleaning up completely.

Gerard

From a web search:

The problem turned out to be that the device was trying to send a packet larger than the MTU size, which defaults to 23 in Android, or at least it does on the particular devices I'm testing with.

Requesting a larger MTU size did fix the problem.

Hello ABG
I already request a 512 MTU size (call to BluetiithLE1.requestMTU size 512) but I realize that the BLE.MTUChanged never triggers.

I could not find a procedure to ask for the actual MTU size.

Is there a specific sequence on when to issue the requestMTU size?

Tomorrow I will write an as simple as possible App Inventor App to connect to Arduino BLE.

Hello Gerard

The devices have to be connected. The ESP32 may not necessarily accept an MTU change and usually we program to live with 20byte packets of data.

Looking at your 'Test_BLE_connect_copy.aia' Project, you have not got the permissions right.

It's also best to check the Android API Level such that should the phone be swapped with another, the App won't fail on permissions. There is no such permission called "BLUETOOTH".

Drag and drop a location sensor component into the Designer and upgrade to the latest BLE Extension.

We can't see what you are actually doing from your Test file!

I still have the OS error 133.
I write a new aia prograam to make it more readable and built in some "options" to test without loading a new app each time.
I remove request for permission for "BLUETOOTH".
Permission for fine location is executed (1 time) and I get prompted for acceptance (I do accept). I do not have specified any location peripheral (like phone GPS), Should I?
Since I am using Android 10, permission requests for bluetooth Scan and bluetooth connect are bypassed.

If I hit buttons to request new MTU size, the request is executed but the BLE,MTU changed is not triggered (make sense since connection not established).
If i hit button "connect for MAC" or "scan/stop scan/pick from list and Connect from list" I get this error 133.

Using nRF Connect I can connect to my esp32 server.

Any recommendation mostly appreciated.

Server.ino (1.6 KB)
BLE_connect.aia (207.1 KB)

Firstly, it is best to write your code for the future, unless your set-up is only every going to use an Android 10 smartphone. Android 10 requires location.

If we look at what data you are sending, we may also see if it can avoid being too large.

We can do that, but we do not send human readable sentences as strings, we send a single character representing the query.

For example, your Client might want to ask the Server for the water temperature.

We would simply send 'T'. The ESP32 script would interpret this as a requirement to send the Client the water temperature. The Server sends only the value, without any 'dressing':

So it sends '32', not "The water temperature is 32 degrees C".

This is the technique we use to keep the data size small.

thank you. At this point I am really looking at establishing the connection. I already ask for fine location.

I understand there is no 'dressing' of the trasfered data and plan to use short commands and replies

Gerard

Well, I have been testing this end (with edited code) and interestingly on Android 10 BLE 5.0, the BLE scan never starts! Android 10 should not need permission to scan or connect. I'm looking into it tonight (UK time).

HI Chris. It seems Android10 BLE 5 needs the location permission to allow scan.

It has got location in my code - Android 10 should not require any permissions.

What I have discovered is that the scan works if the App is run by the Companion. Also, the scan takes much longer than before, measured in minutes rather than seconds.

Hi Gerard

BLE_connect_edit.aia (227.6 KB)

Try this on Android 10. Try it in the Companion (v2.72) and afterwards download and install as an APK. In each case, exit the App via it's exit button so that the log file is appended with everything reported in the Status Label. The Companion will moan about this as you can't close the App within the Companion.

To access the log file, you may need a File Manager. I use:

HI ChrisWard. Thanks gor your time and effort to help me.

I tried your modified app.

1st) I am unsure what you mean "ensure BT and location are on". OK for BT, but I do not find any place where to configure "location".

2nd) On first execution of app, I am ask for finelocation permission (I accept), but scan does not start (nothing shows up in the list and I get an error if I hit stop button key stating scan is not started)
I stop the app with the red exit button.

3rd) on next execution I am ask for permission on picture, multimedia content and files I accept). Scan works and BLE devices appears very quickly in list. I stop scan, pick the device I am interested in (ESP32 with Arduino latest BLE lib -upgraded to 3.0.4 yesterday) and try connect => unfortunately get this error 133. See attached log file.

BLE_log.txt (279 Bytes)