<?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>Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/94531/stop-scanning-and-start-advertising</link><description>Hi, 
 I am developing application on NRF52840 with Zepyr 2.1.2 SDK, and I found two problems when using the example scan_adv, as follows: 
 
 Executing bt_le_scan_start in the main function can receive data smoothly, but using function scan_start() can</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 07 Dec 2022 05:33:24 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/94531/stop-scanning-and-start-advertising" /><item><title>RE: Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/thread/399317?ContentTypeID=1</link><pubDate>Wed, 07 Dec 2022 05:33:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d06347cf-1479-44eb-99eb-11cdea903761</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;This is what when I run&amp;nbsp;the code you posted now:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1670390973871v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;So, it&amp;nbsp;appears to be working as expected?&lt;/p&gt;
&lt;p&gt;Here is the hex file I built with your code if you want to try the same on your board:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/2248.zephyr.hex"&gt;devzone.nordicsemi.com/.../2248.zephyr.hex&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/thread/399106?ContentTypeID=1</link><pubDate>Tue, 06 Dec 2022 08:25:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc41081f-7a08-4b12-8a50-ac6f794867b8</guid><dc:creator>Kevin_Liu</dc:creator><description>&lt;p&gt;&lt;span id="mceQuote0" class="mceItem mceNonEditable mceQuote"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;I use J-Link RTT Viewer V7.58b as a debugging tool.&lt;/span&gt;&lt;/span&gt;&lt;span&gt; After the bt_le_scan_stop() function is executed, the scan_cb() function continues to work without error&lt;/span&gt;&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/94531/stop-scanning-and-start-advertising/399095"]Does scan_start() report any errors?[/quote]
&lt;p&gt;No error occurs, but scan_cb() function does not send any message&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/94531/stop-scanning-and-start-advertising/399095"]Can you post the updated code please? The snippet you posted will not build because the &amp;#39;err&amp;#39; variable being defined twice in main()[/quote]
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/types.h&amp;gt;
#include &amp;lt;stddef.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/sys/util.h&amp;gt;
#include &amp;lt;zephyr/toolchain.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/hci.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;devicetree.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;

struct bt_le_scan_param scan_param = {
		.type       = BT_HCI_LE_SCAN_PASSIVE,
		.options    = BT_LE_SCAN_OPT_NONE,
		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
		.window     = 0x0010,
	};
const struct device *dev;
int state = 1, adv_state;
int8_t rssi_data [8][8] = {0};
static uint8_t mfg_data[] = { 0xff, 0xff, 0x01 };
static const struct bt_data ad[] = {
	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3),
	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME,DEVICE_NAME_LEN)
};


void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
	   struct net_buf_simple *buf)
{
	printk(&amp;quot;scan_cb start\n&amp;quot;);
	char data_str[129];
	bin2hex(buf-&amp;gt;data, buf-&amp;gt;len, data_str, sizeof(data_str));
	printk(&amp;quot;data_str is : %s\n&amp;quot;, data_str);
}

