BLE-NRF51822教程19-Battery Service

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

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

(1)Battery Service事件类型

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**@brief Battery Service event type. */  
  2. typedef enum  
  3. {  
  4.     BLE_BAS_EVT_NOTIFICATION_ENABLED,                             /**< Battery value notification enabled event. */  
  5.     BLE_BAS_EVT_NOTIFICATION_DISABLED                             /**< Battery value notification disabled event. */  
  6. } ble_bas_evt_type_t;  
  7.   
  8. /**@brief Battery Service event. */  
  9. typedef struct  
  10. {  
  11.     ble_bas_evt_type_t evt_type;                                  /**< Type of event. */  
  12. } ble_bas_evt_t;  

(2)Battery Service事件处理函数

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Forward declaration of the ble_bas_t type.   
  2. typedef struct ble_bas_s ble_bas_t;  
  3.   
  4. /**@brief Battery Service event handler type. */  
  5. typedef void (*ble_bas_evt_handler_t) (ble_bas_t * p_bas, ble_bas_evt_t * p_evt);  

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

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**@brief Battery Service init structure. This contains all options and data needed for 
  2.  *        initialization of the service.*/  
  3. typedef struct  
  4. {  
  5.     ble_bas_evt_handler_t         evt_handler;                    /**< Event handler to be called for handling events in the Battery Service. */  
  6.     bool                          support_notification;           /**< TRUE if notification of Battery Level measurement is supported. */  
  7.     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 */  
  8.     uint8_t                       initial_batt_level;             /**< Initial battery level */  
  9.     ble_srv_cccd_security_mode_t  battery_level_char_attr_md;     /**< Initial security level for battery characteristics attribute */  
  10.     ble_gap_conn_sec_mode_t       battery_level_report_read_perm; /**< Initial security level for battery report read attribute */  
  11. } ble_bas_init_t;  

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

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**@brief Battery Service structure. This contains various status information for the service. */  
  2. typedef struct ble_bas_s  
  3. {  
  4.     ble_bas_evt_handler_t         evt_handler;                    /**< Event handler to be called for handling events in the Battery Service. */  
  5.     uint16_t                      service_handle;                 /**< Handle of Battery Service (as provided by the BLE stack). */  
  6.     ble_gatts_char_handles_t      battery_level_handles;          /**< Handles related to the Battery Level characteristic. */  
  7.     uint16_t                      report_ref_handle;              /**< Handle of the Report Reference descriptor. */  
  8.     uint8_t                       battery_level_last;             /**< Last Battery Level measurement passed to the Battery Service. */  
  9.     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). */  
  10.     bool                          is_notification_supported;      /**< TRUE if notification of Battery Level is supported. */  
  11. } ble_bas_t;  

(5)几个函数说明:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. uint32_t  ble_bas_init (ble_bas_t *p_bas, const ble_bas_init_t *p_bas_init) //服务初始化  
  2. void  ble_bas_on_ble_evt (ble_bas_t *p_bas, ble_evt_t *p_ble_evt)  //处理协议栈事件的回调函数  
  3. uint32_t  ble_bas_battery_level_update (ble_bas_t *p_bas, uint8_t battery_level) //更新电池电量时调用  

(6)初始化操作

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Here the sec level for the Battery Service can be changed/increased.  
  2. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.cccd_write_perm);  
  3. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.read_perm);  
  4. BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init.battery_level_char_attr_md.write_perm);  
  5.   
  6. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_report_read_perm);  
  7.   
  8. bas_init.evt_handler          = NULL;  
  9. bas_init.support_notification = true;  
  10. bas_init.p_report_ref         = NULL;  
  11. bas_init.initial_batt_level   = 100;  
  12.   
  13. err_code = ble_bas_init(&bas, &bas_init);  
  14. APP_ERROR_CHECK(err_code);  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值