expose battery level via the standard BLE battery service

This commit is contained in:
geeksville 2020-06-22 10:04:26 -07:00
parent c5851a4a0c
commit 60470211e5
3 changed files with 15 additions and 5 deletions

View File

@ -122,7 +122,7 @@ BLEService *createBatteryService(BLEServer *server)
addWithDesc(pBattery, batteryLevelC, "Percentage 0 - 100");
batteryLevelC->addDescriptor(addBLEDescriptor(new BLE2902())); // Needed so clients can request notification
// I don't think we need to advertise this
// I don't think we need to advertise this? and some phones only see the first thing advertised anyways...
// server->getAdvertising()->addServiceUUID(pBattery->getUUID());
pBattery->start();
@ -135,8 +135,8 @@ BLEService *createBatteryService(BLEServer *server)
*/
void updateBatteryLevel(uint8_t level)
{
// Pretend to update battery levels - fixme do elsewhere
if (batteryLevelC) {
DEBUG_MSG("set BLE battery level %u\n", level);
batteryLevelC->setValue(&level, 1);
batteryLevelC->notify();
}
@ -215,7 +215,7 @@ class MySecurity : public BLESecurityCallbacks
BLEServer *pServer;
BLEService *pDevInfo, *pUpdate;
BLEService *pDevInfo, *pUpdate, *pBattery;
void deinitBLE()
{
@ -230,6 +230,9 @@ void deinitBLE()
pUpdate->executeDelete();
}
pBattery->stop();
pBattery->executeDelete();
pDevInfo->stop();
pDevInfo->executeDelete();
@ -242,6 +245,7 @@ void deinitBLE()
if (pUpdate != NULL)
delete pUpdate;
delete pDevInfo;
delete pBattery;
delete pServer;
batteryLevelC = NULL; // Don't let anyone generate bogus notifies
@ -279,8 +283,7 @@ BLEServer *initBLE(StartBluetoothPinScreenCallback startBtPinScreen, StopBluetoo
pDevInfo = createDeviceInfomationService(pServer, hwVendor, swVersion, hwVersion);
// We now let users create the battery service only if they really want (not all devices have a battery)
// BLEService *pBattery = createBatteryService(pServer);
pBattery = createBatteryService(pServer);
// #define BLE_SOFTWARE_UPDATE
#ifdef BLE_SOFTWARE_UPDATE

View File

@ -35,5 +35,8 @@ BLECharacteristic *addBLECharacteristic(BLECharacteristic *c);
/// Add a characteristic that we will delete when we restart
BLEDescriptor *addBLEDescriptor(BLEDescriptor *c);
/// Given a level between 0-100, update the BLE attribute
void updateBatteryLevel(uint8_t level);
/// Any bluetooth objects you allocate _must_ come from this pool if you want to be able to call deinitBLE()
extern SimpleAllocator btPool;

View File

@ -12,6 +12,7 @@
#include "main.h"
#include "mesh-pb-constants.h"
#include "power.h"
#include "BluetoothUtil.h" // needed for updateBatteryLevel, FIXME, eventually when we pull mesh out into a lib we shouldn't be whacking bluetooth from here
/*
receivedPacketQueue - this is a queue of messages we've received from the mesh, which we are keeping to deliver to the phone.
@ -281,6 +282,8 @@ void MeshService::sendOurPosition(NodeNum dest, bool wantReplies)
sendToMesh(p);
}
int MeshService::onGPSChanged(void *unused)
{
// DEBUG_MSG("got gps notify\n");
@ -301,6 +304,7 @@ int MeshService::onGPSChanged(void *unused)
// Include our current battery voltage in our position announcement
pos.battery_level = powerStatus.batteryChargePercent;
updateBatteryLevel(pos.battery_level);
// We limit our GPS broadcasts to a max rate
static uint32_t lastGpsSend;