nRF52832 GATT 自定义Service/Characteristic

ble_app_blinky例程中,直接调用了sdk的ble_lbs_init函数来初始化service,所以为了增加我们自己的service,从ble_lbs_init来看


uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
{uint32_t   err_code;ble_uuid_t ble_uuid;// Initialize service structure.p_lbs->led_write_handler = p_lbs_init->led_write_handler;// Add service.ble_uuid128_t base_uuid = {LBS_UUID_BASE};err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);VERIFY_SUCCESS(err_code);ble_uuid.type = p_lbs->uuid_type;ble_uuid.uuid = LBS_UUID_SERVICE;err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);VERIFY_SUCCESS(err_code);// Add characteristics.err_code = button_char_add(p_lbs, p_lbs_init);VERIFY_SUCCESS(err_code);err_code = led_char_add(p_lbs, p_lbs_init);VERIFY_SUCCESS(err_code);return NRF_SUCCESS;
}

初始化的整个过程和蓝牙BLE的GATT profiles有关。

Service->增加Characteristic->增加Descriptor

下图为官方提供的流程

(S132 v6.0.0 API Reference - Message Sequence Charts - GATTS ATT Table Population部分)

自定义Service实现

根据上述及uuid相关,自定义一个service如下:
需要一个基本uuid,这里测试,使用0000XXXX-1234-5678-9abcdef012345678

#define USER_UUID_BASE {0x78, 0x56, 0x34, 0x12, 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00}
//定义基本UUID
ble_uuid128_t base_uuid = {USER_UUID_BASE};
err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type);
APP_ERROR_CHECK(err_code);

下一步要增加Service,在增加Service前,为了方便使用定义一个结构体:

struct user_ble_gatt_s
{uint16_t                    service_handle;      // Handle of LED Button Service (as provided by the BLE stack). ble_gatts_char_handles_t    char1_handles;    // Handles related to the Characteristic 1. ble_gatts_char_handles_t    char2_handles; // Handles related to the  Characteristic 2. 此处只增加了一个characteristic,所以此处暂时不使用uint8_t                     uuid_type;           //< UUID type for the Service.
};

这个结构体是为了记录service/characteristic handles,方便其他位置调用。(当然也可以不使用结构体,直接用变量储存。)

结构体定义变量:

struct user_ble_gatt_s user_uuid;

增加Service

  //增加Serviceble_uuid.type=user_uuid.uuid_type;ble_uuid.uuid = USER_UUID_SERVICE;err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &user_uuid.service_handle );APP_ERROR_CHECK(err_code);

增加Service后,就可以增加characteristic,为了实现蓝牙的双方通信,暂时只为这个characteristic实现notify、read、write三个权限。

配置cccd:

ble_gatts_attr_md_t cccd_md;//配置cccdmemset(&cccd_md, 0, sizeof(cccd_md));BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);cccd_md.vloc = BLE_GATTS_VLOC_STACK;

配置characteristic权限


ble_gatts_char_md_t char_md;memset(&char_md, 0, sizeof(char_md));char_md.char_props.read=1;  //允许读char_md.char_props.write = 1;//允许写char_md.char_props.notify=1;    //notifychar_md.p_char_user_desc  = NULL;char_md.p_char_pf         = NULL;char_md.p_user_desc_md    = NULL;char_md.p_cccd_md         = &cccd_md;//实际测试:在notify=1;时,此处写NULL notify也能实现char_md.p_sccd_md         = NULL;

配置其他属性

ble_gatts_attr_md_t attr_md;
ble_gatts_attr_t    attr_char_value;memset(&attr_md, 0, sizeof(attr_md));BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);attr_md.vloc    = BLE_GATTS_VLOC_STACK;memset(&attr_char_value, 0, sizeof(attr_char_value));attr_char_value.p_uuid    = &ble_uuid;    //此characteristic的uuidattr_char_value.p_attr_md = &attr_md;attr_char_value.init_len  = sizeof(uint8_t);   //初始值长度attr_char_value.init_offs = 0;attr_char_value.max_len   = sizeof(uint8_t)*2;  //value最大长度attr_char_value.p_value   = NULL;  //初始值

