<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/81214/cannot-connect-to-nrf-when-in-direct-advertisement-mode</link><description>Hello, I&amp;#39;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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 05 Nov 2021 09:11:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/81214/cannot-connect-to-nrf-when-in-direct-advertisement-mode" /><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/337632?ContentTypeID=1</link><pubDate>Fri, 05 Nov 2021 09:11:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef50f052-f14d-4ff5-ba95-8a3111aff927</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;That&amp;#39;s correct, you need to wait for the disconnected callback to return before stariting another advertisement &amp;quot;session&amp;quot;. Thank you for sharing your solution in details. It will likely help other DevZone users with the same issue in the future!&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/337548?ContentTypeID=1</link><pubDate>Thu, 04 Nov 2021 15:32:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:38612fd3-ecad-4ec3-acec-aa7c2f97d195</guid><dc:creator>fmalinka</dc:creator><description>&lt;p&gt;I found what was the issue. The&amp;nbsp; `bt_le_adv_start` function calls at some point `bt_conn_exists_le`, which looks like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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 &amp;quot;disconnected&amp;quot;,then the
		 * connection was created but has not yet been disconnected.
		 * If the connection state is &amp;quot;disconnected&amp;quot; then the connection
		 * still has valid references. The last reference of the stack
		 * is released after the disconnected callback.
		 */
		BT_WARN(&amp;quot;Found valid connection in %s state&amp;quot;,
			state2str(conn-&amp;gt;state));
		bt_conn_unref(conn);
		return true;
	}

	return false;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So, you cannot start advertisement in the disconnected callback.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/337446?ContentTypeID=1</link><pubDate>Thu, 04 Nov 2021 11:41:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8a997c26-9d57-419d-9ba0-3e4ea94ae1cd</guid><dc:creator>fmalinka</dc:creator><description>&lt;p&gt;Ok, I managed to connect to the board using &lt;a href="https://www.bettercap.org/"&gt;bettercap&lt;/a&gt; tool. Now, what&amp;#39;s happening is that after my PC disconnects from the board I cannot start direct advertisement again. What I tried to do was to start it in the disconnection callback:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void bt_start_adv_direct(const char *address, const char *type) {
  printk(&amp;quot;Begin direct advertising to %s (%s).\n&amp;quot;, address, type);
  int err;

  bt_addr_le_t dir_addr;
  if ((err = bt_addr_le_from_str(address, type, &amp;amp;dir_addr))) {
    printk(&amp;quot;Bt_addr_le_from_str error: %d\n&amp;quot;, err);
  }

  err = bt_le_adv_start(BT_LE_ADV_CONN_DIR_LOW_DUTY(&amp;amp;dir_addr), ad, ARRAY_SIZE(ad), NULL, 0);
  if (err) {
    printk(&amp;quot;Advertising failed to start (err %d)\n&amp;quot;, err);
    return;
  }

  printk(&amp;quot;Advertising successfully started, directed to: %s\n&amp;quot;, address);
}

static void disconnected(struct bt_conn *conn, uint8_t reason) {
    printk(&amp;quot;Disconnected (reason 0x%02x)\n&amp;quot;, reason);
    bt_start_adv_direct(address, type); // global arrays
}  &lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Logs from nRF:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;*** Booting Zephyr OS build v2.6.1-rc1-21-g42d7e712063b  ***
Set up button at GPIO_0 pin 11
Set up LED at GPIO_0 pin 13
Address: DB:94:18:97:FE:0D (random)
Bluetooth initialized
Begin direct advertising to 18:1D:EA:9C:EF:E5 (public).
Advertising successfully started, directed to: 18:1D:EA:9C:EF:E5
Connected
Disconnected (reason 0x13)
Begin direct advertising to 18:1D:EA:9C:EF:E5 (public).
Advertising failed to start (err -22)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;As you can see, advertisement sets up when I call the `bt_start_adv_direct` for the first time in my main function, but it fails when called from the `disconnected` callback. I don&amp;#39;t understand why the error code is 22 (afaik it&amp;#39;s &amp;quot;Invalid argument&amp;quot; error). What should I do to restart advertisement after disconnection?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/337409?ContentTypeID=1</link><pubDate>Thu, 04 Nov 2021 08:41:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f3198c9-2509-4e4e-b5e3-a6eb1e64fbfb</guid><dc:creator>fmalinka</dc:creator><description>&lt;p&gt;You&amp;#39;re right, it is possible to scan for a device that advertise directly (I managed to do it using hciconfig and hcitool). However I just cannot manage to connect to the device, even though my scanner sees it. I&amp;#39;ll dig into it. Thanks for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/336846?ContentTypeID=1</link><pubDate>Mon, 01 Nov 2021 11:53:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:37c259c0-93e2-46e9-b42d-3af28226251f</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Raspberry Pi 4 and BlueZ v5.62-1 should support directed advertising from what I can tell, but please make sure that your scanning device is doing&amp;nbsp;&lt;strong&gt;active&lt;/strong&gt; scanning. I&amp;#39;m not too familiar with the BlueZ stack, so you&amp;#39;ll need to find out exactly how, but I assume you set this in the scan parameters.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/336699?ContentTypeID=1</link><pubDate>Fri, 29 Oct 2021 13:51:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:496e0e6e-7a9b-4589-a6a5-1617fcdcaf65</guid><dc:creator>fmalinka</dc:creator><description>&lt;p&gt;Hi Simon, thanks for a fast reply.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure if my PC supports a direct connection. How can I check it?&lt;/p&gt;
&lt;p&gt;Do you know if Raspberry Pi 4 supports it?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot connect to nRF when in direct advertisement mode</title><link>https://devzone.nordicsemi.com/thread/336685?ContentTypeID=1</link><pubDate>Fri, 29 Oct 2021 12:55:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:365abe94-4b44-45bb-bb5d-589549452a0b</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Are you certain that the PC you&amp;#39;re using support scanning and connecting to directed advertising devices? Quite a few devices don&amp;#39;t support this, for example all iOS devices as far as I know.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/applications/nrf_desktop/README.html"&gt;nRF Desktop application&lt;/a&gt; in the nRFConnect SDK should be able to do directed advertising, so you can take a look at that sample for reference.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>