This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

battery service

I'm working on a project and want to use the battery service to do that, send notification when the battery reach to 75%, I decided to use the battery service found on the sdk files & simply like the ble_app_hrs application but I'll do some edits

the used functions in main :

	/**@brief Function for performing a battery measurement, and update the Battery Level characteristic in the Battery Service.
 */
static void battery_level_update(void)
{
		uint32_t err_code;
		uint8_t  battery_level;

		battery_level = (uint8_t)sensorsim_measure(&m_battery_sim_state, &m_battery_sim_cfg);
		//set the condition for sending notification
		err_code = ble_bas_battery_level_update(&m_bas, battery_level);
		if ((err_code != NRF_SUCCESS) &&
				(err_code != NRF_ERROR_INVALID_STATE) &&
				(err_code != BLE_ERROR_NO_TX_BUFFERS) &&
				(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
		)
		{
				APP_ERROR_HANDLER(err_code);
		}
}

the used functions in the ble_bas.c file :

      uint32_t ble_bas_battery_level_update(ble_bas_t * p_bas, uint8_t battery_level)
     {
      uint32_t err_code = NRF_SUCCESS;
      ble_gatts_value_t gatts_value;

     if (battery_level != p_bas->battery_level_last)
     {
    // Initialize value struct.
    memset(&gatts_value, 0, sizeof(gatts_value));

    gatts_value.len     = sizeof(uint8_t);
    gatts_value.offset  = 0;
    gatts_value.p_value = &battery_level;

    // Save new battery value.
    p_bas->battery_level_last = battery_level;

    // Update database.
    err_code = sd_ble_gatts_value_set(p_bas->conn_handle,
                                      p_bas->battery_level_handles.value_handle,
                                      &gatts_value);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    // Send value if connected and notifying.
    if ((p_bas->conn_handle != BLE_CONN_HANDLE_INVALID) && p_bas->is_notification_supported)
    {
        ble_gatts_hvx_params_t hvx_params;

        memset(&hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = p_bas->battery_level_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = gatts_value.offset;
        hvx_params.p_len  = &gatts_value.len;
        hvx_params.p_data = gatts_value.p_value;

        err_code = sd_ble_gatts_hvx(p_bas->conn_handle, &hvx_params);
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
    }
}

return err_code;

}

what I'm asking about does the ble_bas.c file by default defined to support to send notifications upon change on battery level ?

can I modify that file to send notification only when the battery level reaches to a certain level or it's not recommended ?

I didn't know how to use battery_level_update function, it's not called on the ble_bas_init or the service definition, I mean when the main function call this function to measure the battery or when the battery level is changed this function is called to measure the new battery level then send a notifaction that the battery level is changed, is that right ? ?

I need to measure the battery level every time & take an action on the same board "no need for the mobile app in this case ", for example :

if the battery level was 100% then the used leds intensity will be 75% .

if the battery level was 80% then the used intensity will be 90% and the maximum will be 100%, & the user can set the intensity of each pattern starting from 0% to 90%.

can I call this function to take an action upon the change of battery level "ble_bas_battery_level_update " ? thanks in advance for you great effort.

Parents
  • See ble_bas.h:

    ble_bas_battery_level_update(...) is called by the application (probably main.c for you) after performing a battery measurement. If notifications are enabled for this service (they are by default, see services_init() in main.c) and the battery level has changed from the last reading, then the battery level characteristic is sent to the client over your ble connection.

    What this means for you is that every time you measure your battery level you will want to call ble_battery_level_update(...). At this point every time the battery level changes the value will be sent over ble. But if you only want to update the value when it reaches a predefined level (say 75%) then keep notifications disabled by default bas_init.support_notification = false; and make this true only when you read 75%. Then set it back to false or whatever you want.

    Experiment with this and let me know if it works! -Mike

  • thank you for your help, what I'm thinking about is to do something like this, is that true ? ?

    	/**@brief Function for performing a battery measurement, and update the Battery Level characteristic in the Battery Service.
     */
    static void battery_level_update(void)
    {
    		uint32_t err_code;
    		uint8_t  battery_level;
    
    		battery_level = (uint8_t)sensorsim_measure(&m_battery_sim_state, &m_battery_sim_cfg);
    		//set the condition for sending notification
    	if(battery_level == 100)
    	{
    		err_code = ble_bas_battery_level_update(&m_bas, battery_level);
    		if ((err_code != NRF_SUCCESS) &&
    				(err_code != NRF_ERROR_INVALID_STATE) &&
    				(err_code != BLE_ERROR_NO_TX_BUFFERS) &&
    				(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    		)
    		{
    				APP_ERROR_HANDLER(err_code);
    		}
    	}
    
Reply
  • thank you for your help, what I'm thinking about is to do something like this, is that true ? ?

    	/**@brief Function for performing a battery measurement, and update the Battery Level characteristic in the Battery Service.
     */
    static void battery_level_update(void)
    {
    		uint32_t err_code;
    		uint8_t  battery_level;
    
    		battery_level = (uint8_t)sensorsim_measure(&m_battery_sim_state, &m_battery_sim_cfg);
    		//set the condition for sending notification
    	if(battery_level == 100)
    	{
    		err_code = ble_bas_battery_level_update(&m_bas, battery_level);
    		if ((err_code != NRF_SUCCESS) &&
    				(err_code != NRF_ERROR_INVALID_STATE) &&
    				(err_code != BLE_ERROR_NO_TX_BUFFERS) &&
    				(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    		)
    		{
    				APP_ERROR_HANDLER(err_code);
    		}
    	}
    
Children
No Data
Related