This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

bas vs. bas_client

I am using the nrf Connect v1.9.1 SDK. I am currently trying to read the battery level for the CR2032 battery for the nrf52dk_nrf52832 build. There are two different approaches it seems to reading the battery level. One where you use the bas.h file and use function bt_bas_get_battery_level which is demonstrated in the peripheral_hr sample. The other method is used in the central_bas example, uses the bas_client.h file with a callback function. I currently have the former method in my code but it always seems to return 0x64/100 percent.

What differentiates these?

Which is more accurate?

Why would someone choose one over the other?

Why does the first method always give me 100 percent? I have purposely depleted the CR2032 battery to 2.2 V out of the 3 V.

  • Also, what is error -22? I cannot find any documentation on what the value returned from bt_bas_read_battery_level(&bas, cb_func) means

  • Hi,

    What differentiates these?

    bas.h is the header file for the BAS server implementation. In GATT, servers provide information to the clients, so the server is running on the device that you want to know the battery level of. Another device that wants to read the battery level needs to have a BAS client implementation, which is what you have in bas_client.h. (In most cases, the servers are on the peripheral side and the clients are on the central side. That is not always the case though, and the same device can be both a GATT client and a GATT server.)

    Which is more accurate?

    These are different things and not related to accuracy. Moreover, the Bluetooth BAS client implementation only handles the Bluetooth part of this. Actually measuring the battery level is handled outside of this module, and typically needs to be application specific. The bt_bas_get_battery_level() simply returns the internal battery level, which was set by bt_bas_set_battery_level().

    Why does the first method always give me 100 percent? I have purposely depleted the CR2032 battery to 2.2 V out of the 3 V.

    Referring to the peripheral_hr sample this only use a dummy value and decrements the battery level by 1 every time it is reported (see implementation of bas_notify()). So there is no connection with the actual battery here. For that, you need to implement the battery measurement logic yourself in a way that fits your HW.

    Tom Nowak said:
    Also, what is error -22? I cannot find any documentation on what the value returned from bt_bas_read_battery_level(&bas, cb_func) means

    bt_bas_read_battery_level() can return -22 (-EINVAL) in a few cases: Either one of the input parameters were NULL, or there is no associated connection (so no device to read the battery characteristic from).

    (Note that bt_bas_read_battery_level() is from the BAS client implementation, which is probably not what you want).

    Tom Nowak said:
    I cannot find any documentation on what the value returned from bt_bas_read_battery_level(&bas, cb_func) means

    Unfortunately error codes are sparsely documented in Zephyr and the nRF Connect SDK, but you can refer to errno.h to see the defined "name" of the error code, and see in the implementatino where that error code is returned.

Related