ESP32 BLE can not connect

请帮帮我,我只能搜到ESP32的蓝牙,但是无法与其连接,错误代码os code4;ESP32上的代码使用micro python写的,经过nrf connect等蓝牙调试器的测试都能正常连接与通信,但始终无法与生成的app连接上;论坛里诸位大神的代码我也都失败了。
Please help me, I can only search for the ESP32's Bluetooth, but I cannot connect to it, with error code os code 4. The code on the ESP32 is written in MicroPython, and after testing with Bluetooth debuggers like nRF Connect, it can normally connect and communicate. However, I am always unable to connect with the generated app; I have also tried the code from experts on forums, but all attempts have failed



micropython.txt (4.0 KB)
esp32_2.aia (200.5 KB)

Hello 吠唐 鱼

What is the make/model/Android version of your phone?

meizu 20pro android 14

An excellent phone, shouldn't be an issue. I'm going to take a look at the files you have uploaded. Android 14 has some strict rules for compiled Apps which the projects on the forum don't necessarily cover.

Thank you very much

A few things with the App Inventor Project:

  1. There should not be a need to manipulate the target address picked from a List - the BLE ConnectWithAddress Block does that for you.

  2. Stop Scanning After Picking (no later than that)

  3. You have Registered For Strings - that is to receive stream of data, but you are only sending data with your Blocks.

  4. On intialising Screen1, you must request permissions for Android 14. How you go about doing that depends on whether the App is only for your Use on your Android 14 phone or if it will be used on other phones and other Android versions. Let us know so we can guide you more.

  5. Drag a Location Sensor into the Designer. Your App doesn't need to have a use for it, it's just to satisfy Google's requirement for Bluetooth use.

Concerning the Script:

  1. Use Switch instead of if elif

  2. The values expected are not the values sent from the App (BLE_MSG). When you sort that out, do not use "!" in the message. You should be able to use a single character for each (A = start_power(), B = add_power etc).

  3. I'm not that familiar with MicroPython (We use Arduino C for ESP32) but neither the reader nor the sender UUIDs have a type definition as far as I can tell.

Example Switch Case:

Thank you, I need some time to study; when it comes to coding, I am still a beginner

Regarding the '!', it's because I found an app to control a car via Bluetooth. After testing, the information that its control buttons pass to the ESP32 is just these. I have temporarily completed my DIY for the lights using it, but I still hope to have an app that can more intuitively control the lights

Hello, first of all, thank you for your help. I have successfully completed the connection with the ESP32. However, during the process of transmitting information, I found that the bytes sent from the app come with a square box, and I am unable to write this square box into MicroPython.


1732099786508
1732099870527

I have already solved my problem
1732103697951

Yes, the 'box' is a line feed character? Read the 1st character of the string received into BLE_MSG, which should be BLE_MSG[0]. I understand that requires Python 3.x

....or this might work on the App side instead:

Snap1

Thank you for your help, the method worked very well.