Skip to content

Commit f49c62b

Browse files
committed
BLE: Only send notify events if requested by client
1 parent 1608ae7 commit f49c62b

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

src/bluetooth/DistanceService.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
void DistanceService::setup(BLEServer *pServer) {
2727
mService = pServer->createService(SERVICE_DISTANCE_UUID);
2828
mCharacteristic = mService->createCharacteristic(SERVICE_DISTANCE_CHAR_50MS_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
29+
// SimRa does not like BLE2902
2930
}
3031

3132
bool DistanceService::shouldAdvertise() {

src/bluetooth/HeartRateService.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const BLEUUID HeartRateService::SERVICE_UUID = BLEUUID((uint16_t)ESP_GATT_UUID_H
3030
void HeartRateService::setup(BLEServer *pServer) {
3131
mService = pServer->createService(SERVICE_UUID); // Keep the defaults!!, 5);
3232
mService->addCharacteristic(&mHeartRateMeasurementCharacteristics);
33-
mHeartRateMeasurementCharacteristics.addDescriptor(new BLE2902());
33+
mHeartRateMeasurementCharacteristics.addDescriptor(&mHeartRateMeasurementConfiguration);
3434
mValue[0] = mValue[1] = mValue[2] = 0;
3535
mHeartRateMeasurementCharacteristics.setValue(mValue, 2);
3636

@@ -54,12 +54,13 @@ void HeartRateService::newSensorValues(
5454
return;
5555
}
5656

57-
mValue[0] = mMinimumDistance <= UINT8_MAX ? 0 : 1; // 8/16 bit data no other flags set;
58-
mValue[1] = mMinimumDistance & 0xFFu;
59-
mValue[2] = mMinimumDistance >> 8u;
60-
61-
mHeartRateMeasurementCharacteristics.setValue(mValue, mMinimumDistance <= UINT8_MAX ? 2 : 3);
62-
mHeartRateMeasurementCharacteristics.notify();
57+
if (mHeartRateMeasurementConfiguration.getNotifications()) {
58+
mValue[0] = mMinimumDistance <= UINT8_MAX ? 0 : 1; // 8/16 bit data no other flags set;
59+
mValue[1] = mMinimumDistance & 0xFFu;
60+
mValue[2] = mMinimumDistance >> 8u;
61+
mHeartRateMeasurementCharacteristics.setValue(mValue, mMinimumDistance <= UINT8_MAX ? 2 : 3);
62+
mHeartRateMeasurementCharacteristics.notify();
63+
}
6364

6465
// Reset values
6566
mMinimumDistance = MAX_SENSOR_VALUE;

src/bluetooth/HeartRateService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class HeartRateService : public IBluetoothService {
3737
BLEService *mService = nullptr;
3838
BLECharacteristic mHeartRateMeasurementCharacteristics
3939
= BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_HEART_RATE_MEAS), BLECharacteristic::PROPERTY_NOTIFY);
40+
BLE2902 mHeartRateMeasurementConfiguration;
4041
unsigned long mCollectionStartTime = 0;
4142
uint16_t mMinimumDistance = MAX_SENSOR_VALUE;
4243
uint8_t mValue[4];

src/bluetooth/ObsService.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void ObsService::setup(BLEServer *pServer) {
4646
mTimeCharacteristic.setCallbacks(&mTimeCharacteristicsCallback);
4747

4848
mService->addCharacteristic(&mDistanceCharacteristic);
49-
mDistanceCharacteristic.addDescriptor(new BLE2902);
49+
mDistanceCharacteristic.addDescriptor(&mDistanceConfiguration);
5050

5151
mService->addCharacteristic(&mButtonCharacteristic);
5252
mButtonCharacteristic.addDescriptor(new BLE2902);
@@ -65,7 +65,9 @@ BLEService* ObsService::getService() {
6565
}
6666

6767
void ObsService::newSensorValues(uint32_t millis, uint16_t leftValue, uint16_t rightValue) {
68-
sendEventData(&mDistanceCharacteristic, millis, leftValue, rightValue);
68+
if (mDistanceConfiguration.getNotifications()) {
69+
sendEventData(&mDistanceCharacteristic, millis, leftValue, rightValue);
70+
}
6971
}
7072

7173
void ObsService::newPassEvent(uint32_t millis, uint16_t leftValue, uint16_t rightValue) {

src/bluetooth/ObsService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ObsService : public IBluetoothService {
5151

5252
BLECharacteristic mDistanceCharacteristic
5353
= BLECharacteristic(OBS_DISTANCE_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY);
54+
BLE2902 mDistanceConfiguration;
5455

5556
BLECharacteristic mButtonCharacteristic
5657
= BLECharacteristic(OBS_BUTTON_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY);

0 commit comments

Comments
 (0)