Battery Service是有关电池特性方面的服务,如果需要它,在初始化时将它加入到蓝牙协议栈。

如果通过ble_bas_battery_level_update(),电池电量将会通知,Battery Service将发送事件到应用程序。

(1)Battery Service事件类型

/**@brief Battery Service event type. */
typedef enum
{
    BLE_BAS_EVT_NOTIFICATION_ENABLED,                             /**< Battery value notification enabled event. */
    BLE_BAS_EVT_NOTIFICATION_DISABLED                             /**< Battery value notification disabled event. */
} ble_bas_evt_type_t;  

/**@brief Battery Service event. */
typedef struct
{
    ble_bas_evt_type_t evt_type;                                  /**< Type of event. */
} ble_bas_evt_t;  

(2)Battery Service事件处理函数

// Forward declaration of the ble_bas_t type.
typedef struct ble_bas_s ble_bas_t;  

/**@brief Battery Service event handler type. */
typedef void (*ble_bas_evt_handler_t) (ble_bas_t * p_bas, ble_bas_evt_t * p_evt);  

(3)ble_bas_init_t:初始化用到的结构体

/**@brief Battery Service init structure. This contains all options and data needed for
 *        initialization of the service.*/
typedef struct
{
    ble_bas_evt_handler_t         evt_handler;                    /**< Event handler to be called for handling events in the Battery Service. */
    bool                          support_notification;           /**< TRUE if notification of Battery Level measurement is supported. */
    ble_srv_report_ref_t *        p_report_ref;                   /**< If not NULL, a Report Reference descriptor with the specified value will be added to the Battery Level characteristic */
    uint8_t                       initial_batt_level;             /**< Initial battery level */
    ble_srv_cccd_security_mode_t  battery_level_char_attr_md;     /**< Initial security level for battery characteristics attribute */
    ble_gap_conn_sec_mode_t       battery_level_report_read_perm; /**< Initial security level for battery report read attribute */
} ble_bas_init_t;  

(4)包含可变状态信息的结构体ble_bas_t:

/**@brief Battery Service structure. This contains various status information for the service. */
typedef struct ble_bas_s
{
    ble_bas_evt_handler_t         evt_handler;                    /**< Event handler to be called for handling events in the Battery Service. */
    uint16_t                      service_handle;                 /**< Handle of Battery Service (as provided by the BLE stack). */
    ble_gatts_char_handles_t      battery_level_handles;          /**< Handles related to the Battery Level characteristic. */
    uint16_t                      report_ref_handle;              /**< Handle of the Report Reference descriptor. */
    uint8_t                       battery_level_last;             /**< Last Battery Level measurement passed to the Battery Service. */
    uint16_t                      conn_handle;                    /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
    bool                          is_notification_supported;      /**< TRUE if notification of Battery Level is supported. */
} ble_bas_t; 

(5)几个函数说明:

uint32_t  ble_bas_init (ble_bas_t *p_bas, const ble_bas_init_t *p_bas_init) //服务初始化
void  ble_bas_on_ble_evt (ble_bas_t *p_bas, ble_evt_t *p_ble_evt)  //处理协议栈事件的回调函数
uint32_t  ble_bas_battery_level_update (ble_bas_t *p_bas, uint8_t battery_level) //更新电池电量时调用  

(6)初始化操作

// Here the sec level for the Battery Service can be changed/increased.
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.cccd_write_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init.battery_level_char_attr_md.write_perm);  

BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_report_read_perm);  

bas_init.evt_handler          = NULL;
bas_init.support_notification = true;
bas_init.p_report_ref         = NULL;
bas_init.initial_batt_level   = ;  

err_code = ble_bas_init(&bas, &bas_init);
APP_ERROR_CHECK(err_code);  

