Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

GATT Battery Service Client on nRF52820

Hello,

is there an example application in the nRF SDK 17.1.0 where the GATT Battery Service Client is implemented? I would like to add to my application the feature that the nRF52820 detects the battery level of all devices that send out their battery status.

Best regards

  • Hi,

    The BLE Heart Rate Collector Example uses the Battery Service Client. You can look in the function bas_c_evt_handler() for how notifications on the Battery Level characteristics are enabled and how to read the battery level.

    Best regards,
    Marte

  • Hi,

    Is the Peer Manager module absolutely necessary for the Battery Service (it is present in the Heart Rate Collector example)? In my application I only want to detect the RSSI of devices in my vicinity (this already works) and now I would like to implement that the battery status of the devices can be detected.

    Best regards,

    Louis

  • Hi Louis,

    Peer Manager is not necessary for the Battery Service, as it does not require security. You can remove the Peer Manager and use BLE events to handle security requests. For an example of this, you can look in ble_evt_handler() in the Nordic UART Service Client example (examples/ble_central/ble_app_uart_c/main.c):

    case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
        // Pairing not supported.
        err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
        APP_ERROR_CHECK(err_code);
        break;

    Best regards,
    Marte

  • Hi Marte,

    Is it possible to find a device UUID for a device that sends out the Battery Service UUID? I have several devices that send advertisements with a certain hex sequence in the UUID (BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA), and I would like to add the Battery Service to them. When I have found out that the devices are mine (i.e. they send out a specific device UUID), I want to see if they also offer the Battery Service.

    Best regards,

    Louis

  • Hi,

    If the device is advertising, it can advertise the Battery Service. You can look at the BLE Heart Rate Collector Example, which uses the Battery Service. The service is added to the advertising UUIDs:

    static ble_uuid_t m_adv_uuids[] =                                   /**< Universally unique service identifiers. */
    {
        {BLE_UUID_HEART_RATE_SERVICE,           BLE_UUID_TYPE_BLE},
        {BLE_UUID_BATTERY_SERVICE,              BLE_UUID_TYPE_BLE},
        {BLE_UUID_DEVICE_INFORMATION_SERVICE,   BLE_UUID_TYPE_BLE}
    };

    /**@brief Function for initializing the Advertising functionality.
     */
    static void advertising_init(void)
    {
        ret_code_t             err_code;
        ble_advertising_init_t init;
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type               = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance      = true;
        init.advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
        init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        init.advdata.uuids_complete.p_uuids  = m_adv_uuids;
    
        init.config.ble_adv_fast_enabled  = true;
        init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
        init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    
        init.evt_handler = on_adv_evt;
    
        err_code = ble_advertising_init(&m_advertising, &init);
        APP_ERROR_CHECK(err_code);
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }

    Here is a picture from the nRF Connect for Mobile app showing the heart rate collector advertising the Battery Service (0x180F):

    Best regards,
    Marte

Related