使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之三

使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之三

3 最小BLE应用简介

这个章节简单介绍了在nRF51822芯片上使用S110 SoftDevice协议栈构建一个最小的BLE应用的过程。

3.1初始化介绍

有一些初始化函数通常在执行一个BLE应用之前调用,下面的表格中列出了这些初始化调用函数,在后面将对它们进行详细的介绍。

 

大部分采用数据结构的形式作为输入参数,这些数据结构包含一系列的配置和选项信息,阅读代码中的注释能更好地理解它们。

在广播开始之后,你就进入了main函数中的for循环。

3.2 协议栈S110 SoftDevice

为了使用独特的射频特性,你必须使能S110 SoftDevice协议栈。见《S110 nRF51822 SoftDevice Specification》(S110 nRF51822  SoftDevice协议栈说明书)中对硬件资源的详细需求。

3.3 广播

用于广播的数据结构如下:

        ble_gap.h      中  ble_gap_conn_sec_mode_t

        ble_advdata.h   中  ble_advdata_t

err_code = sd_ble_gap_device_name_set(&device_name_sec_mode, DEVICE_NAME

strlen(DEVICE_NAME));

err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN);

err_code = ble_advdata_set(&advdata);

注意:传递给sd_ble_gap_device_name_set()的安全模式仅适于设备本身的名字。

 

广播参数(ble_gap_adv_params_t)必须通过sd_ble_gap_adv_start()来传递:

err_code = sd_ble_gap_adv_start(&m_adv_params);

3.4 连接参数

SDK提供了一个名为ble_conn_params的模块用于管理连接参数更新,它通过SoftDevice API进行处理,包括请求的时间和第一次请求被拒绝再发送一个新的请求。

在初始化结构体ble_conn_params_init_t中,定义了更新过程的有关参数,例如,是否开始连接,什么开始写入一个特定的CCCD,是否使用连接参数,发送更新请求的延时等等。

在初始化函数ble_conn_params_init()中,使用封装了初始化连接参数(ble_gap_conn_params_t)的结构体ble_conn_params_init_t作为输入参数进行连接参数初始化。

err_code = ble_conn_params_init(&cp_init);

       ble_conn_params SDK模块确保与主机(集中器)的连接参数相适应,如果不适应,外围设备将要求更改连接参数,超过设定的更新次数都没有更新成功后,它就会断开连接或者根据设置返回一个事件到应用层。

3.5 服务

服务可以通过sd_ble_gatts_service_add()进行添加,最好不要在应用层代码中建立服务,而是在一个单独的文件中建立服务。一个服务不是主服务就是次服务,但是在通常实际的应用中大部分使用主服务。变量service_uuid就是你想用于服务的UUID。变量service_handle是一个输出变量,当创建一个服务的时候将会返回一个唯一的句柄值,这个句柄可以在以后用于识别不同的服务。

err_code = sd_ble_gatts_service_add( BLE_GATTS_SVC_TYPE_PRIMARY,

&p_lbs->service_uuid,

&p_lbs->service_handle );

3.6 特性

       特性可以通过sd_ble_gatts_characteristic_add()函数进行添加,它有4个参数。为了代码清晰,这个函数应该只能出现在服务文件中,而不能出现在应用层中。

第1个参数是特性要加入的服务的句柄,第2个参数是特性的结构体,它是一个全局变量,它包含了特性可能用到的性质(读,写,通知等)。第3个参数是值属性的描述,它包含了它的UUID,长度和初始值。第4个参数是返回的特性和描述符的唯一句柄,这个句柄可以在以后用于识别不同的特性。例如,在写事件中用于识别哪一个特性被写入。

err_code = sd_ble_gatts_characteristic_add( p_lbs->service_handle, &char_md,

&attr_char_value,

&p_lbs->led_char_handles);


本文系转载,原文地址:http://www.cnblogs.com/tan-v/p/4276069.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nrf51822中文参考手册nRF51822 是一款集成nRF51x系列无线收发器的超低功耗的片上系统 (Soc) , 包含一个32位ARM Cortex-M0 CPU , flash 存储器和模拟、数字外设。NORDIC SEMICONDUCTOR nRF51822 Product Specification v1.3 Liability disclaimer Nordic Semiconductor ASa reserves the right to make changes without further notice to the product to improve reliability, function or design. Nordic Semiconductor asa does not assume any liability arising out of the application or use of any product or circuits described herein ife support applications Nordic Semiconductor's products are not designed for use in life support appliances, devices, or systems where malfunction of these products can reasonably be expected to result in personal injury. Nordic Semiconductor ASa customers using or selling these products for use in such applications do so at their own risk and agree to fully indemnify Nordic Semiconductor ASA for any damages resulting from such improper use or sale Contact details Foryournearestdistributorpleasevisitwww.nordicsemi.com Information regarding product updates, downloads, and technical support can be accessed through your My Page account on our home page Main office: Otto Nielsens veg 12 Mailing address: Nordic Semiconductor 7052 Trondheim P.O. Box 2336 Norway 7004 Trondhe Phone:+4772898900 Norway 4772898989 画N远 NS-EN ISO 9001 CERTIFICATEDFIRM RoHS and reach statement Nordic semiconductor's products meet the requirements of Directive 2002/95/EC of the European Parliament and of the Council on the restriction of Hazardous Substances(roHS)and the requirements of the reach regulation(EC 1907/2006)on Registration, Evaluation, Authorization and Restriction of Chemicals. The SvHC(Substances of Very High Concern) candidate list is continually being updated Complete hazardous substance reports material composition reports and latest version of nordics reach statementcanbefoundonourwebsitewww.nordicsemicom Page 2 of 67 NORDIC SEMICONDUCTOR nRF51822 Product Specification v1.3 Datasheet status Status Description Objective Pro

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值