improving scanner
This commit is contained in:
1 parent
f770816f01
commit
a3aef999cc
6 files changed
+89
-16
No files matched your search
@@ -222,7 +222,7 @@ bool BLEAdvertising::setBeacon(BLEBeacon& beacon)
|
||||
return beacon.start(*this);
|
||||
}
|
||||
|
||||
bool BLEAdvertising::restartOnDisconnect(bool enable)
|
||||
void BLEAdvertising::restartOnDisconnect(bool enable)
|
||||
{
|
||||
_start_if_disconnect = enable;
|
||||
}
|
||||
|
||||
@@ -108,8 +108,8 @@ public:
|
||||
|
||||
bool isRunning(void);
|
||||
|
||||
void restartOnDisconnect(bool enable);
|
||||
bool start(uint16_t timeout = 0);
|
||||
bool restartOnDisconnect(bool enable);
|
||||
bool stop (void);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
|
||||
@@ -202,31 +202,19 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
*/
|
||||
switch ( evt->header.evt_id )
|
||||
{
|
||||
case BLE_GAP_EVT_ADV_REPORT:
|
||||
{
|
||||
// evt_conn_hdl is equal to BLE_CONN_HANDLE_INVALID
|
||||
ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report;
|
||||
if (_scan_cb) _scan_cb(adv_report);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
{ // Note callback is invoked by BLEGap
|
||||
ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;
|
||||
|
||||
if (para->role == BLE_GAP_ROLE_CENTRAL)
|
||||
{
|
||||
Bluefruit._stopConnLed();
|
||||
Bluefruit._setConnLed(true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
// Note callback is invoked by BLEGap
|
||||
Bluefruit._setConnLed(false);
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID;
|
||||
Bluefruit.Scanner.start(0);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
|
||||
@@ -38,7 +38,9 @@
|
||||
|
||||
BLEScanner::BLEScanner(void)
|
||||
{
|
||||
_rx_cb = NULL;
|
||||
_runnning = false;
|
||||
_start_if_disconnect = true;
|
||||
_rx_cb = NULL;
|
||||
|
||||
_scan_param = (ble_gap_scan_params_t) {
|
||||
.active = 1,
|
||||
@@ -50,11 +52,21 @@ BLEScanner::BLEScanner(void)
|
||||
};
|
||||
}
|
||||
|
||||
bool BLEScanner::isRunning(void)
|
||||
{
|
||||
return _runnning;
|
||||
}
|
||||
|
||||
void BLEScanner::setRxCallback(rx_callback_t fp)
|
||||
{
|
||||
_rx_cb = fp;
|
||||
}
|
||||
|
||||
void BLEScanner::restartOnDisconnect(bool enable)
|
||||
{
|
||||
_start_if_disconnect = enable;
|
||||
}
|
||||
|
||||
ble_gap_scan_params_t* BLEScanner::getParams(void)
|
||||
{
|
||||
return &_scan_param;
|
||||
@@ -64,11 +76,71 @@ bool BLEScanner::start(uint16_t timeout)
|
||||
{
|
||||
_scan_param.timeout = timeout;
|
||||
VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param), false );
|
||||
|
||||
Bluefruit._startConnLed(); // start blinking
|
||||
_runnning = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLEScanner::stop(void)
|
||||
{
|
||||
VERIFY_STATUS( sd_ble_gap_scan_stop(), false );
|
||||
|
||||
Bluefruit._stopConnLed(); // stop blinking
|
||||
_runnning = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BLEScanner::_eventHandler(ble_evt_t* evt)
|
||||
{
|
||||
switch ( evt->header.evt_id )
|
||||
{
|
||||
case BLE_GAP_EVT_ADV_REPORT:
|
||||
{
|
||||
// evt_conn_hdl is equal to BLE_CONN_HANDLE_INVALID
|
||||
ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report;
|
||||
if (_rx_cb) _rx_cb(adv_report); // TODO ada_callback
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
{
|
||||
ble_gap_evt_connected_t const * para = &evt->evt.gap_evt.params.connected;
|
||||
|
||||
if ( para->role == BLE_GAP_ROLE_CENTRAL)
|
||||
{
|
||||
_runnning = false;
|
||||
|
||||
// Turn on Conn LED
|
||||
Bluefruit._stopConnLed();
|
||||
Bluefruit._setConnLed(true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
if ( BLE_GAP_ROLE_CENTRAL == Bluefruit.Gap.getRole(evt->evt.common_evt.conn_handle) )
|
||||
{
|
||||
// Turn off Conn LED
|
||||
Bluefruit._setConnLed(false);
|
||||
|
||||
// Auto start if enabled
|
||||
if ( _start_if_disconnect )
|
||||
{
|
||||
if (!_runnning) start(_scan_param.timeout);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
|
||||
{
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -50,12 +50,24 @@ public:
|
||||
|
||||
ble_gap_scan_params_t* getParams(void);
|
||||
|
||||
bool isRunning(void);
|
||||
|
||||
void restartOnDisconnect(bool enable);
|
||||
bool start(uint16_t timeout = 0);
|
||||
bool stop(void);
|
||||
|
||||
private:
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
private:
|
||||
bool _runnning;
|
||||
bool _start_if_disconnect;
|
||||
rx_callback_t _rx_cb;
|
||||
|
||||
ble_gap_scan_params_t _scan_param;
|
||||
};
|
||||
|
||||
|
||||
@@ -460,6 +460,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// GAP handler
|
||||
Gap._eventHandler(evt);
|
||||
Advertising._eventHandler(evt);
|
||||
Scanner._eventHandler(evt);
|
||||
|
||||
/*------------- BLE Peripheral Events -------------*/
|
||||
/* Only handle Peripheral events with matched connection handle
|
||||
|
||||
Reference in new issue
Block a user