Bluetooth HC-05: Error 507 “Unable to connect” on Android 12+

Goal
I’m connecting to an HC-05 (Bluetooth Classic, SPP) from my MIT App Inventor app.

Environment
• Phone: Xiaomi Redmi Note 13 Pro 5G, Android 15
• Bluetooth module: HC-05, no PIN, paired in Android settings
• HC-05 AT info: ROLE=0 (slave), UART=9600, NAME=HC-05, ADDR=1A:1F:28:22:40:F5

What I do
• On Screen.Initialize I request BLUETOOTH_SCAN, BLUETOOTH_CONNECT, and ACCESS_FINE_LOCATION (Android 12+).
• ListPicker shows “MAC NAME” pairs, e.g. “1A:1F:28:22:40:F5 HC-05”.
• On AfterPicking I connect using the last token (NAME) or the MAC (first token). I also tried splitting by space and selecting index = length of list, as recommended.
• Non-visible components: BluetoothClientl Clockl GetApiLevell

Problem
After selecting the device, I get:
• Error 507: Unable to connect. Is the device turned on? (when using the MAC token or using full “MAC NAME”: 1A:1F:28:22:40:F5 HC-05)

What I tried
• Ensured device is paired and not connected by any other app.
• Toggled Bluetooth/Location, restarted phone.
• Requested permissions sequentially in PermissionGranted (SCAN → CONNECT → LOCATION).
• Tried connecting by MAC only (first token) and by NAME.
• Built a minimal AIA (attached).

Steps to reproduce

  1. Install APK and grant Bluetooth & Location permissions.
  2. Tap “Connect”, pick “HC-05”.
  3. Error 507 appears.

Expected vs Actual
Expected: Connects to HC-05 (SPP).
Actual: Error 507.

Attachments
• Minimal .aia
SmartCar.aia (41.9 KB)
• Blocks screenshots: Initialize, PermissionGranted, ListPicker Before/AfterPicking.




Questions

  1. Is my AfterPicking connect block correct for Android 12+?
  2. Do I need any additional permission flow or timing (e.g., delay before Connect)?
  3. Any known issues with ListPicker selection format on Android 12?

Thank you!

Ask for Bluetooth permissions correctly as shown here

First in Screen.Initialize ask for Connect, then in the PermissionGranted event ask for Scan
Also set the listpicker elements only in the before picking event

Taifun

2 Likes

In addition to Taifun's notes, what microcontroller is the HC-05 attached to? If Taifun's App corrections do not fix the issue, upload your Sketch/Script for the microcontroller.

1 Like

Hi, thanks for your reply.

I tried exactly as you suggested:

  • Screen1.Initialize → only calls AskForPermission "BLUETOOTH_CONNECT"
  • Screen1.PermissionGranted → if permission = "BLUETOOTH_CONNECT", then request "BLUETOOTH_SCAN"
  • ListPickerConnect.BeforePicking → sets elements to BluetoothClient1.AddressesAndNames
  • ListPickerConnect.AfterPicking → connects using only the MAC address (split from selection)

However, I still get the same error:
Error 507: Unable to connect. Is the device turned on?

My setup:

  • Android version: Android 15
  • Device model: Redmi Note 13 Pro 5G
  • Module: HC-05 (paired in phone’s Bluetooth settings, works fine with Serial Bluetooth Terminal app)

Here is my .aia file for reference.
SmartCar.aia (41.5 KB)

I’m still new to MIT App Inventor, so please excuse me if I made any basic mistakes.

Thanks a lot for your help!

If you are asking for help, I recommend you to make it as easy for others to be able to help you ...
You probably will get more feedback then...

which means in your case post a screenshot of your relevant blocks...

To download the aia file, upload it to App Inventor, open it, do some bug hunting for you, etc... this takes time, and most people will not do that...
Thank you.

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

Hi ChrisWard, thanks for your reply.

The HC-05 is connected to an NXP S32K144EVB microcontroller board.
It is programmed in C using the S32 Design Studio IDE.
This is not much more complicated than an Arduino sketch — it’s just using NXP’s HAL drivers.

