Nrf52832 主机短名过滤设置

实际工作中遇到主机扫描从机名称,但实际过程中可能存在产品版本升级的情况,比如Nordic_UART 升级之后变成 Nordic_UART2,那么全名过滤就无法适应这种变化。

于是搜索到可以设置短名过滤,也就是只匹配前几个设定字符,完美实现需求。

先上原始链接: nordic/nrf52 SDK主机设置short name filter(简称过虑)_叔子衿的博客-CSDN博客

方法为:

1.确保正确设置了从机广播设置(advertising_init)中的设备名称类型(name_type)以及长度

2.确保正确设置了主机扫描策略,根据示例中的(name filter)改即可

3.确保nrf_ble_scan_filter_set函数传递参数中p_data结构体变量类型是nrf_ble_scan_short_name_t,即使用nrf_ble_scan_short_name_t结构体变量作为传递给p_data的变量类型,并正确初始化

4.关键:在给.p_short_name赋值时,比从机short name多一个字符,

实际测试如下:

从机端修改:

/**@brief Function for initializing the Advertising functionality.
*/
static void advertising_init(void)
{
uint32_t err_code;
ble_advertising_init_t init;
int8_t tx_power_level = TX_POWER_LEVEL;//设置发射功率
memset(&init, 0, sizeof(init));

init.advdata.name_type = BLE_ADVDATA_SHORT_NAME;
init.advdata.short_name_len = 7; //“Nordic_?”
init.advdata.include_appearance = false;
init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
init.advdata.p_tx_power_level = &tx_power_level;

init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
init.srdata.uuids_complete.p_uuids = m_adv_uuids;

init.config.ble_adv_fast_enabled = true;
init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
init.evt_handler = on_adv_evt;

err_code = ble_advertising_init(&m_advertising, &init);
APP_ERROR_CHECK(err_code);

ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}

主机端修改:

//私有服务的UUID过滤
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
#define NUS_BASE_UUID {{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E}} /**< Used vendor specific UUID. */

#define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */

//写入需要过滤设备的名称
static nrf_ble_scan_short_name_t Filter_ShortName=
{
  .p_short_name = "Nordic_U",
  .short_name_min_len = 7
};
/**@brief Function for initializing the scanning and setting the filters.
*/
static void scan_init(void)
{
ret_code_t err_code;
nrf_ble_scan_init_t init_scan;

memset(&init_scan, 0, sizeof(init_scan));

init_scan.connect_if_match = true;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;

err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);

//写入自定义UUID基数
uint8_t uuid_type;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &uuid_type);
APP_ERROR_CHECK(err_code);

//扫描器的滤波器设置
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
APP_ERROR_CHECK(err_code);

err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_SHORT_NAME_FILTER, &Filter_ShortName);
APP_ERROR_CHECK(err_code);

err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER|NRF_BLE_SCAN_SHORT_NAME_FILTER, false);
APP_ERROR_CHECK(err_code);
}

posted @   勇敢蘑菇  阅读(607)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示