BLE-NRF51822教程19-Battery Service的更多相关文章

  1. 低功耗蓝牙4.0BLE编程-nrf51822开发(6)-Battery Service

    Battery Service是有关电池特性方面的服务,如果需要它,在初始化时将它加入到蓝牙协议栈. 如果通过ble_bas_battery_level_update(),电池电量将会通知,Batte ...

  2. 蓝牙BLE实用教程

    蓝牙BLE实用教程 Bluetooth BLE 欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过 设置 里的修改模板来改变新建文章的内容. 1.蓝牙BLE常见问答 Q: Smart Re ...

  3. [蓝牙] 5、Battery Service module

    Detailed Description This module implements the Battery Service with the Battery Level characteristi ...

  4. node-webkit教程(11)Platform Service之shell

    node-webkit教程(11)Platform Service之shell 文/玄魂 目录 node-webkit教程(10)Platform Service之shell 前言 11.1  She ...

  5. node-webkit教程(10)Platform Service之File dialogs

    node-webkit教程(10)Platform Service之File dialogs 文/玄魂 目录 node-webkit教程(10)Platform Service之File dialog ...

  6. node-webkit教程(8)Platform Service之Clipboard

    node-webkit教程(8)Platform Service之Clipboard 文/玄魂 目录 node-webkit教程(8)Platform Service之Clipboard 前言 8.1 ...

  7. node-webkit教程(7)Platform Service之APP

    node-webkit教程(7)Platform Service之APP 文/玄魂 前言 几个月前,要开发一个简易的展示应用,要求支持离线播放(桌面应用)和在线播放(web应用). 当时第一想到的是f ...

  8. [译]Vulkan教程(19)渲染和呈现

    [译]Vulkan教程(19)渲染和呈现 Rendering and presentation 渲染和呈现 Setup 设置 This is the chapter where everything ...

  9. Directx11教程(19) 画一个简单的地形

    原文:Directx11教程(19) 画一个简单的地形       通常我们在xz平面定义一个二维的网格,然后y的值根据一定的函数计算得到,比如正弦.余弦函数的组合等等,可以得到一个看似不错的地形或者 ...

  10. 蓝牙BLE实用教程(转载)

    欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过 设置 里的修改模板来改变新建文章的内容. 1.蓝牙BLE常见问答 Q: Smart Ready 和 Smart 以及传统蓝牙之间是什么关 ...

随机推荐

  1. 版本引发的血案check the manual that corresponds to your MySQL server version for the right syntax

    该错误mysql5.1有问题,mysql5.3版本没问题

  2. node基础 --概念

    非阻塞IO: node.js使用了事件轮询 setTimeout是非阻塞的: 对于像http,net等原生模块中IO部分也采用了事件轮询,其本质是: 当node接受到浏览器的http请求时,底层的TC ...

  3. Xamarin Android提示内存溢出错误

    Xamarin Android提示内存溢出错误 错误信息:java.lang.OutOfMemoryError, Consider increasing the value of $(JavaMaxi ...

  4. Sprint第一个冲刺(第十一天)

    一.Sprint介绍 修改登录信息界面(修改用户名.密码.邮箱.电话.年龄),且同步到云端:修改Item布局:增添设置页. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:

  5. 简单几何(求交点) UVA 11437 Triangle Fun

    题目传送门 题意:三角形三等分点连线组成的三角形面积 分析:入门题,先求三等分点,再求交点,最后求面积.还可以用梅涅劳斯定理来做 /********************************** ...

  6. Grasshopper 2.0 MP Color FireWire 1394b (Sony ICX274)

        相机参数如下,参见这里: Resolution 1624 x 1224 Frame Rate 30 FPS Megapixels 2.0 MP Chroma Color Sensor Name ...

  7. ADO.NET对象之 DataTable

    ADO.NET可以在与数据库断开连接的方式下通过DataSet或DataTable对象进行数据处理,当需要更新数据时才重新与数据源进行连接,并更新数据源. DataTable对象表示保存在本机内存中的 ...

  8. vi用法

  9. 用Editplus开发HTML

    准备工作 设置Editplus默认的打开浏览器为系统默认浏览器 取消Editplus的临时缓存文件 ☆ 设置不生成临时文件 这里的临时文件,指的是.bak文件,当你在Editplus下修改任意一个文件 ...

  10. MyBatis+springMVC+easyUI (dataGirl)实现分页

    页面展示效果. 页面代码: <%@ page contentType="text/html;charset=UTF-8" language="java"  ...