Here is the current code running on the S32K144.
It is already able to send the string "SPEED:2;DOOR:CLOSED;TIRE:OK\n" to the Serial Bluetooth Terminal app successfully.
I haven’t tested the "BRAKE" command reception yet, but the TX part works fine.

#include "Cpu.h"
#include "pin_mux.h"
#include "clockMan1.h"
#include "lpuart1.h"
#include <string.h>
#include <stdbool.h>

#define TIMEOUT_MS         100U
#define BRAKE_LED_PORT     PTD
#define BRAKE_LED_PIN      0U
#define LED_ACTIVE_LOW     1 
#define UART_INSTANCE      INST_LPUART1

#define TX_INTERVAL_MS     500U 
#define BRAKE_ON_TIME_MS   5000U

static uint8_t  rxByte;  
static char     rxBuffer[32]; 
static uint8_t  rxIndex = 0;

volatile uint32_t brake_led_timer_ms = 0; 
volatile uint32_t ms_counter = 0;  

// ==================== LED control ====================
static void set_brake_led(bool on)
{
    if (LED_ACTIVE_LOW) {
        PINS_DRV_WritePin(BRAKE_LED_PORT, BRAKE_LED_PIN, on ? 0 : 1);
    } else {
        PINS_DRV_WritePin(BRAKE_LED_PORT, BRAKE_LED_PIN, on ? 1 : 0);
    }
}

// ==================== UART RX Callback ====================
void rxCallback(void *driverState, uart_event_t event, void *userData)
{
    (void)driverState;
    (void)userData;

    if (event == UART_EVENT_RX_FULL)
    {
        if (rxIndex < sizeof(rxBuffer) - 1) {
            rxBuffer[rxIndex++] = (char)rxByte;
            rxBuffer[rxIndex] = '\0';
        } else {
            // If full, reset
            rxIndex = 0;
            rxBuffer[rxIndex] = '\0';
        }

        if (rxIndex >= 5) {
            if (memcmp(rxBuffer, "BRAKE", 5) == 0) {
                brake_led_timer_ms = BRAKE_ON_TIME_MS;
                set_brake_led(true);

                const char *ack = "BRAKE_ACK\n";
                LPUART_DRV_SendDataBlocking(UART_INSTANCE,
                                            (const uint8_t *)ack,
                                            strlen(ack),
                                            TIMEOUT_MS);
                // Reset buffer
                rxIndex = 0;
                rxBuffer[0] = '\0';
            }
        }

        LPUART_DRV_SetRxBuffer(UART_INSTANCE, &rxByte, 1U);
    }
}

// ==================== Main ====================
int main(void)
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  #ifdef PEX_RTOS_INIT
    PEX_RTOS_INIT();                   /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of Processor Expert internal initialization.                    ***/
    // ===== Clock init =====
    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                   g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

    // ===== Pin init =====
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

    // ===== LPUART1 init (baud in lpuart1_InitConfig0 = 9600) =====
    LPUART_DRV_Init(UART_INSTANCE, &lpuart1_State, &lpuart1_InitConfig0);

    // Callback
    LPUART_DRV_InstallRxCallback(UART_INSTANCE, rxCallback, NULL);
    LPUART_DRV_ReceiveData(UART_INSTANCE, &rxByte, 1U);

    // ===== LED blue =====
    PINS_DRV_SetPinDirection(BRAKE_LED_PORT, BRAKE_LED_PIN, GPIO_OUTPUT_DIRECTION);
    set_brake_led(false);

    // ===== Main loop =====
    while (1)
    {
        // Delay 1ms (FIRC 48 MHz)
        for (volatile uint32_t i = 0; i < 48000; i++);

        ms_counter++;

        // LED BRAKE
        if (brake_led_timer_ms > 0) {
            brake_led_timer_ms--;
            if (brake_led_timer_ms == 0) {
                set_brake_led(false);
            }
        }

        // TX_INTERVAL_MS
        if (ms_counter >= TX_INTERVAL_MS) {
            ms_counter = 0;
            const char *msg = "SPEED:2;DOOR:CLOSED;TIRE:OK\n";
            LPUART_DRV_SendDataBlocking(UART_INSTANCE,
                                        (const uint8_t *)msg,
                                        strlen(msg),
                                        TIMEOUT_MS);
        }
    }
}

