PLS help me fix ERROR 507: Unable to connect. Is this device turn on?

Hello everyone,

I am developing an Android app using MIT App Inventor to communicate with an Arduino Mega through an HC-05 Bluetooth module. However, I keep getting Error 507: Unable to connect when I try to connect to the HC-05 using my app.

Here are the details of my setup:

  • Microcontroller: Arduino Mega

  • Bluetooth module: HC-05

  • Phone: realme C15

  • Android version: Android 11

  • Development platform: MIT App Inventor

What I have tried:

  1. I paired the HC-05 with my phone through Android Bluetooth settings.

  2. The HC-05 appears in BluetoothClient1.AddressesAndNames in my app.

  3. I use a ListPicker to display the paired Bluetooth devices.

  4. In ListPicker1.AfterPicking, I call BluetoothClient1.Connect using ListPicker1.Selection as the address.

  5. I enabled the location permission for my app and turned on the phone's Location service.

  6. I tried setting BluetoothClient1.NoLocationNeeded to true.

  7. I also tried setting BluetoothClient1.Secure to false.

  8. I tested the HC-05 using Serial Bluetooth Terminal on the same phone. It connects successfully, and I can send and receive data.

  9. However, my MIT App Inventor app still reports Error 507. This happens both when I use the installed APK and when I test with the MIT AI2 Companion.

When I select the HC-05 in my app, the alert displays both its MAC address and its device name, for example:

75:E6:58:74:09:D1 HC-05

I have also tried requesting the android.permission.ACCESS_FINE_LOCATION permission when the screen initializes, but the problem persists.

My questions:

  • What could cause Error 507 when another Bluetooth terminal app can connect to the same HC-05 on the same phone?

  • Should I pass the complete ListPicker.Selection string to BluetoothClient1.Connect, or should I extract only the MAC address?

  • Is there any additional Bluetooth permission or component property I need to configure for Android 11?

  • Could the problem be related to the HC-05's Bluetooth serial service (SPP) or the way BluetoothClient1.Connect is called?

I would appreciate any suggestions or a working example of the correct blocks for connecting to an HC-05 with MIT App Inventor.

Thank you for your help!

Hello luongg04

Well, I can see you have been working hard on this issue. I am not familiar with your phone so I looked it up. According to gsmarena, it has Bluetooth v5.0 i.e. BLE. However, they also say it's running Android 10.

  1. Can you check the Bluetooth spec on your phone? If it is BLE, we can't normally use an HC05, the two are generally incompatible. BLE and Classic are related but their systems are not similar.

  2. You should pass the whole string

  3. Permissions vary between Classic and BLE on Android from Android 12 and up.

  4. It's mostly likely related to a clash of BT systems, but:

(a) You are sending only single digits (chars), which if the world was fair you could get away with.
(b) Do not pair the HC-05 with your phone, that's an engagement outside of your App, it needs to be performed by the App.

EDIT: It is often the case that you need to give the App a couple of seconds to connect, which is easily achieved via a Clock Timer Block.

Is your App only ever going to be run by you on your C15? If so, and it definitely is running Android 11, then fine location is the only permission that needs to be granted when the App is launched.

If your App will be run on other phones too, potentially Android v12 and up come into play, so to cater for that we would test for the Android version and then request permissions accordingly