-
Notifications
You must be signed in to change notification settings - Fork 7.6k
function setBatteryLevel() in BLEHIDDevice.cpp is invalid #6708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I find this page #4517, it seems to deal with this bug, but I think it doesn't work |
oh, I fix this bug, in BLEHIDDevice.cpp #45 this is origon:
this is my fix:
|
Looks like you've found a possible bug. Changing from |
yes, it works. I don't know why. I only think it should same as "BLE2904* batteryLevelDescriptor = new BLE2904()" |
@BG2CRW Thank you for reporting and investigating it! I think it is neessary to investigate the issue. Descriptor 0x2904 aka "Characteristic Presentation Format" is already added by Line 44. Line 45 adds another Descriptor 0x2902, which is the notify descriptor - necessary for notifying Win10 that the value has changed. |
I’m looking forward to the right code. |
if line 45 is 2902, it won't change battery level since the second connect. |
Based on a fast research, Battery Level Service uses 2901 and 2902, instead of 2904 and 2902. I need to confirm it and then create the code for the 2901, which is different from 2904. |
Thank you. It looks like a lot of work. |
@SuGlider I copy BLE2904.h and BLE2904.cpp as BLE2901.h and BLE2901.h, and replace 2901 as 2904. Then change 2904 in BLEHIDDevice.cpp. |
hello, do you have any idea about it? I can code it by myself. |
After some investigation, it seems that Windows only shows the battery once connected. Some keyboards will offer a separated application that monitors the battery and display it as an icon in the Taskbar. But it sounds like Windows itself doesn't monitor the battery from BLE/Bluetooth devices. |
But, if you test it like me. first, burn it when line 45 is "2902", and connect with your WIN11. Second, change line 45 as 2904 and burn it. Then, the battery level can change real-time whenever connect it to your PC. |
Meanwhile, I use the NRF library to code keyboard, it can change battery level real-time. I trust that, esp32 also can change bettery level real-time on win11. |
@BG2CRW - where in Windows you see the battery level changing? In the Bluetooth settings? |
you can use this test .https://github.com/T-vK/ESP32-BLE-Keyboard and change the main function as: #include "BleKeyboard.h" BleKeyboard bleKeyboard; void setup() { void loop() { delay(5000); |
OK, thanks. |
step1: if line 45 is "m_batteryLevelCharacteristic->addDescriptor(new BLE2902());", you can find that the battery level change once connect, but don't change after reconnect. step2: ”//“ line 45, and reconnect to your PC(don't need to remove from the list and rescan), you can find that it can change. step3: if you delete the connection and rescan/connect, you can find that the battery icon disappear. step4: you return the step1. |
whether you have reproduced my phenomenon? |
@SuGlider |
OK... I just got it to work... which is actually weird. I just compiled and uploaded the code... I can see the INPUT device name ESP32 Keyboard, connected and its battery changes from 0, 25, 50, 75, 100% with the battery icon changing as well (empty to full). When it is 0%, a little window shows up in the bottom right corner saying that the keyboard has no battery. In other words... it works and I just can't reproduce the issue. Important detail:If I reset the ESP32 board, it says it has reconnected, but the Battery indicator stops updating. This is the final sketch (few changes to the one you sent me): #include "BleKeyboard.h"
BleKeyboard bleKeyboard;
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
bleKeyboard.begin();
}
void loop() {
if (bleKeyboard.isConnected()) {
Serial.println("Setting Level to 0%");
bleKeyboard.setBatteryLevel(0);
delay(2000);
Serial.println("Setting Level to 25%");
bleKeyboard.setBatteryLevel(25);
delay(2000);
Serial.println("Setting Level to 50%");
bleKeyboard.setBatteryLevel(50);
delay(2000);
Serial.println("Setting Level to 75%");
bleKeyboard.setBatteryLevel(75);
delay(2000);
Serial.println("Setting Level to 100%");
bleKeyboard.setBatteryLevel(100);
delay(2000);
} else {
Serial.println("Not connected yet...");
}
Serial.println("Pause of 5 seconds...");
delay(5000);
}
|
In summary:It seems it is necessary to remove the ESP32 Keyboard from the list of paired devices and then scan/add it again in order to display and to update the Battery Indicator. Not sure if this is an ESP32 issue or a Windows issue... |
Yes, I want to say is that need to scan/add it again in order to display and to update the Battery Indicator. Only reset ESP32,the Battery Indicator won't update. |
BUT, this time, you can try to do the step2: ”//“ line 45 in BLEDevice.cpp, and reconnect to your PC, you can find that it can update. |
I'm sure it is the problem in ESP32. Because my NRF52840 works well. Although I haven't read its library. |
OK. I'll work in this issue and try to find out what is going on. |
I think that the issue may be in the BLE bonding process. If this is the case... it may take a big effort and time. |
BLE bonding has to do with reconnection and running BLE like if it had never disconnected before, in a seamless way. Changing or commenting out Line 45 doesn't seem to be the way to solve it definitively. |
Yes, I know it is not a right way. Now, my project use this feature to recognize if this is a new connect and comment or not line 45 to solve this problem for my product users. |
this is ble_bas.c in NRF sdk. /**
/
#define NRF_LOG_MODULE_NAME ble_bas #define INVALID_BATTERY_LEVEL 255 /**@brief Function for handling the Write event.
void ble_bas_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
} /**@brief Function for adding the Battery Level characteristic.
ret_code_t ble_bas_init(ble_bas_t * p_bas, const ble_bas_init_t * p_bas_init)
} /**@brief Function for sending notifications with the Battery Level characteristic.
ret_code_t ble_bas_battery_level_update(ble_bas_t * p_bas,
} ret_code_t ble_bas_battery_lvl_on_reconnection_update(ble_bas_t * p_bas,
} #endif // NRF_MODULE_ENABLED(BLE_BAS) |
BLE_UUID_REPORT_REF_DESCR is 0x2908 |
Thanks a lot! |
Board
ESP32 DevModule
Device Description
a ble keyboard use ESP32 DevModule
Hardware Configuration
BLE Server
Version
v2.0.2
IDE Name
arduino ide
Operating System
win10
Flash frequency
40MHz
PSRAM enabled
no
Upload speed
921600
Description
BLEHIDDevice hid;
hid->setBatteryLevel(level);
esp32实现的蓝牙键盘(server 模式),用这种方法进行电量设置,在win10系统下显示键盘电量。
在初次连接时,电量显示正常,电量也可变化。但是断开连接后重新连接,电量始终显示为初次连接时的电量,且更改变量赋值后,win10系统显示变量仍然无变化。
I want to use this way to make my BLE keyboard change battery level in win10. but it is invalid.
When I connect my keyboard first time, it is work, meanwhile, win10 can show my real-time battery level.
but after disconnect and reconnect, win10 can only show its last battery level, and can't show my realt-ime battery level, when I change my battery level variable.
Sketch
BLEHIDDevice hid; hid->setBatteryLevel(level);
I use this library https://github.com/T-vK/ESP32-BLE-Keyboard, it is also mentioned by T-vK/ESP32-BLE-Keyboard#99
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: