Skip to content

Commit 78e04bf

Browse files
committed
Experiment with BLE
- start aggressively with ble advertisement, but reduce after one minute - reduce advertisement payload to ensure important info is sent. not clear if this is a ESP limitation or if I do something wrong. - stop advertising after being connected and restart on disconnect.
1 parent a8718b6 commit 78e04bf

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

src/OpenBikeSensorFirmware.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static BluetoothManager* bluetoothManager;
6262

6363
Gps gps;
6464

65-
static const long BLUETOOTH_INTERVAL_MILLIS = 150;
65+
static const long BLUETOOTH_INTERVAL_MILLIS = 50;
6666
static long lastBluetoothInterval = 0;
6767

6868
static const long DISPLAY_INTERVAL_MILLIS = 20;

src/bluetooth/BatteryService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void BatteryService::setup(BLEServer *pServer) {
3535
}
3636

3737
bool BatteryService::shouldAdvertise() {
38-
return true;
38+
return false;
3939
}
4040

4141
BLEService* BatteryService::getService() {

src/bluetooth/BluetoothManager.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include "BluetoothManager.h"
2525

26+
const uint32_t BluetoothManager::HIGH_ADVERTISEMENT_TIME_MS = 60 * 1000;
27+
2628
void BluetoothManager::init(
2729
const String &obsName,
2830
const uint16_t leftOffset, const uint16_t rightOffset,
@@ -49,16 +51,29 @@ void BluetoothManager::init(
4951
for (auto &service : services) {
5052
service->getService()->start();
5153
}
54+
lastDisconnected = millis();
5255
}
5356

54-
void BluetoothManager::activateBluetooth() const {
57+
void BluetoothManager::activateBluetooth() {
5558
auto adv = pServer->getAdvertising();
59+
60+
// only the heard rate service && OBS service use advertisement
61+
// if I add one more service the 0x12 info is not sent.
5662
for (auto &service : services) {
5763
if (service->shouldAdvertise()) {
5864
adv->addServiceUUID(service->getService()->getUUID());
5965
}
6066
}
61-
adv->setMinPreferred(0x0);
67+
setFastAdvertising();
68+
69+
// Save some bytes in the advertising payload
70+
// https://specificationrefs.bluetooth.com/assigned-values/Appearance%20Values.pdf
71+
// adv->setAppearance(1152); // Generic: Cycling
72+
73+
// causes 0x12 Slave Connection Interval Range to be sent
74+
adv->setMinPreferred(0x06); // Apple?
75+
adv->setMaxPreferred(0x12);
76+
6277
BLEDevice::startAdvertising();
6378
}
6479

@@ -75,16 +90,8 @@ void BluetoothManager::newSensorValues(const uint32_t millis, const uint16_t lef
7590
for (auto &service : services) {
7691
service->newSensorValues(millis, leftValues, rightValues);
7792
}
78-
}
79-
// disconnecting
80-
if (!deviceConnected && oldDeviceConnected) {
81-
delay(500); // give the bluetooth stack the chance to get things ready
82-
pServer->startAdvertising(); // restart advertising
83-
oldDeviceConnected = deviceConnected;
84-
}
85-
// connecting
86-
if (deviceConnected && !oldDeviceConnected) {
87-
oldDeviceConnected = deviceConnected;
93+
} else if (lastDisconnected + HIGH_ADVERTISEMENT_TIME_MS < millis) {
94+
setSlowAdvertising();
8895
}
8996
}
9097

@@ -104,10 +111,31 @@ bool BluetoothManager::hasConnectedClients() {
104111
void BluetoothManager::onConnect(BLEServer* pServer) {
105112
log_i("BTLE connected!");
106113
deviceConnected = true;
107-
BLEDevice::startAdvertising();
108114
};
109115

110116
void BluetoothManager::onDisconnect(BLEServer* pServer) {
111117
log_i("BTLE disconnected!");
118+
lastDisconnected = millis();
112119
deviceConnected = false;
120+
setFastAdvertising();
121+
pServer->startAdvertising();
122+
}
123+
124+
void BluetoothManager::setFastAdvertising() {
125+
auto adv = pServer->getAdvertising();
126+
adv->setMinInterval(48 /* 0.625 msec = 30ms */);
127+
adv->setMaxInterval(80 /* 0.625 msec = 50ms */);
128+
fastAdvertising = true;
129+
}
130+
131+
void BluetoothManager::setSlowAdvertising() {
132+
if (fastAdvertising) {
133+
log_i("Decreasing BTLE advertisement interval.");
134+
auto adv = pServer->getAdvertising();
135+
adv->stop();
136+
adv->setMinInterval(800 /* 0.625 msec = 500ms */);
137+
adv->setMaxInterval(1280 /* 0.625 msec = 800ms */);
138+
adv->start();
139+
fastAdvertising = false;
140+
}
113141
}

src/bluetooth/BluetoothManager.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BluetoothManager: public BLEServerCallbacks {
4949
* Starts advertising all services that internally implement shouldAdvertise()
5050
* with `true`. The bluetooth server needs to be started before this method.
5151
*/
52-
void activateBluetooth() const;
52+
void activateBluetooth();
5353

5454
/**
5555
* Stops advertising the bluetooth services. The bluetooth server will not be
@@ -86,8 +86,12 @@ class BluetoothManager: public BLEServerCallbacks {
8686
std::list<IBluetoothService*> services;
8787
void onDisconnect(BLEServer *pServer) override;
8888
void onConnect(BLEServer *pServer) override;
89+
void setFastAdvertising();
90+
void setSlowAdvertising();
8991
bool deviceConnected;
90-
bool oldDeviceConnected;
92+
bool fastAdvertising;
93+
uint32_t lastDisconnected;
94+
static const uint32_t HIGH_ADVERTISEMENT_TIME_MS;
9195

9296
};
9397

src/bluetooth/ObsService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void ObsService::setup(BLEServer *pServer) {
5757
}
5858

5959
bool ObsService::shouldAdvertise() {
60-
return false;
60+
return true;
6161
}
6262

6363
BLEService* ObsService::getService() {

0 commit comments

Comments
 (0)