修改nordic系列芯片广播名

一、local name 和device name的区别

关于local name 和device name,很多人可能有疑惑,为什么蓝牙有两个名字。可以这样简单地区分:
1.Local Name是广播出来的。Device Name是GATT service中的一个特性,需要连接后才能读或写。。
2.Local Name不能过长,因为广播包数据长度有限。Local Name有两类short和long。具体长度可以自己设置。Device Name的最长为248byte。Local Name最长能到达29bytes。

3.Local Name和Device Name要求保持一致性。Local Name必须是Device Name的开始连续的一部分或全部。例如Device Name是"BT_DEVICE",则Local Name可以是"BT_D"或 “BT_DEVICE”。

二、举例

1.如何用代码实现的

我们看看nordic sdk15.0的代码是如何去设置这两个名字的。

整个工程我们只看到“DEVICE_NAME”的宏定义。只找到设置Device Name,找不到设置local name的代码,其实是隐藏起来了。我们找到下面这个函数ble_advdata_encode,最后一段

这里是编码,把所有的adv data组合起来,用于广播的。我们再看深一层,看看name_encode这个函数。在这个函数里找到获取宏定义DEVICE_NAME里函数。

 // Get GAP device name and lengtherr_code = sd_ble_gap_device_name_get(&p_encoded_data[(*p_offset) +AD_DATA_OFFSET],  &actual_length);

2.验证

既然我们找到了,我们一起来修改这两个变量试试:
把DEVICE_NAME改成这样

#define DEVICE_NAME                         "Nordic_HRM__Nordic_HRM__Nordic_HRM__Nordic_HRM__Nordic_HRM__Nordic_HRM__Nordic_HRM__"                            /**< Name of device. Will be included in the advertising data. */

