Skip to content

Commit 22571d4

Browse files
committed
Advertise fast for at least 30 secs then slow down
On power up, advertise aggressively for at least 30 seconds then switch to a longer interval to conserve battery life. This fast/slow pattern is designed to balance connection response time and battery life. When a disconnect event is received restart the fast/slow pattern. When a failed connect event is received, restart the fast/slow pattern. When the screen is activated and ble is not connected, restart the fast/slow pattern. This pattern is consistent with Apple's BLE developer standards (QA 1931).
1 parent 4820b2f commit 22571d4

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/components/ble/NimbleController.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ void NimbleController::StartAdvertising() {
122122

123123
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
124124
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
125+
/* fast advertise for 30 sec */
126+
if (fastAdvCount < 15) {
127+
adv_params.itvl_min = 32;
128+
adv_params.itvl_max = 47;
129+
fastAdvCount++;
130+
} else {
131+
adv_params.itvl_min = 1636;
132+
adv_params.itvl_max = 1651;
133+
}
125134

126135
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
127136
fields.uuids128 = &dfuServiceUuid;
@@ -139,7 +148,7 @@ void NimbleController::StartAdvertising() {
139148
rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
140149
ASSERT(rc == 0);
141150

142-
rc = ble_gap_adv_start(addrType, NULL, 5000, &adv_params, GAPEventCallback, this);
151+
rc = ble_gap_adv_start(addrType, NULL, 2000, &adv_params, GAPEventCallback, this);
143152
ASSERT(rc == 0);
144153
}
145154

@@ -163,6 +172,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
163172
alertNotificationClient.Reset();
164173
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
165174
bleController.Disconnect();
175+
fastAdvCount = 0;
166176
StartAdvertising();
167177
} else {
168178
connectionHandle = event->connect.conn_handle;
@@ -181,6 +191,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
181191
alertNotificationClient.Reset();
182192
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
183193
bleController.Disconnect();
194+
fastAdvCount = 0;
184195
StartAdvertising();
185196
break;
186197

src/components/ble/NimbleController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ namespace Pinetime {
7272
uint16_t connHandle();
7373
void NotifyBatteryLevel(uint8_t level);
7474

75+
void RestartFastAdv() {
76+
fastAdvCount = 0;
77+
}
78+
7579
private:
7680
static constexpr const char* deviceName = "InfiniTime";
7781
Pinetime::System::SystemTask& systemTask;
@@ -94,6 +98,7 @@ namespace Pinetime {
9498

9599
uint8_t addrType; // 1 = Random, 0 = PUBLIC
96100
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;
101+
uint8_t fastAdvCount = 0;
97102

98103
ble_uuid128_t dfuServiceUuid {
99104
.u {.type = BLE_UUID_TYPE_128},

src/systemtask/SystemTask.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ void SystemTask::Work() {
233233
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
234234
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
235235

236+
if (!bleController.IsConnected())
237+
nimbleController.RestartFastAdv();
238+
236239
isSleeping = false;
237240
isWakingUp = false;
238241
isDimmed = false;

0 commit comments

Comments
 (0)