diff --git a/changelog.md b/changelog.md index 3d191b7b..cf9657b6 100644 --- a/changelog.md +++ b/changelog.md @@ -10,7 +10,11 @@ - Upgrade Bluetooth 5 with Softdevice to S132 v5.0.0 - Support max ATT MTU up to 247 (negotiable with peer) -- Added BLEAdvertising multiple services aaddition and addManufacturerData() API +- BLEAdvertising + - Added multiple services aaddition API + - Added addManufacturerData() + - Added getInterval() to retrieve current active interval + - Added Slow interval callback support via setSlowCallback() - Fixed an discovery bug introduced in 0.7.5 ## 0.7.5 diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino b/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino index a1414b50..684457ed 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino @@ -44,21 +44,41 @@ void startAdv(void) * - Enable auto advertising if disconnected * - Interval: fast mode = 20 ms, slow mode = 152.5 ms * - Timeout for fast mode is 30 seconds - * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * - Start(timeout) with timeout = 30 * * For recommended advertising interval * https://developer.apple.com/library/content/qa/qa1931/_index.html */ Bluefruit.Advertising.restartOnDisconnect(true); - Bluefruit.Advertising.setInterval(32, 32); // in unit of 0.625 ms + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.setStopCallback(adv_stop_callback); // callback when adv is stopped - // Homekit include advertising interval as part of its data - // Therefor setData(homekit) must be after setInterval() + /* Homekit include advertising interval as part of its adv data + * - Therefore setInterval() must be call before setData(homekit). + * - Furthermore since Adverting Data (for homekit) changes when adv interval change + * from fast to slow. We do either way to stay consistence + * 1. Advertise with fixed interval (fast = slow) + * 2. Register stop callback for adv stop, then re-set the Advertising data to match + * the changed interval. Which is what this sketch does. + */ Bluefruit.Advertising.setData(homekit); - - Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds + // Stop after 30 secconds + Bluefruit.Advertising.start(30); +} + +void adv_stop_callback(void) +{ + // change interval to only slow since we alredy passed first 30 seconds + Bluefruit.Advertising.setInterval(244, 244); + + // Reset advertising data for homekit. + Bluefruit.Advertising.clearData(); + Bluefruit.Advertising.setData(homekit); + + // Advertising indefintely. + Bluefruit.Advertising.start(0); } void loop() diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp index 5fefcf30..e093e0e4 100644 --- a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp @@ -259,6 +259,7 @@ BLEAdvertising::BLEAdvertising(void) _fast_timeout = BLE_ADV_FAST_TIMEOUT_DFLT; _stop_timeout = _left_timeout = 0; _stop_cb = NULL; + _slow_cb = NULL; } void BLEAdvertising::setFastTimeout(uint16_t sec) @@ -299,6 +300,11 @@ uint16_t BLEAdvertising::getInterval(void) return _active_interval; } +void BLEAdvertising::setSlowCallback(slow_callback_t fp) +{ + _slow_cb = fp; +} + void BLEAdvertising::setStopCallback(stop_callback_t fp) { _stop_cb = fp; @@ -420,6 +426,9 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt) if ( _stop_timeout == 0 ) { + // Call slow callback if available + if (_slow_cb) _slow_cb(); + // if stop_timeout is 0 --> no timeout _start(_slow_interval, 0); }else @@ -427,6 +436,9 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt) // Advertising if there is still time left, otherwise stop it if ( _left_timeout ) { + // Call slow callback if available + if (_slow_cb) _slow_cb(); + _start(_slow_interval, _left_timeout); }else { diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.h b/libraries/Bluefruit52Lib/src/BLEAdvertising.h index 14c2ac08..dce4a889 100644 --- a/libraries/Bluefruit52Lib/src/BLEAdvertising.h +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.h @@ -116,10 +116,14 @@ class BLEAdvertising : public BLEAdvertisingData { public: typedef void (*stop_callback_t) (void); + typedef void (*slow_callback_t) (void); + BLEAdvertising(void); void setType(uint8_t adv_type); void setFastTimeout(uint16_t sec); + + void setSlowCallback(slow_callback_t fp); void setStopCallback(stop_callback_t fp); void setInterval (uint16_t fast, uint16_t slow); @@ -157,6 +161,7 @@ private: uint16_t _left_timeout; stop_callback_t _stop_cb; + slow_callback_t _slow_cb; // Internal function bool _start(uint16_t interval, uint16_t timeout);