低功耗藍芽4.0BLE程式設計-nrf51822開發(6)-Battery Service
阿新 • • 發佈:2019-01-22
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 = 100;
err_code = ble_bas_init(&bas, &bas_init);
APP_ERROR_CHECK(err_code);