void adv_start(uint8_t mfg){
	mfg_data[2] = mfg;
	int err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), NULL, 0);
	if (err) {
		printk(&amp;quot;Advertising failed to start (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
}

static void adv_stop(){
	int err = bt_le_adv_stop();
	if (err) {
		printk(&amp;quot;Advertising failed to stop (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
}
void scan_start(){
	int err = bt_le_scan_start(&amp;amp;scan_param, scan_cb);
		if (err) {
			printk(&amp;quot;Starting scanning failed (err %d)\n&amp;quot;, err);
			return;
		}
		k_sleep(K_MSEC(400));
}

static void scan_stop(){
	int err = bt_le_scan_stop();
	if (err) {
		printk(&amp;quot;Stop scanning failed (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
}


void main(void)
{
	int err = bt_enable(NULL);
	if (err) {
		printk(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
		return;
	}
	printk(&amp;quot;Bluetooth initialized\n&amp;quot;);
	scan_start();

	/*  if not use scan_start()
	*********************************************************
	err = bt_le_scan_start(&amp;amp;scan_param, scan_cb);
	if (err) {
		printk(&amp;quot;Starting scanning failed (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
	********************************************************
	*/

	k_sleep(K_MSEC(800));
	//scan_stop();
	err = bt_le_scan_stop();
	if (err) {
		printk(&amp;quot;Stop scanning failed (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
	while(1);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/thread/399095?ContentTypeID=1</link><pubDate>Tue, 06 Dec 2022 07:35:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e6904919-fd49-47cb-ae75-2ac7b0622501</guid><dc:creator>Vidar Berg</dc:creator><description>[quote user="Kevin_Liu"]I changed it to bt_le_scan_stop but still have the same problem,&amp;nbsp;is there any way to stop scanning?[/quote]
&lt;p&gt;Please explain how&amp;nbsp;you determine that it does not stop. Is it because&amp;nbsp;bt_le_scan_stop() returns with an error?&lt;/p&gt;
[quote user="Kevin_Liu"]In addition, using function scan_start() still cannot receive data.[/quote]
&lt;p&gt;Does scan_start() report any errors?&lt;/p&gt;
[quote user="Kevin_Liu"]my program is as follows:[/quote]
&lt;p&gt;Can you post the updated code please? The snippet you posted will not build because the &amp;#39;err&amp;#39; variable being defined twice in main()&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/thread/399073?ContentTypeID=1</link><pubDate>Tue, 06 Dec 2022 01:44:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11edde78-ac52-4220-9de0-939c7b946728</guid><dc:creator>Kevin_Liu</dc:creator><description>&lt;p&gt;&lt;span&gt;Hi,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you for your reply, I changed it to bt_le_scan_stop but still have the same problem,&amp;nbsp;is there any way to stop scanning?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In addition, using function scan_start() still cannot receive data.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;my program is as follows:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/types.h&amp;gt;
#include &amp;lt;stddef.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/sys/util.h&amp;gt;
#include &amp;lt;zephyr/toolchain.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/hci.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;devicetree.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;

struct bt_le_scan_param scan_param = {
		.type       = BT_HCI_LE_SCAN_PASSIVE,
		.options    = BT_LE_SCAN_OPT_NONE,
		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
		.window     = 0x0010,
	};
const struct device *dev;
int state = 1, adv_state;
int8_t rssi_data [8][8] = {0};
static uint8_t mfg_data[] = { 0xff, 0xff, 0x01 };
static const struct bt_data ad[] = {
	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3),
	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME,DEVICE_NAME_LEN)
};


void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
	   struct net_buf_simple *buf)
{...}

void adv_start(uint8_t mfg){
	mfg_data[2] = mfg;
	int err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), NULL, 0);
	if (err) {
		printk(&amp;quot;Advertising failed to start (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
}

static void adv_stop(){
	int err = bt_le_adv_stop();
	if (err) {
		printk(&amp;quot;Advertising failed to stop (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
}
void scan_start(){
	int err = bt_le_scan_start(&amp;amp;scan_param, scan_cb);
		if (err) {
			printk(&amp;quot;Starting scanning failed (err %d)\n&amp;quot;, err);
			return;
		}
		k_sleep(K_MSEC(400));
}

static void scan_stop(){
	int err = bt_le_scan_stop();
	if (err) {
		printk(&amp;quot;Stop scanning failed (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
}


void main(void)
{
	int err = bt_enable(NULL);
	if (err) {
		printk(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
		return;
	}
	printk(&amp;quot;Bluetooth initialized\n&amp;quot;);
	scan_start();

	/*  if not use scan_start()
	*********************************************************
	int err = bt_le_scan_start(&amp;amp;scan_param, scan_cb);
	if (err) {
		printk(&amp;quot;Starting scanning failed (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
	********************************************************
	*/

	k_sleep(K_MSEC(800));
	//scan_stop();
	int err = bt_le_scan_stop();
	if (err) {
		printk(&amp;quot;Stop scanning failed (err %d)\n&amp;quot;, err);
		return;
	}
	k_sleep(K_MSEC(400));
	while(1);
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/thread/399005?ContentTypeID=1</link><pubDate>Mon, 05 Dec 2022 14:20:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5afd82ef-1760-405e-ac75-942a48249483</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I see from your code snippet that you call&amp;nbsp;bt_le&lt;strong&gt;_adv_&lt;/strong&gt;stop() at the end.&amp;nbsp;Is that a typo? If you want to stop scanning, you will have to use&amp;nbsp;&lt;span&gt;bt_le&lt;strong&gt;_scan_&lt;/strong&gt;stop()/scan_stop().&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Vidar&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>