Skip to content

Commit 5eaa5b6

Browse files
committed
fix(ble): add ble hid change battery level api
Close #385
1 parent 64848f4 commit 5eaa5b6

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

examples/bluetooth/ble_remote_control/main/hidd.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ client_info_t client;
2323
extern bool is_connected; // Need this here in the case of disconnection
2424

2525
// Characteristic Values
26-
static const uint8_t val_batt_level = 0;
26+
static uint8_t val_batt_level = 0;
2727
// 0% battery level (assume battery not present, for user to modify if need)
2828
static uint8_t val_bat_cccd[] = {0x01, 0x00};
2929
// CCCD allows the Central Device to enable/disable notifications/indications
@@ -479,6 +479,7 @@ esp_err_t copy_attribute_handles(int instance_id, uint16_t *handles, int num_han
479479
switch (instance_id) {
480480
case INST_ID_BAT_SVC:
481481
memcpy(handle_table.handles_bat_svc, handles, num_handle * sizeof(uint16_t));
482+
client.bat_attr_handle = handles[IDX_BAT_LVL_VAL];
482483
return ESP_OK;
483484
case INST_ID_HID:
484485
memcpy(handle_table.handles_hid_svc, handles, num_handle * sizeof(uint16_t));
@@ -558,3 +559,21 @@ esp_err_t send_user_input(void)
558559
false // need_confirm
559560
);
560561
}
562+
563+
esp_err_t set_hid_battery_level(uint8_t value)
564+
{
565+
if (value > 100) {
566+
value = 100;
567+
}
568+
569+
val_batt_level = value;
570+
571+
return esp_ble_gatts_send_indicate(
572+
client.gatt_if, // gatts_if
573+
client.connection_id, // conn_id
574+
client.bat_attr_handle, // attr_handle
575+
1, // value_len
576+
&val_batt_level, // value
577+
false // need_confirm
578+
);
579+
}

examples/bluetooth/ble_remote_control/main/hidd.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -140,6 +140,7 @@ typedef struct {
140140
esp_gatt_if_t gatt_if;
141141
uint16_t connection_id;
142142
uint16_t attr_handle;
143+
uint16_t bat_attr_handle;
143144
} client_info_t;
144145

145146
/**
@@ -164,3 +165,11 @@ void set_hid_report_values(uint8_t joystick_x, uint8_t joystick_y, uint8_t butto
164165
* @brief Send the HID Report to the client (notification)
165166
*/
166167
esp_err_t send_user_input(void);
168+
169+
/**
170+
* @brief Set the hid battery level value to be sent
171+
*
172+
* @note Values to be set depends on the device specification
173+
* @note The value is capped at 100
174+
*/
175+
esp_err_t set_hid_battery_level(uint8_t value);

examples/bluetooth/ble_remote_control/main/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void joystick_task()
7676
uint8_t hat_switch = 0; // unused in this example
7777
uint8_t button_in = 0;
7878
uint8_t throttle = 0; // unused in this example
79-
79+
uint8_t battery_level = 0;
8080
while (true) {
8181
DELAY(HID_LATENCY);
8282

@@ -121,6 +121,8 @@ void joystick_task()
121121
set_hid_report_values(x_axis, y_axis, button_in, hat_switch, throttle);
122122
print_user_input_report(x_axis, y_axis, hat_switch, button_in, throttle);
123123
ret = send_user_input();
124+
battery_level = (battery_level + 1) % 100;
125+
set_hid_battery_level(battery_level);
124126
}
125127

126128
// Alternatively, to simply poll user input can do:

0 commit comments

Comments
 (0)