Battery Service

The BatteryService implements the standard BLE battery service, providing battery state information from a BLE peripheral to a BLE central.

API Reference

Header File

Classes

class BatteryService : public espp::BaseComponent

Battery Service This class is responsible for creating and managing the Battery Service.

The service is created with the following characteristics:

  • Battery Level (read, notify, unencrypted)

The Battery Level characteristic is a standard characteristic defined by the Bluetooth SIG. It is used to report the current battery level of the device.

Public Functions

inline explicit BatteryService(espp::Logger::Verbosity log_level = espp::Logger::Verbosity::WARN)

Constructor

Parameters:

log_level – The log level for the component

inline void init(NimBLEServer *server)

Initialize the Battery Service

Parameters:

server – The BLE server to add the service to

inline void deinit()

Deinitialize the Battery Service

Note

This should only be called after NimBLEDevice::deinit(true) has been called, since that will free the memory used by the service

inline void start()

Start the service

Note

Kept for API compatibility: NimBLE starts every service when the server starts (esp-nimble-cpp 2.x), so there is nothing to do here.

inline NimBLEService *get_service()

Get the service

Returns:

The Battery Service

inline NimBLEUUID uuid()

Get the UUID of the service

Returns:

The UUID of the service

inline void set_battery_level(uint8_t level)

Set the battery level

Note

The level is clamped to the range [0, 100]

Note

This must be called after the service has been initialized

Parameters:

level – The battery level

inline uint8_t get_battery_level()

Get the battery level

Returns:

The battery level

inline const std::string &get_name() const

Get the name of the component

Note

This is the tag of the logger

Returns:

A const reference to the name of the component

inline void set_log_tag(const std::string_view &tag)

Set the tag for the logger

Parameters:

tag – The tag to use for the logger

inline espp::Logger::Verbosity get_log_level() const

Get the log level for the logger

Returns:

The verbosity level of the logger

inline void set_log_level(espp::Logger::Verbosity level)

Set the log level for the logger

Parameters:

level – The verbosity level to use for the logger

inline void set_log_verbosity(espp::Logger::Verbosity level)

Set the log verbosity for the logger

See also

set_log_level

Note

This is a convenience method that calls set_log_level

Parameters:

level – The verbosity level to use for the logger

inline espp::Logger::Verbosity get_log_verbosity() const

Get the log verbosity for the logger

See also

get_log_level

Note

This is a convenience method that calls get_log_level

Returns:

The verbosity level of the logger

inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)

Set the rate limit for the logger

Note

Only calls to the logger that have _rate_limit suffix will be rate limited

Parameters:

rate_limit – The rate limit to use for the logger

Public Static Attributes

static constexpr uint8_t BATTERY_LEVEL_MAX = 100

Maximum battery level.

static constexpr uint16_t BATTERY_LEVEL_UNIT = 0x27AD

Unit is percentage.

static constexpr uint16_t BATTERY_SERVICE_UUID = 0x180F

Battery Service UUID.

static constexpr uint16_t BATTERY_LEVEL_CHAR_UUID = 0x2A19

Battery Level Characteristic UUID.