BLE - added battery service and battery level characteristic

This commit is contained in:
chrismcna
2020-08-22 11:06:14 +01:00
parent 88e25bb80c
commit e4d56b7ce9
3 changed files with 38 additions and 0 deletions

View File

@@ -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();
}

View File

@@ -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

View File

@@ -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() );
}
}
}