First calling ble_bas_battery_level_update() failed errCode=0x8

First calling ble_bas_battery_level_update() failed errCode=0x8. But afterward calling ble_bas_battery_level_update() successfully.

# SEGGER J-Link RTT Viewer V6.32i Terminal Log File
# Compiled: 15:22:50 on Jul 24 2018
# Logging started @ 14 Jul 2022 15:28:04
 0> <info> app: Version 1.0.0
 0>
 0> <info> app: ========| flash info |========
 0> <info> app: erase unit:   4096 bytes
 0> <info> app: program unit: 4 bytes
 0> <info> app: end address: 0x7FFFF
 0> <info> app: ==============================
 0> <info> app_timer: RTC: initialized.
 0> <debug> app: NOR_DATA_PERIOD=819
 0>
 0> <debug> app: NOR_RECORD_PERIOD=8192
 0>
 0> <info> app: bsp_button_longkey_handler 0.
 0> <warning> app: Battery volage 85mV is too low
 0>
 0> <warning> app: Battery volage 17mV is too low
 0>
 0> <info> app: Reg 9 A
 0> <info> app: bsp_button_longkey_handler 1.
 0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
 0> <info> app: Connected.
 0> <debug> app: state=1
 0>
 0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
 0> <info> ble_bas: Battery level has been updated: 0%
 0> <debug> ble_bas: Error: 0x00000008 while sending notification with conn_handle: 0x0000
 0> <warning> app: ble_bas_battery_level_update() failed errCode=0x8
 0>
 0> <debug> app: mv=2144, percent=0
 0>
 0> <info> ble_bas: Battery level has been updated: 69%
 0> <info> ble_bas: Battery notification has been sent using conn_handle: 0x0000
 0> <debug> app: mv=3998, percent=69
 0>
 0> <info> ble_bas: Battery level has been updated: 0%
 0> <info> ble_bas: Battery notification has been sent using conn_handle: 0x0000
 0> <debug> app: mv=8, percent=0
 0>
 0> <debug> app: mv=196, percent=0
 0>
 0> <info> ble_bas: Battery level has been updated: 85%
 0> <info> ble_bas: Battery notification has been sent using conn_handle: 0x0000
 0> <debug> app: mv=4101, percent=85
 0>
 0> <info> ble_bas: Battery level has been updated: 0%
 0> <info> ble_bas: Battery notification has been sent using conn_handle: 0x0000
 0> <debug> app: mv=1922, percent=0
 0>

Parents
  • #include "sdk_config.h"
    #include "battery_ble.h"
    #include "ble_bas.h"
    #include "battery_voltage.h"
    #include "ble_srv_common.h"
    #include "error.h"
    #include "nrf_log.h"
    
    #define INVALID_BATTERY_LEVEL 0xFF // Invalid/default battery level.
    
    static bool      _gBleBasInitialized = false;
    static ble_bas_t _gBleBas;
    
    /* Event handler, handles events in the Battery Service.
     *
     * details This callback function is often used to enable a service when requested over BLE,
     * and disable when not requested to save power. m_batt_meas runs all the time, so no
     * enabling/disabling is performed.
     */
    static void _batteryBleEvtHandler(ble_bas_t * pBas, ble_bas_evt_t * pEvt)
    {
        switch (pEvt->evt_type)
        {
            case BLE_BAS_EVT_NOTIFICATION_ENABLED:
                NRF_LOG_DEBUG("BLE_BAS_EVT_NOTIFICATION_ENABLED \n");
                break;
    
            case BLE_BAS_EVT_NOTIFICATION_DISABLED:
                NRF_LOG_DEBUG("BLE_BAS_EVT_NOTIFICATION_DISABLED \n");
                break;
    
            default:
                break;
        }
    }
    
    int batteryBleInit(void) {
        // Initialize Battery Service.
        ble_bas_init_t basInit;
        memset(&basInit, 0, sizeof(basInit));
    
        // Security level for the Battery Service
        basInit.bl_rd_sec        = SEC_OPEN;
        basInit.bl_cccd_wr_sec   = SEC_OPEN;
        basInit.bl_report_rd_sec = SEC_OPEN;
    
        basInit.evt_handler          = _batteryBleEvtHandler;
        basInit.support_notification = true;
        basInit.p_report_ref         = NULL;
        basInit.initial_batt_level   = INVALID_BATTERY_LEVEL;
    
        const ret_code_t errCode = ble_bas_init(&_gBleBas, &basInit);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_bas_init() failed\n");
            return ERROR_BLE_BAS_INITIALIZE_FAIL;
        }
        
        _gBleBasInitialized = true;
    
        return 0;
    }
    
    int batteryBleUpdate(void) {
        if (!_gBleBasInitialized) {
            return ERROR_BLE_BAS_UNINITIALIZED;
        }
    
        const int mv          = batteryVoltageGet();
        const uint8_t percent = batteryVoltage2Percent(mv);
        ret_code_t errCode    = ble_bas_battery_level_update(&_gBleBas, percent, BLE_CONN_HANDLE_ALL);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_bas_battery_level_update() failed errCode=0x%x\n", errCode);
            return ERROR_BLE_BAS_UPDATE_FAIL;
        }
    
        NRF_LOG_DEBUG("mv=%d, percent=%u\n", mv, percent);
    
        return 0;
    }
    
    
    battery_ble.h

