diff --git a/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino b/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino index 47f3e627..7a0b5325 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino @@ -47,8 +47,8 @@ void setup() Bluefruit.Central.setDisconnectCallback(disconnect_callback); // Start Central Scan - Bluefruit.Central.setScanCallback(scan_callback); - Bluefruit.Central.startScanning(); + Bluefruit.Scanner.setRxCallback(scan_callback); + Bluefruit.Scanner.start(0); } /** diff --git a/libraries/Bluefruit52Lib/examples/Central/central_scan/central_scan.ino b/libraries/Bluefruit52Lib/examples/Central/central_scan/central_scan.ino index 3ec3d66d..386c5078 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_scan/central_scan.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_scan/central_scan.ino @@ -26,8 +26,8 @@ void setup() // Start Central Scan Bluefruit.setConnLedInterval(250); - Bluefruit.Central.setScanCallback(scan_callback); - Bluefruit.Central.startScanning(); + Bluefruit.Scanner.setRxCallback(scan_callback); + Bluefruit.Scanner.start(0); Serial.println("Scanning ..."); } diff --git a/libraries/Bluefruit52Lib/examples/Central/multiple_bleuart/multiple_bleuart.ino b/libraries/Bluefruit52Lib/examples/Central/multiple_bleuart/multiple_bleuart.ino index a2346e95..88dfc3c4 100644 --- a/libraries/Bluefruit52Lib/examples/Central/multiple_bleuart/multiple_bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Central/multiple_bleuart/multiple_bleuart.ino @@ -79,8 +79,8 @@ void setup() Bluefruit.Central.setDisconnectCallback(disconnect_callback); // Start Central Scan - Bluefruit.Central.setScanCallback(scan_callback); - Bluefruit.Central.startScanning(); + Bluefruit.Scanner.setRxCallback(scan_callback); + Bluefruit.Scanner.start(0); } /** diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index 5152f69d..4dfe6453 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -42,15 +42,6 @@ */ BLECentral::BLECentral(void) { - _scan_cb = NULL; - _scan_param = (ble_gap_scan_params_t) { - .active = 1, - .selective = 0, - .p_whitelist = NULL, - .interval = 0x00A0, - .window = 0x0050, - .timeout = 0, // no timeout - }; } void BLECentral::begin(void) @@ -62,26 +53,6 @@ void BLECentral::begin(void) /*------------------------------------------------------------------*/ /* Scan and Parser *------------------------------------------------------------------*/ -void BLECentral::setScanCallback(scan_callback_t fp) -{ - _scan_cb = fp; -} - -bool BLECentral::startScanning(uint16_t timeout) -{ - _scan_param.timeout = timeout; - VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param), false ); - Bluefruit._startConnLed(); // start blinking - return ERROR_NONE; -} - -bool BLECentral::stopScanning(void) -{ - Bluefruit._stopConnLed(); // stop blinking - VERIFY_STATUS( sd_ble_gap_scan_stop(), false ); - return true; -} - uint8_t* BLECentral::extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len) { *result_len = 0; @@ -168,7 +139,7 @@ bool BLECentral::connect(const ble_gap_addr_t* peer_addr, uint16_t min_conn_inte .conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit }; - VERIFY_STATUS( sd_ble_gap_connect(peer_addr, &_scan_param, &gap_conn_params), false ); + VERIFY_STATUS( sd_ble_gap_connect(peer_addr, Bluefruit.Scanner.getParams(), &gap_conn_params), false ); return true; } @@ -255,7 +226,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) // Note callback is invoked by BLEGap Bluefruit._setConnLed(false); _conn_hdl = BLE_CONN_HANDLE_INVALID; - startScanning(); + Bluefruit.Scanner.start(0); break; case BLE_GAP_EVT_TIMEOUT: @@ -263,7 +234,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) { // TODO Advance Scanning // Restart Scanning - startScanning(); + Bluefruit.Scanner.start(0); } break; diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index 51ee621f..8f3b2384 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -63,10 +63,6 @@ class BLECentral /*------------------------------------------------------------------*/ /* Scan & Parser *------------------------------------------------------------------*/ - void setScanCallback(scan_callback_t fp); - bool startScanning(uint16_t timeout = 0); - bool stopScanning(void); - uint8_t* extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len); uint8_t* extractScanData(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len); bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, BLEUuid ble_uuid); @@ -96,11 +92,7 @@ class BLECentral private: uint16_t _conn_hdl; - ble_gap_scan_params_t _scan_param; - scan_callback_t _scan_cb; - bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len); - void _event_handler(ble_evt_t* evt); friend class AdafruitBluefruit; diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.cpp b/libraries/Bluefruit52Lib/src/BLEScanner.cpp new file mode 100644 index 00000000..6c977508 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEScanner.cpp @@ -0,0 +1,74 @@ +/**************************************************************************/ +/*! + @file BLEScanner.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "bluefruit.h" + +BLEScanner::BLEScanner(void) +{ + _rx_cb = NULL; + + _scan_param = (ble_gap_scan_params_t) { + .active = 1, + .selective = 0, + .p_whitelist = NULL, + .interval = 0x00A0, + .window = 0x0050, + .timeout = 0, // no timeout + }; +} + +void BLEScanner::setRxCallback(rx_callback_t fp) +{ + _rx_cb = fp; +} + +ble_gap_scan_params_t* BLEScanner::getParams(void) +{ + return &_scan_param; +} + +bool BLEScanner::start(uint16_t timeout) +{ + _scan_param.timeout = timeout; + VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param), false ); + Bluefruit._startConnLed(); // start blinking +} + +bool BLEScanner::stop(void) +{ + VERIFY_STATUS( sd_ble_gap_scan_stop(), false ); + Bluefruit._stopConnLed(); // stop blinking +} diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.h b/libraries/Bluefruit52Lib/src/BLEScanner.h new file mode 100644 index 00000000..af8471bc --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEScanner.h @@ -0,0 +1,64 @@ +/**************************************************************************/ +/*! + @file BLEScanner.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef BLESCANNER_H_ +#define BLESCANNER_H_ + +#include +#include "bluefruit_common.h" + +class BLEScanner +{ +public: + typedef void (*rx_callback_t) (ble_gap_evt_adv_report_t*); + + BLEScanner(void); + + void setRxCallback(rx_callback_t fp); + + ble_gap_scan_params_t* getParams(void); + + bool start(uint16_t timeout = 0); + bool stop(void); + +private: + + rx_callback_t _rx_cb; + ble_gap_scan_params_t _scan_param; +}; + + + +#endif /* BLESCANNER_H_ */ diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index ff0591eb..865de07f 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -54,6 +54,7 @@ #include "BLECharacteristic.h" #include "BLEService.h" +#include "BLEScanner.h" #include "BLECentral.h" #include "BLEClientCharacteristic.h" #include "BLEClientService.h" @@ -95,6 +96,7 @@ class AdafruitBluefruit *------------------------------------------------------------------*/ BLEAdvertising Advertising; BLEAdvertisingData ScanResponse; + BLEScanner Scanner; BLECentral Central; BLEDiscovery Discovery;