Setting Non-Connectable and Scannable Extended Advertising in NCS v2.5.0 (nRF52840)

Hi,

When I use NCS V2.5.0 (board:nRF 52840),I want to set the Non-Connectable and Scannable Extended Advertising.My code as follows.I use the APP nRF Connect for moblie,but it can‘t scan the adv.I also use the Ellisys BTR (Picture shows 'Test Packet') ,How can I set the Non-Connectable and Scannable Extended Advertising? 

/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/settings/settings.h>
#include <dk_buttons_and_leds.h>
#include <zephyr/bluetooth/addr.h>

#include <mpsl_radio_notification.h>
#include <mpsl.h>

#include <bluetooth/gatt_dm.h>
#include <zephyr/bluetooth/gap.h>
#include <zephyr/bluetooth/hci.h>


#define MY_APPEARANCE 0x04

#define ADV_20_IDX 0

#define RUN_STATUS_LED          DK_LED1
#define CON_STATUS_LED          DK_LED2
#define RUN_LED_BLINK_INTERVAL  1000

uint64_t last_latancy_scan=0;

#define BLE_ADV_TIMEOUT                 (10000)  // N * 10ms for advertiser timeout
#define BLE_ADV_EVENTS                  (500)


#define Tw_tick  48
#define Ts_tick  480    
#define Ta_tick  32   

#define Tw 37.5
#define MAX_COUNT 60
static uint32_t timer_count = 0;

uint64_t exp_start_time=0;
uint16_t external_expcount=0;
uint16_t last_exp=0;

char device_addr[18] = {           
    "FF:FF:FF:FF:A1:A1"   
};


static struct bt_le_ext_adv *ext_adv[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
 
static const struct bt_le_adv_param *adv_20_param =
	BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY|BT_LE_ADV_OPT_EXT_ADV|BT_LE_ADV_OPT_SCANNABLE,         //BT_LE_ADV_OPT_EXT_ADV|BT_LE_ADV_OPT_CODED
			Ta_tick, /* 20 ms */
			Ta_tick, /* 20ms */
			NULL);


static struct bt_data scan_rsp_data[] = {
		BT_DATA(BT_DATA_NAME_COMPLETE, "SCAN_RSP", sizeof("SCAN_RSP") - 1),
		};


//BT_LE_ADV_OPT_NONE
//BT_LE_ADV_OPT_SCANNABLE
//BT_LE_ADV_OPT_CONNECTABLE


uint64_t cnt=0;
uint64_t last_cnt=0;

uint8_t adv1_data[]=
{
	0x01,         //一个字节存储广播ID                                        15
	0x00,0x00,    //两个字节用来存储实验轮数                                   16 17
	0x00,0x00,0x00,0x00,//八个字节存储该轮实验开始的公共时间                    18-25
	0x00,0x00,0x00,0x00,
};




static  struct bt_data connectable_20_data[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
	BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE,0x00,MY_APPEARANCE),   //NCS的appearance为两个字节
	BT_DATA(BT_DATA_NAME_COMPLETE, "ADV1", sizeof("ADV1") - 1), //广播名字写到蓝牙包里
	BT_DATA(BT_DATA_MANUFACTURER_DATA, adv1_data, ARRAY_SIZE(adv1_data)),
};



static void print_adv_data(const struct bt_data *ad, size_t ad_len)
{
    for (size_t i = 0; i < ad_len; i++) {
        printk("AD[%zu]: type=0x%02x, len=%u, data=", i, ad[i].type, ad[i].data_len);
        for (uint8_t j = 0; j < ad[i].data_len; j++) {
            printk("%02x ", ad[i].data[j]);
        }
        printk("\n");
    }
}



//4.11  修改广告创建过程,为了实现增发广告


// 修改广告创建函数
static int advertising_set_create(struct bt_le_ext_adv **adv,
    const struct bt_le_adv_param *param,
    const struct bt_data *ad, 
    size_t ad_len,
    const struct bt_data *sd,
    size_t sd_len,
    struct bt_le_ext_adv_cb *cb)
{
    int err;
    err = bt_le_ext_adv_create(param, cb, adv);
    if (err) {
        return err;
    }

    // 设置广播数据和扫描响应数据
    err = bt_le_ext_adv_set_data(*adv, ad, ad_len, sd, sd_len);
    if (err) {
        bt_le_ext_adv_delete(*adv);
        *adv = NULL;
    }
    return err;
}

static int adv_1_create(void)
{
int err;

err = advertising_set_create(&ext_adv[ADV_20_IDX], 
	adv_20_param,
	connectable_20_data, 
	ARRAY_SIZE(connectable_20_data),
	scan_rsp_data,  // 新增扫描响应数据
	ARRAY_SIZE(scan_rsp_data),
	NULL); // 均不带带回调
	return err;
}



static void adv_init(void)
{
	int err;
	err = adv_1_create();
}

// 修改随机地址设置函数
static void random_addr_set()
{
    int err;
    bt_addr_le_t addr;
    
    // 使用静态随机地址
    err = bt_addr_le_from_str(device_addr, "random", &addr);
    if (err) {
        printk("Failed to parse address (err %d)\n", err);
        return;
    }
    
    // 确保地址类型是静态随机
    addr.type = BT_ADDR_LE_RANDOM;
    
    err = bt_id_create(&addr, NULL);
    if (err) {
        printk("Failed to set identity address (err %d)\n", err);
    }
}

void radio_handler(const void *context)
{
	cnt++;
	int time_stamp = k_uptime_get(); 
	printk("t:%d\n",time_stamp);
}


int radio_notification_init(){
	int err;
	err = mpsl_radio_notification_cfg_set(MPSL_RADIO_NOTIFICATION_TYPE_INT_ON_ACTIVE,MPSL_RADIO_NOTIFICATION_DISTANCE_800US,SWI1_IRQn);
    if (err) {
       printk("mpsl_radio_notification_cfg_set failed (err %d)\n", err);
       return 0;
    }

    IRQ_CONNECT(SWI1_IRQn, 2,
    radio_handler, NULL, 0);
  
    irq_enable(SWI1_IRQn);
	return err;
}


int main(void)
{
	int err;
	int blink_status = 0;

	printk("Starting Bluetooth multiple advertising sets example\n");

	err = dk_leds_init();
	if (err) {
		printk("LEDs init failed (err %d)\n", err);
		return 0;
	}


	random_addr_set();//设置MAC地址要放在协议栈之前

	

	err = bt_enable(NULL);
	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
		return 0;
	}

	printk("Bluetooth initialized\n");

	//radio_notification_init();

	adv_init();   

	err=bt_le_ext_adv_start(ext_adv[ADV_20_IDX], BT_LE_EXT_ADV_START_DEFAULT);  //开启第一路

	printk("adv started\n");


	for (;;) {
		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
		k_sleep(K_MSEC(1000));

	}
}

Related