Cannot connect to nRF when in direct advertisement mode

Hello, I'm trying to manage connecting to my board (nRF52840-DK) which direct advertise (low prio) with peer address set to MAC address of my PC running Linux with bluez 5.62-1. Unfortunately I cannot connect to the board, no matter what. I develop the board with Zephyr RTOS. This is the code responsible for setting up advertisement:

static void bt_ready(void)
{
  int err;
  bt_addr_le_t dir_addr;

  const char *address = "D8:8F:76:5B:57:7E";
  const char *type = "public";
  if ((err = bt_addr_le_from_str(address, type, &dir_addr))) {
    printk("Bt_addr_le_from_str error: %d\n", err);
  }

  err = bt_le_adv_start(BT_LE_ADV_CONN_DIR_LOW_DUTY(&dir_addr), ad, ARRAY_SIZE(ad), NULL, 0);
  if (err) {
    printk("Advertising failed to start (err %d)\n", err);
    return;
  }

  printk("Advertising successfully started\n");
}

If i set the advertisement mode to BT_LE_ADV_CONN_NAME, then there is no problem - the device is scannable and connectable.

My question is - can someone post any code or describe any method that will allow me to connect my PC to the board?

  • I found what was the issue. The  `bt_le_adv_start` function calls at some point `bt_conn_exists_le`, which looks like this:

    bool bt_conn_exists_le(uint8_t id, const bt_addr_le_t *peer)
    {
    	struct bt_conn *conn = bt_conn_lookup_addr_le(id, peer);
    
    	if (conn) {
    		/* Connection object already exists.
    		 * If the connection state is not "disconnected",then the
    		 * connection was created but has not yet been disconnected.
    		 * If the connection state is "disconnected" then the connection
    		 * still has valid references. The last reference of the stack
    		 * is released after the disconnected callback.
    		 */
    		BT_WARN("Found valid connection in %s state",
    			state2str(conn->state));
    		bt_conn_unref(conn);
    		return true;
    	}
    
    	return false;
    }

    So, you cannot start advertisement in the disconnected callback.

  • That's correct, you need to wait for the disconnected callback to return before stariting another advertisement "session". Thank you for sharing your solution in details. It will likely help other DevZone users with the same issue in the future!

    Best regards,

    Simon

Related