然后运行一下,发现出错了。(晕)
我们再看看是哪里出现的问题,device name的长度最长是248,local name的长度看广播了多少东西,但是这个name_encode这个函数帮我们截断,不用我们担心。
看看出错的地方:
[外链图片转存失败(img-0ETZ0al8-1564398878266)(en-resource://database/1644:0)]
就是下面这个函数出现的问题。这个函数是API,我们看不到里面的代码。

    err_code = sd_ble_gap_device_name_set(&sec_mode,(const uint8_t *)DEVICE_NAME,strlen(DEVICE_NAME));

看看注释

/**@brief Set GAP device name.** @note  If the device name is located in application flash memory (see @ref ble_gap_cfg_device_name_t),*        it cannot be changed. Then @ref NRF_ERROR_FORBIDDEN will be returned.** @param[in] p_write_perm Write permissions for the Device Name characteristic, see @ref ble_gap_conn_sec_mode_t.* @param[in] p_dev_name Pointer to a UTF-8 encoded, <b>non NULL-terminated</b> string.* @param[in] len Length of the UTF-8, <b>non NULL-terminated</b> string pointed to by p_dev_name in octets (must be smaller or equal than @ref BLE_GAP_DEVNAME_MAX_LEN).** @retval ::NRF_SUCCESS GAP device name and permissions set successfully.* @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.* @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.* @retval ::NRF_ERROR_DATA_SIZE Invalid data size(s) supplied.* @retval ::NRF_ERROR_FORBIDDEN Device name is not writable.*/
SVCALL(SD_BLE_GAP_DEVICE_NAME_SET, uint32_t, sd_ble_gap_device_name_set(ble_gap_conn_sec_mode_t const *p_write_perm, uint8_t const *p_dev_name, uint16_t len));

还是看不到什么东西,再看看这个类型 ble_gap_cfg_device_name_t

/*** @brief Device name and its properties, set with @ref sd_ble_cfg_set.** @note  If the device name is not configured, the default device name will be*        @ref BLE_GAP_DEVNAME_DEFAULT, the maximum device name length will be*        @ref BLE_GAP_DEVNAME_DEFAULT_LEN, vloc will be set to @ref BLE_GATTS_VLOC_STACK and the device name*        will have no write access.** @note  If @ref max_len is more than @ref BLE_GAP_DEVNAME_DEFAULT_LEN and vloc is set to @ref BLE_GATTS_VLOC_STACK,*        the attribute table size must be increased to have room for the longer device name (see*        @ref sd_ble_cfg_set and @ref ble_gatts_cfg_attr_tab_size_t).** @note  If vloc is @ref BLE_GATTS_VLOC_STACK :*        - p_value must point to non-volatile memory (flash) or be NULL.*        - If p_value is NULL, the device name will initially be empty.** @note  If vloc is @ref BLE_GATTS_VLOC_USER :*        - p_value cannot be NULL.*        - If the device name is writable, p_value must point to volatile memory (RAM).** @retval ::NRF_ERROR_INVALID_PARAM  One or more of the following is true:*                                    - Invalid device name location (vloc).*                                    - Invalid device name security mode.* @retval ::NRF_ERROR_INVALID_LENGTH One or more of the following is true:*                                    - The device name length is invalid (must be between 0 and @ref BLE_GAP_DEVNAME_MAX_LEN).*                                    - The device name length is too long for the given Attribute Table.* @retval ::NRF_ERROR_NOT_SUPPORTED  Device name security mode is not supported.*/
typedef struct
{ble_gap_conn_sec_mode_t  write_perm;   /**< Write permissions. */uint8_t                  vloc:2;       /**< Value location, see @ref BLE_GATTS_VLOCS.*/uint8_t                 *p_value;      /**< Pointer to where the value (device name) is stored or will be stored. */uint16_t                 current_len;  /**< Current length in bytes of the memory pointed to by p_value.*/uint16_t                 max_len;      /**< Maximum length in bytes of the memory pointed to by p_value.*/
} ble_gap_cfg_device_name_t;

好了,找到了BLE_GAP_DEVNAME_DEFAULT_LEN只有31,原来是我的设备名写得太长了。
怎么改呢,直接修改了BLE_GAP_DEVNAME_DEFAULT_LEN好像没用,因为这个函数没被调用,大家不要觉得协议栈能用到这个变量,协议栈的调用方式我之前介绍过,都是通过svc和swi的,就是中断服务函数,协议栈的变量不可能被应用层调用,反过来也是不可能的。

那怎么解决这个问题呢:
一、改短设备名的长度,小于31就没问题了。
二、上面的问题是因为分配的栈不足,那么怎么增大栈呢。
仔细看看nrf_sdh_ble_default_cfg_set这个函数,里面有很多分配stack的代码,我们依样画葫芦就好。在nrf_sdh_ble_default_cfg_set函数的最后增加下面的代码就可以了。

// BLE_GAP_CFG_DEVICE_NAME.memset(&ble_cfg, 0x00, sizeof(ble_cfg));ble_cfg.gap_cfg.device_name_cfg.max_len = 248;ble_cfg.gap_cfg.device_name_cfg.vloc = BLE_GATTS_VLOC_STACK;ret_code = sd_ble_cfg_set(BLE_GAP_CFG_DEVICE_NAME, &ble_cfg, *p_ram_start);if (ret_code != NRF_SUCCESS){NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GAP_CFG_DEVICE_NAME.",nrf_strerror_get(ret_code));}

欢迎关注个人公众号“低功耗蓝牙技术研究及推广”

Nordic系列芯片讲解十(修改nordic系列芯片的广播名)相关推荐

  1. Nordic系列芯片讲解五( Nordic sdk中nrf_drv_twi的使用)

    最近做手表项目用到TWI总线,一个用来驱动oled,一个用来驱动三轴加速度传感器.因为两个模块并的驱动时序不一样,所以分开两个twi总线来驱动它们.这里用到了sdk里面的nrf_drv_twi.c. ...

  2. Nordic系列芯片讲解八( Nordic SDK常见特殊指令汇集)

    文章目录 一.Compiler-specific Keywords and Operators 2.__inline 3.__align 5.__packed 6.__svc 7.__weak 二.F ...

  3. 灵汐科技携类脑芯片KA200及类脑系列产品亮相国家“十三五”科技创新成就展

    金秋十月,碧空如洗,凉爽舒适. 2021年10月21日至27日,以"创新驱动发展 迈向科技强国"为主题的国家"十三五"科技成就展在北京展览馆盛大开幕. 本次展规 ...

  4. [系统安全] 四十五.APT系列(10)Metasploit后渗透技术信息收集、权限提权和功能模块详解

    您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列.因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全.逆向分 ...

  5. [系统安全] 四十四.APT系列(9)Metasploit技术之基础用法万字详解及防御机理

    您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列.因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全.逆向分 ...

  6. eureka集群只注册一个_Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  7. Spring Boot干货系列:(十二)Spring Boot使用单元测试 | 嘟嘟独立博客

    原文地址 2017-12-28 开启阅读模式 Spring Boot干货系列:(十二)Spring Boot使用单元测试 Spring Boot干货系列 Spring Boot 前言 这次来介绍下Sp ...

  8. axi dma 寄存器配置_FPGA Xilinx Zynq 系列(三十二)AXI 接口

    大侠好,欢迎来到FPGA技术江湖,江湖偌大,相见即是缘分.大侠可以关注FPGA技术江湖,在"闯荡江湖"."行侠仗义"栏里获取其他感兴趣的资源,或者一起煮酒言欢. ...

  9. python建站部署_SpringBoot入门建站全系列(三十二)接入xxl-job分布式任务调度平台...

    SpringBoot入门建站全系列(三十二)接入xxl-job分布式任务调度平台 一.概述 XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源 ...

最新文章

  1. NLP文本标注工具与平台(数据标注公司)
  2. 用拉链法实现哈希算法的运算
  3. 【Python】Autoviz: 一行代码搞定数据集探索并可视化
  4. php mysql 安装错误_Apache+php配置 Mysql安装出错解决办法
  5. spark mllib源码分析之随机森林(Random Forest)
  6. 车间生产能耗管控方案_SAREN三仁净化工程:锂电池生产车间的设计规范及方案...
  7. gcc/g++ 链接库的编译与链接
  8. 多线程条件变量(pthread_cond_wait)用法
  9. Linux中.rpm,Linux中rpm的使用
  10. 数据分析不能挣钱、不能给公司创造利润,那要你有什么用?
  11. 让美团、京东、搜狐都说好的数据仓库,牛在哪?
  12. CSS z-index 属性的使用方法和层级树的概念
  13. Linux下ping命令、traceroute命令、tracert命令的使用
  14. 利用卷积自编码器对图片进行降噪
  15. vue 头像修改-裁剪图片 vue-cropper
  16. 如何将html页面打印出来,网页太长如何全部打印_怎样打印整个网页内容-win7之家...
  17. 2021年京东/淘宝/天猫/双十一红包最新优惠攻略,1111超级红包如何抢?
  18. 重读“发展Linux,中日两国之比较”有感
  19. 计算机基础知识还有那些,关于电脑基础知识有哪些
  20. 【机器学习】训练集、验证集与测试集

热门文章

  1. 产业链图谱:2021年中国新能源汽车产业链图谱|产业链全景图
  2. 美发沙龙_美发机器人? 保持你的头发!
  3. 在微型计算机系统组成中 把微处理器cpu,计算机基础知识及答案(二)
  4. OpenCV常用图像拼接方法(四):基于Stitcher类拼接
  5. ios和android手机测试,IOS和Android进行手机测试有哪些区别?
  6. 两种方法获取文件OEP
  7. AP统计和微积分怎么按计算器?
  8. 2021 谁怕?一蓑烟雨任平生
  9. jetson nano 安装 迈德威视 工业相机 mindvision SDK
  10. 真正重要的10个联盟营销指标