slightly change advertising implementation
This commit is contained in:
1 parent
7117a3030f
commit
8034f79236
3 files changed
+37
-31
No files matched your search
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
- Separated `BLEAdvertisingData` and `BLEAdvertising`
|
||||
- Added `setStopCallback()` support to declare a callback when advertising stops
|
||||
- Add the option to advertise for a specific time. There are multiple timeouts: initial fast advertising mode, slow advertising mode, and an optional delay to stop advertising entirely. Values can be set in multiple of timeout ticks (0.625ms per unit) or in ms (approximate since it gets converted to 0.625ms units). The optional timeout to stop advertising entire is set via the `.stop(timeout)` parameter.
|
||||
- Add the option to advertise for a specific time. There are multiple timeouts: initial fast advertising mode, slow advertising mode, and an optional delay to stop advertising entirely. Values can be set in multiple of timeout ticks (0.625ms per unit) or in ms (approximate since it gets converted to 0.625ms units). The optional timeout to stop advertising entire is set via the `.start(timeout)` parameter.
|
||||
- Default timeout to slow mode = 30 seconds. Default adv intervals are fast mode = 20 ms, slow = 152.5 ms
|
||||
|
||||
### BLEGAP (To support multiple connections)
|
||||
|
||||
@@ -37,11 +37,6 @@
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
#define BLE_ADV_INTERVAL_FAST_DFLT 32 // 20 ms (in 0.625 ms unit)
|
||||
#define BLE_ADV_INTERVAL_SLOW_DFLT 244 // 152.5 ms (in 0.625 ms unit)
|
||||
|
||||
#define BLE_ADV_TIMEOUT_DFLT 30 // in seconds
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* BLEAdvertisingData shared between ADV and ScanResponse
|
||||
*------------------------------------------------------------------*/
|
||||
@@ -178,20 +173,19 @@ void BLEAdvertisingData::clearData(void)
|
||||
*------------------------------------------------------------------*/
|
||||
BLEAdvertising::BLEAdvertising(void)
|
||||
{
|
||||
_timeout_sec = BLE_ADV_TIMEOUT_DFLT;
|
||||
_type = BLE_GAP_ADV_TYPE_ADV_IND;
|
||||
|
||||
_fast_interval = BLE_ADV_INTERVAL_FAST_DFLT;
|
||||
_slow_interval = BLE_ADV_INTERVAL_SLOW_DFLT;
|
||||
|
||||
_started_sec = 0;
|
||||
_stop_sec = 0;
|
||||
_fast_timeout = BLE_ADV_FAST_TIMEOUT_DFLT;
|
||||
_stop_timeout = 0;
|
||||
_stop_cb = NULL;
|
||||
}
|
||||
|
||||
void BLEAdvertising::setTimeout(uint16_t sec)
|
||||
void BLEAdvertising::setFastTimeout(uint16_t sec)
|
||||
{
|
||||
_timeout_sec = sec;
|
||||
_fast_timeout = sec;
|
||||
}
|
||||
|
||||
void BLEAdvertising::setType(uint8_t adv_type)
|
||||
@@ -221,7 +215,7 @@ bool BLEAdvertising::setBeacon(BLEBeacon& beacon)
|
||||
return beacon.start(*this);
|
||||
}
|
||||
|
||||
bool BLEAdvertising::_start(uint16_t interval)
|
||||
bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
|
||||
{
|
||||
// ADV Params
|
||||
ble_gap_adv_params_t adv_para =
|
||||
@@ -231,7 +225,7 @@ bool BLEAdvertising::_start(uint16_t interval)
|
||||
.fp = BLE_GAP_ADV_FP_ANY ,
|
||||
.p_whitelist = NULL ,
|
||||
.interval = interval , // advertising interval (in units of 0.625 ms)
|
||||
.timeout = _timeout_sec
|
||||
.timeout = timeout ,
|
||||
};
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para), false );
|
||||
@@ -244,9 +238,9 @@ bool BLEAdvertising::start(uint32_t stop_sec)
|
||||
{
|
||||
// Configure Data
|
||||
VERIFY_STATUS( sd_ble_gap_adv_data_set(_data, _count, Bluefruit.ScanResponse.getData(), Bluefruit.ScanResponse.count()), false );
|
||||
VERIFY( _start(_fast_interval) );
|
||||
|
||||
_started_sec = 0; // reset established time
|
||||
// Initially advertising with fast interval
|
||||
VERIFY( _start(_fast_interval, _fast_timeout) );
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -289,18 +283,27 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
|
||||
{
|
||||
_started_sec += _timeout_sec;
|
||||
|
||||
// Continue to adv if stop is not configured or time not reach it yet
|
||||
if ( (_stop_sec == 0) || (_started_sec < _stop_sec) )
|
||||
if ( _stop_timeout == 0 )
|
||||
{
|
||||
_start(_slow_interval);
|
||||
// if stop_timeout is 0 --> no timeout
|
||||
_start(_slow_interval, 0);
|
||||
}else
|
||||
{
|
||||
Bluefruit._stopConnLed(); // stop blinking
|
||||
// Advertising if there is still time left, otherwise stop it
|
||||
if ( _stop_timeout > _fast_timeout )
|
||||
{
|
||||
_start(_slow_interval, _stop_timeout - _fast_timeout);
|
||||
|
||||
// invoke stop callback
|
||||
if (_stop_cb) ada_callback(NULL, _stop_cb);
|
||||
// this will cause the next timeout event (this case code) stop advertising
|
||||
_stop_timeout = _fast_timeout;
|
||||
}else
|
||||
{
|
||||
// Stop advertising
|
||||
Bluefruit._stopConnLed(); // stop blinking
|
||||
|
||||
// invoke stop callback
|
||||
if (_stop_cb) ada_callback(NULL, _stop_cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -44,6 +44,10 @@
|
||||
#include "BLEService.h"
|
||||
#include "services/BLEBeacon.h"
|
||||
|
||||
#define BLE_ADV_INTERVAL_FAST_DFLT 32 // 20 ms (in 0.625 ms unit)
|
||||
#define BLE_ADV_INTERVAL_SLOW_DFLT 244 // 152.5 ms (in 0.625 ms unit)
|
||||
#define BLE_ADV_FAST_TIMEOUT_DFLT 30 // in seconds
|
||||
|
||||
class BLEAdvertisingData
|
||||
{
|
||||
protected:
|
||||
@@ -79,10 +83,10 @@ public:
|
||||
BLEAdvertising(void);
|
||||
|
||||
void setType(uint8_t adv_type);
|
||||
void setTimeout(uint16_t sec);
|
||||
void setFastTimeout(uint16_t sec);
|
||||
void setStopCallback(stop_callback_t fp);
|
||||
|
||||
void setInterval(uint16_t fast, uint16_t slow);
|
||||
void setInterval (uint16_t fast, uint16_t slow);
|
||||
void setIntervalMS(uint16_t fast, uint16_t slow);
|
||||
|
||||
bool setBeacon(BLEBeacon& beacon);
|
||||
@@ -98,19 +102,18 @@ public:
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
private:
|
||||
uint16_t _timeout_sec;
|
||||
uint8_t _type;
|
||||
|
||||
uint16_t _fast_interval;
|
||||
uint16_t _slow_interval;
|
||||
uint16_t _fast_interval; // in 0.625 ms
|
||||
uint16_t _slow_interval; // in 0.625 ms
|
||||
|
||||
uint16_t _started_sec;
|
||||
uint16_t _stop_sec;
|
||||
uint16_t _fast_timeout; // in second
|
||||
uint16_t _stop_timeout; // in second
|
||||
|
||||
stop_callback_t _stop_cb;
|
||||
|
||||
// Internal function
|
||||
bool _start(uint16_t interval);
|
||||
bool _start(uint16_t interval, uint16_t timeout);
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user