Add BLEScanner
This commit is contained in:
1 parent
e5d771e942
commit
f770816f01
8 files changed
+149
-46
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 ...");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 <Arduino.h>
|
||||
#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_ */
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in new issue
Block a user