If you think the microcontroller code might be related to the MIT App Inventor connection issue (Error 507), please let me know.
I’m happy to share any further details or make adjustments for testing.

Hi Taifun, thank you for your feedback.
I understand it’s easier for others to help if I post the relevant blocks directly here.
I’ll upload screenshots of the important blocks instead of the .aia file so it’s faster for everyone to check.


Sorry for any inconvenience earlier — I’m still quite new to App Inventor and learning the best way to ask for help here.

The blocks look fine
Btw usually there is no need to extract the mac address; usually to connect, the listpicker selection as it is should work fine
Also the result of the connect method is true or false, so usually you use an if statement like this

If connect...
Then "connection was successful" 
Else "connection was NOT successful "

What you could do is to try another device to find out if it is a device specific issue

Taifun

Hi everyone, thanks for your help so far!

I followed the video step-by-step from this tutorial (https://youtu.be/aQcJ4uHdQEA?si=rXUCs_bfdHw3kle_), and used their GitHub project at https://github.com/binaryupdates/arudino-hc05-bluetooth. I duplicated everything exactly:

  • Wired HC-05 to Arduino as shown
  • Uploaded the same Arduino sketch (bluetooth_arduino.ino)
  • Used the provided .aia in App Inventor for controlling an LED

However, despite doing everything exactly as shown, the app still fails to connect — pressing the CONNECT button results in:

Error 507: Unable to connect. Is the device turned on?

What’s strange is that the HC-05 module can connect successfully with the Serial Bluetooth Terminal app on my phone.

I wonder:

  • Could this be an issue with my phone model or Android version (Android 12+ / Android 15)?
  • Or maybe a hidden setting in the HC-05 or pairing process?

I’d really appreciate advice on:

  1. Possible phone/Android-side issues (e.g., permissions, OS behavior, Bluetooth profiles)
  2. Whether I should modify any HC-05 AT settings (like device name, PIN, or baud rate) before use

Thanks so much — I’m still learning and grateful for any guidance from the community!

modify any HC-05 AT settings (like device name, PIN, or baud rate) before use

First thing, you do not communicate with the HC-05, you use it to communicate with the microcontroller. the HC-05 default settings are usually good to go.

Could this be an issue with my phone model or Android version (Android 12+ / Android 15)?
Or maybe a hidden setting in the HC-05 or pairing process?

No, given that you can connect to the Serial Bluetooth Terminal App on the same phone device.

The ListPickerConnect.Afterpicking block is wrong - the mac address should not be split and the ListPicker Elements should not be overwritten.

Concerning permissions, the code is not checking that scanning has been granted, which is critical with respect to the 507 error. So:

EDIT: ABG has pointed out that the Coarse Location isn't checked for in my Permission Granted code block - so that needs correcting. There is an alternative way of 'switching on' location - just drag and drop a location sensor into your design, that silently triggers Location on, even though the App doesn't actually use it.

Thank you very much, ChrisWard, for the clear explanation and for pointing out the issues in my blocks. I’m sorry for the late reply!

Your clarification about the HC-05 default settings, permissions, and the correct way to handle the ListPicker block is very helpful. I now understand that the MAC address should not be split and that the permission handling is critical for avoiding the 507 error.

I will adjust my blocks according to your guidance and also try the suggestion about enabling Location through the Location Sensor if needed.

Thanks again for your support!

I'm having the exact same problem. I've been about to lose my mind for three days. Did you find a solution?

Are you able to connect using a BlueTooth terminal program from the Play Store?

If yes, then

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.

export_and_upload_aia

.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.