Reply
  • #include "sdk_config.h"
    #include "battery_ble.h"
    #include "ble_bas.h"
    #include "battery_voltage.h"
    #include "ble_srv_common.h"
    #include "error.h"
    #include "nrf_log.h"
    
    #define INVALID_BATTERY_LEVEL 0xFF // Invalid/default battery level.
    
    static bool      _gBleBasInitialized = false;
    static ble_bas_t _gBleBas;
    
    /* Event handler, handles events in the Battery Service.
     *
     * details This callback function is often used to enable a service when requested over BLE,
     * and disable when not requested to save power. m_batt_meas runs all the time, so no
     * enabling/disabling is performed.
     */
    static void _batteryBleEvtHandler(ble_bas_t * pBas, ble_bas_evt_t * pEvt)
    {
        switch (pEvt->evt_type)
        {
            case BLE_BAS_EVT_NOTIFICATION_ENABLED:
                NRF_LOG_DEBUG("BLE_BAS_EVT_NOTIFICATION_ENABLED \n");
                break;
    
            case BLE_BAS_EVT_NOTIFICATION_DISABLED:
                NRF_LOG_DEBUG("BLE_BAS_EVT_NOTIFICATION_DISABLED \n");
                break;
    
            default:
                break;
        }
    }
    
    int batteryBleInit(void) {
        // Initialize Battery Service.
        ble_bas_init_t basInit;
        memset(&basInit, 0, sizeof(basInit));
    
        // Security level for the Battery Service
        basInit.bl_rd_sec        = SEC_OPEN;
        basInit.bl_cccd_wr_sec   = SEC_OPEN;
        basInit.bl_report_rd_sec = SEC_OPEN;
    
        basInit.evt_handler          = _batteryBleEvtHandler;
        basInit.support_notification = true;
        basInit.p_report_ref         = NULL;
        basInit.initial_batt_level   = INVALID_BATTERY_LEVEL;
    
        const ret_code_t errCode = ble_bas_init(&_gBleBas, &basInit);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_bas_init() failed\n");
            return ERROR_BLE_BAS_INITIALIZE_FAIL;
        }
        
        _gBleBasInitialized = true;
    
        return 0;
    }
    
    int batteryBleUpdate(void) {
        if (!_gBleBasInitialized) {
            return ERROR_BLE_BAS_UNINITIALIZED;
        }
    
        const int mv          = batteryVoltageGet();
        const uint8_t percent = batteryVoltage2Percent(mv);
        ret_code_t errCode    = ble_bas_battery_level_update(&_gBleBas, percent, BLE_CONN_HANDLE_ALL);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_bas_battery_level_update() failed errCode=0x%x\n", errCode);
            return ERROR_BLE_BAS_UPDATE_FAIL;
        }
    
        NRF_LOG_DEBUG("mv=%d, percent=%u\n", mv, percent);
    
        return 0;
    }
    
    
    battery_ble.h

Children
No Data
Related