From 905bb3b67132998af8ab195e7291bdd5036ec5c5 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 8 Nov 2018 20:14:34 +0700 Subject: [PATCH] correct scanner when using filter. Tested all central example --- .project | 72 +++++++++++++++++++ .../central_bleuart/central_bleuart.ino | 6 +- .../central_bleuart_multi.ino | 14 ++-- .../central_custom_hrm/central_custom_hrm.ino | 2 + .../Central/central_hid/central_hid.ino | 9 +-- .../central_scan_advanced.ino | 45 ++++++------ .../central_ti_sensortag_optical.ino | 25 ++++--- libraries/Bluefruit52Lib/keywords.txt | 1 + libraries/Bluefruit52Lib/src/BLEScanner.cpp | 59 +++++++++------ libraries/Bluefruit52Lib/src/BLEScanner.h | 4 +- 10 files changed, 168 insertions(+), 69 deletions(-) create mode 100644 .project diff --git a/.project b/.project new file mode 100644 index 00000000..d8d8c308 --- /dev/null +++ b/.project @@ -0,0 +1,72 @@ + + + Adafruit_nRF52_Arduino + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + arduino-libraries + 2 + /home/hathach/Arduino/libraries + + + + + 1498710934502 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.o + + + + 1498710934507 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.d + + + + 1498710934515 + + 6 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.pyc + + + + 1498710934520 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.so + + + + diff --git a/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino b/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino index b943319b..87bbd45f 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino @@ -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() } } } - diff --git a/libraries/Bluefruit52Lib/examples/Central/central_bleuart_multi/central_bleuart_multi.ino b/libraries/Bluefruit52Lib/examples/Central/central_bleuart_multi/central_bleuart_multi.ino index 1722c67b..053cd22a 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_bleuart_multi/central_bleuart_multi.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_bleuart_multi/central_bleuart_multi.ino @@ -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); } /** diff --git a/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino b/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino index 88cb18e6..594f33d9 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino @@ -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); } diff --git a/libraries/Bluefruit52Lib/examples/Central/central_hid/central_hid.ino b/libraries/Bluefruit52Lib/examples/Central/central_hid/central_hid.ino index 45e06832..e60c18bf 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_hid/central_hid.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_hid/central_hid.ino @@ -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)); } - diff --git a/libraries/Bluefruit52Lib/examples/Central/central_scan_advanced/central_scan_advanced.ino b/libraries/Bluefruit52Lib/examples/Central/central_scan_advanced/central_scan_advanced.ino index 527dcb3a..999ef600 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_scan_advanced/central_scan_advanced.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_scan_advanced/central_scan_advanced.ino @@ -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 } - diff --git a/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino b/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino index 48d2f4b7..dc590303 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino @@ -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(""); } diff --git a/libraries/Bluefruit52Lib/keywords.txt b/libraries/Bluefruit52Lib/keywords.txt index 6400e143..f841316d 100644 --- a/libraries/Bluefruit52Lib/keywords.txt +++ b/libraries/Bluefruit52Lib/keywords.txt @@ -220,6 +220,7 @@ setIntervalMS KEYWORD2 filterRssi KEYWORD2 filterMSD KEYWORD2 filterUuid KEYWORD2 +filterService KEYWORD2 clearFilters KEYWORD2 restartOnDisconnect KEYWORD2 diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.cpp b/libraries/Bluefruit52Lib/src/BLEScanner.cpp index 18d482f6..73be6717 100644 --- a/libraries/Bluefruit52Lib/src/BLEScanner.cpp +++ b/libraries/Bluefruit52Lib/src/BLEScanner.cpp @@ -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; diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.h b/libraries/Bluefruit52Lib/src/BLEScanner.h index 82de7072..e25b3a54 100644 --- a/libraries/Bluefruit52Lib/src/BLEScanner.h +++ b/libraries/Bluefruit52Lib/src/BLEScanner.h @@ -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