增加characteristic

最后调用增加characteristic函数:sd_ble_gatts_characteristic_add(user_uuid.service_handle,&char_md,&attr_char_value,&user_uuid.char1_handles);至此,characteristic已经增加完成,但是仅能实现read和notify功能,write功能写入,mcu却无法处理,所以还需要做一些配置

characteristic写功能实现

sdk中有这么一个宏:

#define NRF_SDH_BLE_OBSERVER (   _name,_prio,_handler,_context
)
Macro for registering nrf_sdh_soc_evt_observer_t. Modules that want to be notified about SoC events must register the handler using this macro.
This macro places the observer in a section named "sdh_soc_observers".Parameters
[in]    _name       Observer name.
[in]    _prio       Priority of the observer event handler. The smaller the number, the higher the priority.
[in]    _handler    BLE event handler.
[in]    _context    Parameter to the event handler.

描述中:Modules that want to be notified about SoC events must register the handler using this macro.

所以在user_uuid定义处增加此宏:

struct user_ble_gatt_s user_uuid;
NRF_SDH_BLE_OBSERVER(user_uuid_obs,0,ble_user_uuid_on_ble_evt, &user_uuid);

其中,_name:user_uuid_obs只是name,随意定义,
_context:&user_uuid为回调函数的参数,如果没有必要可以设置为NULL ,
关键是_prio:0,_handler:ble_user_uuid_on_ble_evt两个参数

重症纠结患者,回调函数:

void ble_user_uuid_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;switch (p_ble_evt->header.evt_id){case BLE_GATTS_EVT_WRITE:for(int i=0;i<p_evt_write->len;i++)  //这里仅仅是直接将ble发送来的数据打印出来不做其他处理NRF_LOG_INFO("%x",p_evt_write->data[i]);break;default:    // No implementation needed.break;}
}

简单的几句,但是如果没有sdk的例程,要找到数据储存在p_evt_write->data数组里,可能要花上一段时间了…

注意,写入的数据的最大长度取决与 attr_char_value.max_len = sizeof(uint8_t)*2;【value最大长度,如果超出限制,那么写入会无效】。如果写入的数据长度小于现有的值的数据长度,那么并不会覆盖所有的值,如本身为字符"123",现在写入"a",那么值并不是"a",而是"a23",需要注意。

至此characteristic的write功能简单的实现。
自定义Service/Charactereistic功能已经简单完成,这里并没有很深入的研究。
如果需要继续增加其他权限或内容,可以根据蓝牙的说明修改对应的权限配置即可。

nRF52832 ble_app_blinky 例程

