diff --git a/src/hardware/blectl.cpp b/src/hardware/blectl.cpp index 1ba7748..6691b24 100644 --- a/src/hardware/blectl.cpp +++ b/src/hardware/blectl.cpp @@ -51,6 +51,8 @@ BLECharacteristic *pTxCharacteristic; BLECharacteristic *pRxCharacteristic; uint8_t txValue = 0; +BLECharacteristic *pBatteryServiceBatteryLevelCharacteristic; + char *gadgetbridge_msg = NULL; uint32_t gadgetbridge_msg_size = 0; @@ -261,6 +263,22 @@ void blectl_setup( void ) { // Start advertising pServer->getAdvertising()->addServiceUUID( pService->getUUID() ); + + // Create battery service + BLEService *pBatteryService = pServer->createService(BATTERY_SERVICE_UUID); + + // Create a BLE battery service, batttery level Characteristic - + pBatteryServiceBatteryLevelCharacteristic = pBatteryService->createCharacteristic( BATTERY_SERVICE_BATTERY_LEVEL_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY ); + pBatteryServiceBatteryLevelCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED); + pBatteryServiceBatteryLevelCharacteristic->addDescriptor( new BLEDescriptor(BATTERY_SERVICE_BATTERY_LEVEL_DESCRIPTOR_UUID) ); + pBatteryServiceBatteryLevelCharacteristic->addDescriptor( new BLE2902() ); + + // Start battery service + pBatteryService->start(); + + // Start advertising battery service + pServer->getAdvertising()->addServiceUUID( pBatteryService->getUUID() ); + // Slow advertising interval for battery life // The maximum 0x4000 interval of ~16 sec was too slow, I could not reliably connect pServer->getAdvertising()->setMinInterval( 100 ); @@ -433,4 +451,15 @@ void blectl_read_config( void ) { } file.close(); } +} + + + +void blectl_update_battery( int32_t percent, bool charging, bool plug ) +{ + uint8_t level = (uint8_t)percent; + if (level > 100) level = 100; + + pBatteryServiceBatteryLevelCharacteristic->setValue(&level, 1); + pBatteryServiceBatteryLevelCharacteristic->notify(); } \ No newline at end of file diff --git a/src/hardware/blectl.h b/src/hardware/blectl.h index 82d5fae..173450d 100644 --- a/src/hardware/blectl.h +++ b/src/hardware/blectl.h @@ -30,6 +30,10 @@ #define CHARACTERISTIC_UUID_RX BLEUUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E") #define CHARACTERISTIC_UUID_TX BLEUUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E") + #define BATTERY_SERVICE_UUID BLEUUID((uint16_t)0x180F) // Battery service UUID + #define BATTERY_SERVICE_BATTERY_LEVEL_CHARACTERISTIC_UUID BLEUUID((uint16_t)0x2A19) // Battery service - battery level characteristic UUID + #define BATTERY_SERVICE_BATTERY_LEVEL_DESCRIPTOR_UUID BLEUUID((uint16_t)0x2901) // Battery service - battery level descriptor UUID + #define BLECTL_JSON_COFIG_FILE "/blectl.json" #define EndofText 0x03 @@ -91,4 +95,6 @@ void blectl_save_config( void ); void blectl_read_config( void ); + void blectl_update_battery( int32_t percent, bool charging, bool plug ); + #endif // _BLECTL_H \ No newline at end of file diff --git a/src/hardware/pmu.cpp b/src/hardware/pmu.cpp index bce193e..baf7fad 100644 --- a/src/hardware/pmu.cpp +++ b/src/hardware/pmu.cpp @@ -7,6 +7,8 @@ #include "pmu.h" #include "powermgm.h" #include "motor.h" +#include "blectl.h" + #include "gui/statusbar.h" @@ -283,6 +285,7 @@ void pmu_loop( TTGOClass *ttgo ) { if ( nextmillis < millis() || updatetrigger == true ) { nextmillis = millis() + 1000; statusbar_update_battery( pmu_get_battery_percent( ttgo ), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() ); + blectl_update_battery( pmu_get_battery_percent( ttgo ), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() ); } } }