<?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/91929/stop-scanning-and-start-advertising</link><description>I am working on nrf52840 with nrf connect sdk zephyr example scan_adv, I want to modify it for example scanning for 2 second and advertising for 5 second, I tried to use .timeout in 
 
 bt_le_scan_param and the scanning stop, but the advertising didn</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 19 Sep 2022 13:55:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/91929/stop-scanning-and-start-advertising" /><item><title>RE: Stop scanning and start advertising</title><link>https://devzone.nordicsemi.com/thread/386925?ContentTypeID=1</link><pubDate>Mon, 19 Sep 2022 13:55:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7866172-ef10-4680-b58b-ee473deb7430</guid><dc:creator>qwertynoon</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/* main.c - Application main entry point */

/*
 * Copyright (c) 2015-2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include &amp;lt;zephyr/types.h&amp;gt;
#include &amp;lt;stddef.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;sys/util.h&amp;gt;

#include &amp;lt;bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;bluetooth/hci.h&amp;gt;



static uint8_t mfg_data[] = { 0xff, 0xff, 0x00 };

static const struct bt_data ad[] = {
	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3),
};

uint8_t ext_adv_data[64];

void start_adv();

	struct bt_le_scan_param scan_param = {
		.type       = BT_HCI_LE_SCAN_PASSIVE,
		.options    = BT_LE_SCAN_OPT_NONE,
		.interval   = 0x0100,
		.window     = 0x080,
		.timeout	=2000,
	};


static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
		    struct net_buf_simple *buf)
{
	char le_addr[BT_ADDR_LE_STR_LEN];
	bt_addr_le_to_str(addr,le_addr,sizeof(le_addr));
	printk(&amp;quot;%02x\n&amp;quot;, addr-&amp;gt;a.val);
	printk(&amp;quot;%s\n&amp;quot;,le_addr);
	printk(&amp;quot;length:%d,data memory address:%02x\n&amp;quot;, buf-&amp;gt;len,buf-&amp;gt;data);
	memcpy(ext_adv_data,buf-&amp;gt;data,buf-&amp;gt;len);
	printk(&amp;quot;ext adv data:&amp;quot;);
	for(uint8_t i=0;i&amp;lt;buf-&amp;gt;len;i++)
	{
		printk(&amp;quot;%x&amp;quot;,ext_adv_data[i]);
	}
	printk(&amp;quot;\n&amp;quot;);
	
}

void stop_scan_handler()
{
    printk(&amp;quot;Scanning stop...\n&amp;quot;);
    int err = bt_le_scan_stop();
    if (err) {
        printk(&amp;quot;Stoping scanning failed (err %d)\n&amp;quot;, err);
	return;
    }
    start_adv();
}

K_WORK_DEFINE(stop_scan_work, stop_scan_handler);

void stop_scan_work_handler(struct k_timer *dummy)
{
    k_work_submit(&amp;amp;stop_scan_work);
}

K_TIMER_DEFINE(k_timer_stop_scan, stop_scan_work_handler, NULL);

void start_scan()
{
    printk(&amp;quot;Scanning start...\n&amp;quot;);
    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_timer_start(&amp;amp;k_timer_stop_scan, K_MSEC(2000), K_NO_WAIT); 
}


void stop_adv_handler()
{
    int err = bt_le_adv_stop();
    if (err) {
        printk(&amp;quot;Advertising failed to stop (err %d)\n&amp;quot;, err);
        return;
    }
    printk(&amp;quot;Finish Advertising...\n&amp;quot;);
    start_scan();
}

K_WORK_DEFINE(stop_adv_work, stop_adv_handler);

void stop_adv_work_handler(struct k_timer *dummy)
{
    k_work_submit(&amp;amp;stop_adv_work);
}

K_TIMER_DEFINE(k_timer_stop_adv, stop_adv_work_handler, NULL);

void start_adv()
{
    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;
    }
    printk(&amp;quot;Advertising...\n&amp;quot;);
    k_timer_start(&amp;amp;k_timer_stop_adv, K_MSEC(5000), K_NO_WAIT); 
}

void connected(){};
void disconnected(){};
static struct bt_conn_cb conn_callbacks = {
	.connected = connected,
	.disconnected = disconnected,
};

void main(void)
{
	 
	 
	int err;
	
	
	printk(&amp;quot;Starting Scanner/Advertiser Demo\n&amp;quot;);

	/* Initialize the Bluetooth Subsystem */
	err = bt_enable(NULL);
        if (IS_ENABLED(CONFIG_SETTINGS)) settings_load();
	if (err) {
		printk(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
		return;
	}

	printk(&amp;quot;Bluetooth initialized\n&amp;quot;);
        start_adv();
        
        bt_conn_cb_register(&amp;amp;conn_callbacks);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>