nRF52832 GATT 自定义Service/Characteristic相关推荐

  1. 关于ubuntu自定义service服务时找不到/usr/lib/systemd/system目录的问题

    关于ubuntu自定义service服务时找不到/usr/lib/systemd/system目录的问题 问题 我们知道在 systemd 取代了 init 而成为广大 Linux 系统中 PID 为 ...

  2. ros自定义service消息.srv文件中增加自定义.msg消息

    先制作msg文件 1. 在disinfect_msg包下创建 :testInfo.msg 文件 int32 Id string TargetName string X string Y string ...

  3. 在linux下创建自定义service服务

    三个部分 这个脚本分为3个部分:[Unit] [Service] [Install]. Unit Unit表明该服务的描述,类型描述.我们称之为一个单元.比较典型的情况是单元A要求在单元B启动之后再启 ...

  4. Openstack组件部署 — 将一个自定义 Service 添加到 Keystone

    目录 目录 Keystone 认证流程 让 Keystone 为一个新的项目 Service 提供验证功能 最后 Keystone 认证流程 User 使用凭证(username/password) ...

  5. ros(7)自定义service数据

    创建Persom.srv 新建srv文件夹 在srv文件夹中创建Persom.srv,编辑文件 string name uint8 age uint8 sexuint8 unknown = 0 uin ...

  6. systemctl自定义service

    应用场景:开启自启动运行脚本/usr/local/manage.sh 一.服务介绍 CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户 ...

  7. Linux 自定义service,并重定向输出到日志文件

    最近由于项目需要,需后台运行java application,进行一些操作,并将屏幕输出定向到日志文件中. 首先将jiar 文件拷贝到linux.这里我放在 /home/jerry 输入命令 cd / ...

  8. nrf52832 学习笔记(七)蓝牙协议层级理解

    nrf52832 学习笔记(七)蓝牙协议层级理解 本文主要由一下几篇文档摘录汇总而成 ,如有错误欢迎斧正 da14531 蓝牙协议文档 深入浅出低功耗蓝牙(BLE)协议栈 低功耗蓝牙ATT/GATT/ ...

  9. 从零开始的nrf52832蓝牙开发(1)--蓝牙协议基础

    想要进行蓝牙开发,第一步肯定要对蓝牙协议有所了解.除了要对蓝牙的一些专业术语有所熟悉,还应该对蓝牙协议每层功能有一定认知. 概略图: 物理层(PHY): 物理层规定了蓝牙频段:2400MHz~2483 ...

  10. 【Nordic】如何极致实现Nordic 蓝牙性能

    在很多应用场合,BLE只是作为一个数据透传模块,即将设备端数据上传给手机,同时接收手机端下发的数据.本文将和大家一起,一步一步演示如何开发一个BLE透传应用程序.按照本文的说明,大家可以很快就实现一个 ...

最新文章

  1. 由一个异常开始思考springmvc参数解析
  2. 3、以太网基础知识——ARP地址解析协议原理
  3. Java 集合 List Arrays.asList
  4. wifiwan口速率什么意思_无线路由器怎么设置wan口速率
  5. 李洪强经典面试题37
  6. linux 移动硬盘 mnt,linux 移动硬盘 mnt
  7. Lucence.Net学习+盘古分词
  8. [3] ADB 设备连接管理
  9. 好的技术不一定能给你带来财富,但是好的工具一定可以让你创造财富
  10. Wss3入门(2):设置匿名访问包括匿名阅读和匿名评论,修改评论的字段等。
  11. 苹果mac绘图软件:AutoCAD
  12. word表格转为html5,怎么把网页版的表格转至Word
  13. 网络中搜不到局域网内的其他计算机,局域网中搜不到其他计算机怎么修复
  14. 全自动软化水设备:全自动软化水设备选型要点说明
  15. 2021-06-22 19点30 程序外生活 - 中国A50指数 机器预测学习跟踪记录 - 周2白天反弹持续进行,量能不高但平稳,持续反弹概率大,等待顶部信号出现再反向交易,暂多。但周月线持续跌势。
  16. 唯一约束和主键约束的区别
  17. Ogre天龙八部地形mesh部分的C++源码
  18. Win10自带电影和电视报错0x800700ea的解决方法
  19. Linux Linux中“”, “.“和”..“的含义
  20. 录制快、回放稳,爱奇艺iOS云录制回放平台技术实践

热门文章

  1. 一路(16)奔波,一起(17)前行—2016 年终总结
  2. 关于Node.js中内存管理的思考与实践
  3. 使用 Python 从谷歌搜索结果中抓取图像
  4. 晦涩难懂的c语言语句,《C++覆辙录》——2.12:晦涩难懂的operator -
  5. 如何处理WordPress上传资源报HTTP错误
  6. 帅案之上——作为开发者的远见与卓识
  7. html适配手机 响应式,移动端适配(响应式)
  8. MATLAB:randn简介
  9. C措辞教程第一章: C措辞概论 (5)
  10. 课程预约小程序开发需要哪些功能?