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. GridView块布局

    <GridView android:id="@+id/gridview" android:layout_width="match_parent" andr ...

  2. cf 621D

    http://acm.zzkun.com/archives/717 这个大神的解答非常,额 猥琐.但是实在是太强了.感觉所有的大数都可以用 long double 了.

  3. Android 编程下 App Install Location

    从 API 8 开始(参考官方文档:App Install Location | Android Developers),你可以将你的应用安装在外部储存中(例如,安装到设备的 SD 卡上).这是一个可 ...

  4. SPEL语言-Spring Expression Language

    Spring表达式语言全称为"Spring Expression Language",缩写为"SpEL",类似于Struts 2x中使用的OGNL表达式语言,能 ...

  5. 游戏 scrollView

    using UnityEngine; using System.Collections; public class LLL : MonoBehaviour { Vector2 scrollPositi ...

  6. Mysql_mysql force Index 强制索引

    其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...

  7. [ZT] 几大酒店集团美国Co-Brand信用卡比较(三)如何选择最适合你的酒店联名信用卡

    原文地址: http://www.3798.com/archives/596.html 接着对我们这种不是某个酒店忠诚客户的用户选择卡片进行分析.首先要强调的是,我们比较的是信用卡项目本身,而不是酒店 ...

  8. 三星S4接电话黑屏无法挂断通话

    最近发现S4的通话距离感应起出了问题,接电话后直接熄屏,按什么按钮都没有反应.通话结束后只能等对方挂断才会恢复正常,再或者长按9秒电源键强制重启.极大的影响了实用体验.网上搜了下,发现这样的问题还不少 ...

  9. 如何在URL筛选管理器中过滤不需要的URL

    互联网可以说是一把名副其实的双刃剑.一方面其可以提高工作效率.给企业提供充分的资源;另一方面如果管理不严,也会带来很多的隐患.如员工在上班时间玩游戏.炒股等等.为此现在很多企业希望对员工的网络行为进行 ...

  10. 数据库发出sql命令mysql教程

    $db = mysql教程_connect("localhost", "phpdb", "phpdb");mysql_select_db(& ...