Stop scanning and start advertising

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't start, I tried to print out "Advertising ..." however it didn't print, seems like it stuck when the timeout.
/* main.c - Application main entry point */

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

#include <zephyr/types.h>
#include <stddef.h>
#include <sys/printk.h>
#include <sys/util.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>

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];
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("%x\n", addr->a.val);
	printk("%s\n",le_addr);
	printk("length:%d,data memory address:%x\n", buf->len,buf->data);
	memcpy(ext_adv_data,buf->data,buf->len);
	printk("ext adv data:");
	for(uint8_t i=0;i<buf->len;i++)
	{
		printk("%x",ext_adv_data[i]);
	}
	printk("\n");
	
}

void main(void)
{
	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,
	};
	 
	 
	int err;
	
	
	printk("Starting Scanner/Advertiser Demo\n");

	/* Initialize the Bluetooth Subsystem */
	err = bt_enable(NULL);
	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
		return;
	}

	printk("Bluetooth initialized\n");

	// err = bt_le_scan_start(&scan_param, scan_cb);
	// if (err) {
	// 	printk("Starting scanning failed (err %d)\n", err);
	// 	return;
	// }

	do {
		printk("Scanning...\n");
		err = bt_le_scan_start(&scan_param, scan_cb);
		if (err) {
			printk("Starting scanning failed (err %d)\n", err);
		return;
		}
		k_sleep(K_MSEC(400));
		/* Start advertising */
		err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad),
				      NULL, 0);
		if (err) {
			printk("Advertising failed to start (err %d)\n", err);
			return;
		}
		printk("Advertising...\n");
		k_sleep(K_MSEC(400));
		/*Stop aadvertising */
		err = bt_le_adv_stop();
		if (err) {
			printk("Advertising failed to stop (err %d)\n", err);
			return;
		}
		printk("Finish Advertising...\n");
	} while (1);
}
Parents
  • /* main.c - Application main entry point */
    
    /*
     * Copyright (c) 2015-2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr/types.h>
    #include <stddef.h>
    #include <sys/printk.h>
    #include <sys/util.h>
    
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/hci.h>
    
    
    
    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("%02x\n", addr->a.val);
    	printk("%s\n",le_addr);
    	printk("length:%d,data memory address:%02x\n", buf->len,buf->data);
    	memcpy(ext_adv_data,buf->data,buf->len);
    	printk("ext adv data:");
    	for(uint8_t i=0;i<buf->len;i++)
    	{
    		printk("%x",ext_adv_data[i]);
    	}
    	printk("\n");
    	
    }
    
    void stop_scan_handler()
    {
        printk("Scanning stop...\n");
        int err = bt_le_scan_stop();
        if (err) {
            printk("Stoping scanning failed (err %d)\n", 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(&stop_scan_work);
    }
    
    K_TIMER_DEFINE(k_timer_stop_scan, stop_scan_work_handler, NULL);
    
    void start_scan()
    {
        printk("Scanning start...\n");
        int err = bt_le_scan_start(&scan_param, scan_cb);
        if (err) {
            printk("Starting scanning failed (err %d)\n", err);
    	return;
        }
        k_timer_start(&k_timer_stop_scan, K_MSEC(2000), K_NO_WAIT); 
    }
    
    
    void stop_adv_handler()
    {
        int err = bt_le_adv_stop();
        if (err) {
            printk("Advertising failed to stop (err %d)\n", err);
            return;
        }
        printk("Finish Advertising...\n");
        start_scan();
    }
    
    K_WORK_DEFINE(stop_adv_work, stop_adv_handler);
    
    void stop_adv_work_handler(struct k_timer *dummy)
    {
        k_work_submit(&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("Advertising failed to start (err %d)\n", err);
    	return;
        }
        printk("Advertising...\n");
        k_timer_start(&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("Starting Scanner/Advertiser Demo\n");
    
    	/* Initialize the Bluetooth Subsystem */
    	err = bt_enable(NULL);
            if (IS_ENABLED(CONFIG_SETTINGS)) settings_load();
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	printk("Bluetooth initialized\n");
            start_adv();
            
            bt_conn_cb_register(&conn_callbacks);
    }

Reply
  • /* main.c - Application main entry point */
    
    /*
     * Copyright (c) 2015-2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr/types.h>
    #include <stddef.h>
    #include <sys/printk.h>
    #include <sys/util.h>
    
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/hci.h>
    
    
    
    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("%02x\n", addr->a.val);
    	printk("%s\n",le_addr);
    	printk("length:%d,data memory address:%02x\n", buf->len,buf->data);
    	memcpy(ext_adv_data,buf->data,buf->len);
    	printk("ext adv data:");
    	for(uint8_t i=0;i<buf->len;i++)
    	{
    		printk("%x",ext_adv_data[i]);
    	}
    	printk("\n");
    	
    }
    
    void stop_scan_handler()
    {
        printk("Scanning stop...\n");
        int err = bt_le_scan_stop();
        if (err) {
            printk("Stoping scanning failed (err %d)\n", 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(&stop_scan_work);
    }
    
    K_TIMER_DEFINE(k_timer_stop_scan, stop_scan_work_handler, NULL);
    
    void start_scan()
    {
        printk("Scanning start...\n");
        int err = bt_le_scan_start(&scan_param, scan_cb);
        if (err) {
            printk("Starting scanning failed (err %d)\n", err);
    	return;
        }
        k_timer_start(&k_timer_stop_scan, K_MSEC(2000), K_NO_WAIT); 
    }
    
    
    void stop_adv_handler()
    {
        int err = bt_le_adv_stop();
        if (err) {
            printk("Advertising failed to stop (err %d)\n", err);
            return;
        }
        printk("Finish Advertising...\n");
        start_scan();
    }
    
    K_WORK_DEFINE(stop_adv_work, stop_adv_handler);
    
    void stop_adv_work_handler(struct k_timer *dummy)
    {
        k_work_submit(&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("Advertising failed to start (err %d)\n", err);
    	return;
        }
        printk("Advertising...\n");
        k_timer_start(&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("Starting Scanner/Advertiser Demo\n");
    
    	/* Initialize the Bluetooth Subsystem */
    	err = bt_enable(NULL);
            if (IS_ENABLED(CONFIG_SETTINGS)) settings_load();
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	printk("Bluetooth initialized\n");
            start_adv();
            
            bt_conn_cb_register(&conn_callbacks);
    }

Children
No Data
Related