Failure to connect when using the nRF Connect mobile app for iPhone.

Howdy,

I am using an nRF52840 to perform BLE communication. For this communication I am using extended advertising to perform most of my data transmission. Thing is though, sometimes I need to connect to the device to perform updates and send commands. I set up all the connection callbacks and have the system reestablish advertising after disconnecting. To test all of this, I decided to use the nRF Connect app to test the connection. The problem I am facing is that when I try to connect, it seems to get stuck in a loop of connecting and disconnecting. My phone simply says that it is trying to connect, while my code shows that it is connecting and disconnecting rapidly. I have attached the setup functions and my callbacks for the connection in the code below. Any help would be much appreciated.

static struct bt_conn *default_conn;
struct bt_le_ext_adv *adv;
static struct bt_le_adv_param adv_param = {
	.options = BT_LE_ADV_OPT_USE_IDENTITY | BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_EXT_ADV,
	// .interval_min = BT_GAP_ADV_FAST_INT_MIN_1, // Every 1 to 3 seconds
	// .interval_max = BT_GAP_ADV_FAST_INT_MAX_1,
	.interval_min = BT_GAP_ADV_SLOW_INT_MIN,
    .interval_max = BT_GAP_ADV_SLOW_INT_MAX,
	.peer = NULL
};
struct bt_data adv_data[] = {
	BT_DATA(BT_DATA_MANUFACTURER_DATA, "Random Junk", 12),
};

static void app_led_cb(void)
{
	byte_status = byte_status | 0b00000001; // Set LED status bit to 1
	nrf_gpio_pin_set(LED_R);
	k_timer_start(&led_timeout, LED_TIMEOUT_SECONDS, LED_TIMEOUT_SECONDS);
}

static struct my_lbs_cb app_callbacks = {
	.led_cb    = app_led_cb,
};
static void connected(struct bt_conn *conn, uint8_t err)
{
	if (err) {
        #if INCLUDE_PRINT_STATEMENTS
                printk("Connection failed (error 0x%02x)\n", err);
        #endif
    } 
    else 
    {
        #if INCLUDE_PRINT_STATEMENTS
                printk("Connected\n");
        #endif
    }

	default_conn = bt_conn_ref(conn);
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
	printk("Disconnection started\n");
	bt_conn_unref(default_conn);
	default_conn = NULL;
}

static void recycled_cb(void){
	int ret = bt_le_ext_adv_start(adv, NULL);
	if (ret) 
	{
		#if INCLUDE_PRINT_STATEMENTS
				printk("Advertising failed to start (err %d)\n", ret);
		#endif
	}
	printk("Successfully Disconnected\n");
}

static struct bt_conn_cb conn_callbacks = {
    .connected = connected,
    .disconnected = disconnected,
	.recycled = recycled_cb,
};

  • Hello,

    I guess you are seeing a lot of "Connected" and "Disconnection started" in your log? If so, the first thing I would check is the disconnect reason. Try printing it in your disconnected event:

    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
        printk("Disconnected, reason %d\n", reason);
        bt_conn_unref(default_conn);
        default_conn = NULL;
    }

    What does it say? The values from the reason parameter is represented in ncs\zephyr\include\zephyr\bluetooth\hci_types.h.

    I don't know if you are using bonding or not, but if you are, a common pitfall is that the bonding data is stored on one device (phone or nRF), and not the other one. Make sure that it is deleted from both devices. On the phone, there is typically a "forget device" option in the phone's settings -> Bluetooth menu.

    On the nRF, the quickest way is to erase the flash. If you are using VS code, press the icon next to the "flash" button, which will do a "erase and flash". If you are using command line, you can call: " nrfjprog --eraseall" before flashing the application.

  • Hi Edvin,

    Sorry for the late response.

    I added the code to pull the error code. The error code is 0x3E with the define of BT_HCI_ERR_CONN_FAIL_TO_ESTAB.  I am not currently using bonding for this project. I plan to have it just perform the pairing for every connection performed.

    Even so, I should note that my phone is not getting a connection. The nRF detects the connection request but no connection is being fulfilled. This is what I believe is causing the error. The question is why it is not connecting. I have tried altering the broadcasting interval but even still it is not connecting. Hope you have a great day.

  • I figured out the solution to this. I had set the BLE connection parameters in 'BT_LE_CONN_PARAM' at too long of an interval. I lowered the low interval from 30 ms to 10 ms, and the upper interval from 4 s to 30 ms. I also set the supervision interval to 720 ms from the original 4 s. This ended up allowing it to connect within 30 seconds instead of never connecting. I will note that when wearing Air Pods and trying to connect, it took roughly 5 minutes to fully connect, so probably will have a bit of trouble in high interference environments. 

  • TheLoneRanger said:
    it took roughly 5 minutes to fully connect, so probably will have a bit of trouble in high interference environments

    That seems like a very noisy environment. 

    If you want to get to the bottom of why it didn't work in the first place, I suggest you try to capture a sniffer trace of the failed connection attempts. 

    You can do so using the nRF Sniffer for Bluetooth LE (requires an extra DK). If you do, feel free to upload the sniffer trace here (.pcapng file), and I can help you analyse it.

    Remember to select the advertising device in WireShark before you try to connect to it with the phone, so that the sniffer will follow that device into a connection.

    Best regards,

    Edvin

  • Sorry for the late reply. Took a bit to get the dongle and to get everything setup. I cannot see anything within the log file that would be a major source for the connect timing. Any feedback would be greatly appreciated.

    iphone connecting to device.pcapng

Related