correct scanner when using filter. Tested all central example

This commit is contained in:
hathach committed 2018-11-08 20:14:34 +07:00
1 parent 6062aaa5d5
commit 905bb3b671
10 files changed
+168 -69

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Adafruit_nRF52_Arduino</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
<linkedResources>
<link>
<name>arduino-libraries</name>
<type>2</type>
<location>/home/hathach/Arduino/libraries</location>
</link>
</linkedResources>
<filteredResources>
<filter>
<id>1498710934502</id>
<name></name>
<type>22</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-*.o</arguments>
</matcher>
</filter>
<filter>
<id>1498710934507</id>
<name></name>
<type>22</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-*.d</arguments>
</matcher>
</filter>
<filter>
<id>1498710934515</id>
<name></name>
<type>6</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-*.pyc</arguments>
</matcher>
</filter>
<filter>
<id>1498710934520</id>
<name></name>
<type>22</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-*.so</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
@@ -75,6 +75,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
// Connect to device with bleuart service in advertising
Bluefruit.Central.connect(report);
}else
{
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner start() to resume scanning
Bluefruit.Scanner.start();
}
}
@@ -181,4 +186,3 @@ void loop()
}
}
}
@@ -117,12 +117,14 @@ void setup()
/* Start Central Scanning
* - Enable auto scan if disconnected
* - Interval = 100 ms, window = 80 ms
* - Filter only accept bleuart service in advertising
* - Don't use active scan (used to retrieve the optional scan response adv packet)
* - Start(0) = will scan forever since no timeout is given
*/
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
Bluefruit.Scanner.filterUuid(BLEUART_UUID_SERVICE);
Bluefruit.Scanner.useActiveScan(false); // Don't request scan response data
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
}
@@ -133,14 +135,10 @@ void setup()
*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
// Check if advertising data contains the BleUart service UUID
if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
{
Serial.print("BLE UART service detected. Connecting ... ");
// Connect to the device with bleuart service in advertising packet
Bluefruit.Central.connect(report);
}
// Since we configure the scanner with filterUuid()
// Scan callback only invoked for device with bleuart service advertised
// Connect to the device with bleuart service in advertising packet
Bluefruit.Central.connect(report);
}
/**
@@ -90,6 +90,8 @@ void loop()
*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
// Since we configure the scanner with filterUuid()
// Scan callback only invoked for device with hrm service advertised
// Connect to device with HRM service in advertising
Bluefruit.Central.connect(report);
}
@@ -59,15 +59,15 @@ void setup()
* - Enable auto scan if disconnected
* - Interval = 100 ms, window = 80 ms
* - Don't use active scan
* - Filter only accept HID service in advertising
* - Start(timeout) with timeout = 0 will scan forever (until connected)
*/
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
Bluefruit.Scanner.filterService(hid); // only report HID service
Bluefruit.Scanner.useActiveScan(false);
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
Bluefruit.Scanner.filterService(hid); // only report HID service
}
/**
@@ -76,7 +76,9 @@ void setup()
*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
// Connect to device
// Since we configure the scanner with filterUuid()
// Scan callback only invoked for device with hid service advertised
// Connect to the device with hid service in advertising packet
Bluefruit.Central.connect(report);
}
@@ -221,4 +223,3 @@ void processKeyboardReport(hid_keyboard_report_t* report)
// update last report
memcpy(&last_kbd_report, report, sizeof(hid_keyboard_report_t));
}
@@ -59,12 +59,13 @@ void setup()
void scan_callback(ble_gap_evt_adv_report_t* report)
{
PRINT_LOCATION();
uint8_t len = 0;
uint8_t buffer[32];
memset(buffer, 0, sizeof(buffer));
/* Display the timestamp and device address */
if (report->scan_rsp)
if (report->type.scan_response)
{
Serial.printf("[SR%10d] Packet received from ", millis());
}
@@ -77,11 +78,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
Serial.print("\n");
/* Raw buffer contents */
Serial.printf("%14s %d bytes\n", "PAYLOAD", report->dlen);
if (report->dlen)
Serial.printf("%14s %d bytes\n", "PAYLOAD", report->data.len);
if (report->data.len)
{
Serial.printf("%15s", " ");
Serial.printBuffer(report->data, report->dlen, '-');
Serial.printBuffer(report->data.p_data, report->data.len, '-');
Serial.println();
}
@@ -90,20 +91,20 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
/* Adv Type */
Serial.printf("%14s ", "ADV TYPE");
switch (report->type)
if ( report->type.connectable )
{
case BLE_GAP_ADV_TYPE_ADV_IND:
Serial.printf("Connectable undirected\n");
break;
case BLE_GAP_ADV_TYPE_ADV_DIRECT_IND:
Serial.printf("Connectable directed\n");
break;
case BLE_GAP_ADV_TYPE_ADV_SCAN_IND:
Serial.printf("Scannable undirected\n");
break;
case BLE_GAP_ADV_TYPE_ADV_NONCONN_IND:
Serial.printf("Non-connectable undirected\n");
break;
Serial.print("Connectable ");
}else
{
Serial.print("Non-connectable ");
}
if ( report->type.directed )
{
Serial.println("directed");
}else
{
Serial.println("undirected");
}
/* Shortened Local Name */
@@ -178,6 +179,10 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
}
Serial.println();
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner start() to resume scanning
Bluefruit.Scanner.start();
}
void printUuid16List(uint8_t* buffer, uint8_t len)
@@ -209,9 +214,5 @@ void printUuid128List(uint8_t* buffer, uint8_t len)
void loop()
{
// Toggle both LEDs every 1 second
digitalToggle(LED_RED);
delay(1000);
// nothing to do
}
@@ -122,7 +122,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
/* Choose a peripheral to connect with by searching for an advertisement packet with a
Complete Local Name matching our target device*/
uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 };
uint8_t buffer[BLE_GAP_ADV_SET_DATA_SIZE_MAX] = { 0 };
Serial.print("Parsing report for Local Name ... ");
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer)))
@@ -131,7 +131,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
Serial.printf("%14s %s\n", "Local Name:", buffer);
Serial.print(" Local Name data: ");
printHexList(buffer, BLE_GAP_ADV_MAX_SIZE );
printHexList(buffer, BLE_GAP_ADV_SET_DATA_SIZE_MAX );
Serial.print("Determining Local Name Match ... ");
if ( !memcmp( buffer, SENSORTAG_ADV_COMPLETE_LOCAL_NAME, sizeof(SENSORTAG_ADV_COMPLETE_LOCAL_NAME)) )
@@ -144,11 +144,16 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
else
{
Serial.println("No Match");
Bluefruit.Scanner.start(); // continue scanning
}
}
else
{
Serial.println("Failed");
Serial.println("Failed");
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner start() to resume scanning
Bluefruit.Scanner.start();
}
}
@@ -163,7 +168,7 @@ void connect_callback(uint16_t conn_handle)
Serial.println( conn_handle );
/* Complete Local Name */
uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 };
uint8_t buffer[BLE_GAP_ADV_SET_DATA_SIZE_MAX] = { 0 };
// If Service is not found, disconnect and return
Serial.print("Discovering Optical Service ... ");
@@ -289,15 +294,15 @@ void printReport( const ble_gap_evt_adv_report_t* report )
Serial.print( " rssi: " );
Serial.println( report->rssi );
Serial.print( " scan_rsp: " );
Serial.println( report->scan_rsp );
Serial.print( " type: " );
Serial.println( report->type );
Serial.println( report->type.scan_response );
// Serial.print( " type: " );
// Serial.println( report->type );
Serial.print( " dlen: " );
Serial.println( report->dlen );
Serial.println( report->data.len );
Serial.print( " data: " );
for( int i = 0; i < report->dlen; i+= sizeof(uint8_t) )
for( int i = 0; i < report->data.len; i+= sizeof(uint8_t) )
{
Serial.printf( "%02X-", report->data[ i ] );
Serial.printf( "%02X-", report->data.p_data[ i ] );
}
Serial.println("");
}
+1
View File
@@ -220,6 +220,7 @@ setIntervalMS KEYWORD2
filterRssi KEYWORD2
filterMSD KEYWORD2
filterUuid KEYWORD2
filterService KEYWORD2
clearFilters KEYWORD2
restartOnDisconnect KEYWORD2
+37 -22
View File
@@ -245,12 +245,12 @@ bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEU
return false;
}
bool BLEScanner::checkReportForService(const ble_gap_evt_adv_report_t* report, BLEClientService svc)
bool BLEScanner::checkReportForService(const ble_gap_evt_adv_report_t* report, BLEClientService& svc)
{
return checkReportForUuid(report, svc.uuid);
}
bool BLEScanner::checkReportForService(const ble_gap_evt_adv_report_t* report, BLEService svc)
bool BLEScanner::checkReportForService(const ble_gap_evt_adv_report_t* report, BLEService& svc)
{
return checkReportForUuid(report, svc.uuid);
}
@@ -335,35 +335,50 @@ 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 const* evt_report = &evt->evt.gap_evt.params.adv_report;
bool invoke_cb = false;
// filter by rssi
if ( _filter_rssi > evt_report->rssi ) break;
// filter by uuid
if ( _filter_uuid_count )
do
{
uint8_t i;
for(i=0; i<_filter_uuid_count; i++)
// filter by rssi
if ( _filter_rssi > evt_report->rssi ) break;
// filter by uuid
if ( _filter_uuid_count )
{
if ( checkReportForUuid(evt_report, _filter_uuid[i]) ) break;
uint8_t i;
for(i=0; i<_filter_uuid_count; i++)
{
if ( checkReportForUuid(evt_report, _filter_uuid[i]) ) break;
}
// If there is no matched UUID in the list --> filter failed
if ( i == _filter_uuid_count ) break;
}
// If there is no matched UUID in the list --> filter failed
if ( i == _filter_uuid_count ) break;
}
// filter by MSD if present
if ( _filter_msd_en )
{
uint16_t id;
if ( !(parseReportByType(evt_report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&id, 2) && (id == _filter_msd_id)) ) break;
}
// filter by MSD if present
if ( _filter_msd_en )
invoke_cb = true;
} while(0); // for quick break
if ( invoke_cb )
{
uint16_t id;
if ( !(parseReportByType(evt_report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&id, 2) && (id == _filter_msd_id)) ) break;
}
ble_gap_evt_adv_report_t* adv_report = (ble_gap_evt_adv_report_t*) rtos_malloc( sizeof(ble_gap_evt_adv_report_t) );
VERIFY(adv_report,);
ble_gap_evt_adv_report_t* adv_report = (ble_gap_evt_adv_report_t*) rtos_malloc( sizeof(ble_gap_evt_adv_report_t) );
(*adv_report) = (*evt_report);
if (_rx_cb) ada_callback(adv_report, _rx_cb, adv_report);
(*adv_report) = (*evt_report);
if (_rx_cb) ada_callback(adv_report, _rx_cb, adv_report);
}else
{
// continue scanning since report is filtered and callback is not invoked
VERIFY_STATUS( sd_ble_gap_scan_start(NULL, &_report_data), );
}
}
break;
+2 -2
View File
@@ -85,8 +85,8 @@ public:
uint8_t parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* buf, uint8_t bufsize = 0);
bool checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid);
bool checkReportForService(const ble_gap_evt_adv_report_t* report, BLEClientService svc);
bool checkReportForService(const ble_gap_evt_adv_report_t* report, BLEService svc);
bool checkReportForService(const ble_gap_evt_adv_report_t* report, BLEClientService& svc);
bool checkReportForService(const ble_gap_evt_adv_report_t* report, BLEService& svc);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY