This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Remove non-continuity from BLE scanning for project nRF5_SDK_17.0.2_d674dde/examples/ble_central/ble_app_uart_c

Dear community,

I'm facing a problem during bluetooth scanning. I'm using the example named in the subject. This allows me to use the UART as debug printout.

I slightly adjusted the scan_evt_handler:

/**@brief Function for handling Scanning Module events.
 */
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
    ret_code_t err_code;

    switch(p_scan_evt->scan_evt_id)
    {
         case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
         {
              err_code = p_scan_evt->params.connecting_err.err_code;
              APP_ERROR_CHECK(err_code);
              printf("Connecting error.\n");
         } break;

        
         case NRF_BLE_SCAN_EVT_CONNECTED:
         {
              ble_gap_evt_connected_t const * p_connected =
                               p_scan_evt->params.connected.p_connected;
             // Scan is automatically stopped by the connection.
             NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
                      p_connected->peer_addr.addr[0],
                      p_connected->peer_addr.addr[1],
                      p_connected->peer_addr.addr[2],
                      p_connected->peer_addr.addr[3],
                      p_connected->peer_addr.addr[4],
                      p_connected->peer_addr.addr[5]
                      );
             printf("connected.\n");
         } break;

         case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
         {
             NRF_LOG_INFO("Scan timed out.");
             printf("Scan timed out.\n");
             scan_start();
         } break;

         default:
             scanNr=(scanNr+1)%65535;
             printf("scan/time: %u %d\n", scanNr, my_timer_get_ms());
             NRF_LOG_INFO("Scanning evt.");
             break;
    }
}

To add the my_timer_get_ms(), I followed:

https://devzone.nordicsemi.com/f/nordic-q-a/21402/how-to-call-time-function-in-nordic-nrf518222-milliseconds/122929

The problem is, that the scanner is not running smoothly, which means, it temporarely produces results each 0.1s (as expected from sdk_config setting: #define NRF_BLE_SCAN_SCAN_INTERVAL 160), but then it halts in com-viewer for about 1.2 seconds before resuming again.

As I can see from the timestamp, the CPU/timer is not stalled during these 1.2 seconds, because when operation resumes, the value of my_timer_get_ms() also increased by 1.2s.

Any advice or help on how to get rid of that 1.2 second sporadic scan-outages would be greatly appreciated.

Best regards,

Richard

  • Hi,

    Try using the logger instead, the logging will then be deferred and processed in idle stage.

    NRF_LOG_INFO("scan/time: %u %d", scanNr, my_timer_get_ms());

  • Following your suggestion, i changed the code and removed all printfs, so i changed:

    default:
                 scanNr=(scanNr+1)%65535;
                 printf("scan/time: %u %d\n", scanNr, my_timer_get_ms());
                 NRF_LOG_INFO("Scanning evt.");
                 break;

    void print_address(const ble_gap_evt_adv_report_t* p_adv_report) {
        NRF_LOG_INFO("addr: %02x:%02x:%02x:%02x:%02x:%02x",
           p_adv_report->peer_addr.addr[5],
           p_adv_report->peer_addr.addr[4],
           p_adv_report->peer_addr.addr[3],
           p_adv_report->peer_addr.addr[2],
           p_adv_report->peer_addr.addr[1],
           p_adv_report->peer_addr.addr[0]);
    }
    
    static void scan_evt_handler(scan_evt_t const * p_scan_evt)
    {
        (...code as before, no printf...)
            default:
                scanNr=(scanNr+1)%65535;
                NRF_LOG_INFO("scan/time/rssi: %u %d %d", scanNr, my_timer_get_ms(), p_scan_evt->params.filter_match.p_adv_report->rssi);
                //printf("scan/time: %u %d\n", scanNr, my_timer_get_ms());
                print_address(p_scan_evt->params.filter_match.p_adv_report);
                break;
    }

    Now this results in log, where my device would be Fx:Ex:6x:7x:0x:Fx, x is to anonymize MAC:

    <info> app: scan/time/rssi: 187 27959 -45
    <info> app: addr: Fx:Ex:6x:7x:0x:Fx
    <info> app: scan/time/rssi: 188 27971 -85
    <info> app: addr: other
    <info> app: scan/time/rssi: 189 28142 -92
    <info> app: addr: other
    <info> app: scan/time/rssi: 190 28142 -93
    <info> app: addr: other
    <info> app: scan/time/rssi: 191 28161 -80
    <info> app: addr: other
    <info> app: scan/time/rssi: 192 28162 -80
    <info> app: addr: other
    <info> app: scan/time/rssi: 193 28681 -78
    <info> app: addr: other
    <info> app: scan/time/rssi: 194 28681 -78
    <info> app: addr: other
    <info> app: scan/time/rssi: 195 28787 -89
    <info> app: addr: other

    [good, 27959 to 28975 is ca. 1000ms]

    <info> app: scan/time/rssi: 196 28975 -55
    <info> app: addr: Fx:Ex:6x:7x:0x:Fx


    <info> app: scan/time/rssi: 197 29059 -89
    <info> app: addr: other
    <info> app: scan/time/rssi: 198 29549 -89
    <info> app: addr: other

    [good, 28975 to 29986 is ca. 1000ms]

    <info> app: scan/time/rssi: 199 29986 -50
    <info> app: addr: Fx:Ex:6x:7x:0x:Fx
    <info> app: scan/time/rssi: 200 30180 -90
    <info> app: addr: other
    <info> app: scan/time/rssi: 201 30484 -90
    <info> app: addr: other
    <info> app: scan/time/rssi: 202 30501 -78
    <info> app: addr: other
    <info> app: scan/time/rssi: 203 30501 -78
    <info> app: addr: other
    <info> app: scan/time/rssi: 204 30760 -78
    <info> app: addr: other
    <info> app: scan/time/rssi: 205 30761 -78
    <info> app: addr: other
    <info> app: scan/time/rssi: 206 30800 -90
    <info> app: addr: other
    <info> app: scan/time/rssi: 207 30961 -85
    <info> app: addr: other

    [good, 29986 to 31001 is ca. 1000ms]
    <info> app: scan/time/rssi: 208 31001 -45
    <info> app: addr: Fx:Ex:6x:7x:0x:Fx

    [now continuously follow many "other", while "Fx:Ex:6x:7x:0x:Fx" misses till:]

    <info> app: scan/time/rssi: 265 41143 -50
    <info> app: addr: Fx:Ex:6x:7x:0x:Fx

    This is not good, as there was no detection of my device for ca 10s.

    During the last log-period, only 265-208=57 scans were performed while ms time passed as 41143-31001=ca. 10s.

    When checking the BLE-beacon peripheral with App nRF Connect, I get fairly stable results concerning the sender, as shown in line after rssi value, so I assume the sender is working.

    Any further suggestions to get all beacons?

    Best regards,

    Richard

  • Try setting scan interval == scan window. i.e., in sdk_config.h, try setting

    NRF_BLE_SCAN_SCAN_WINDOW
    NRF_BLE_SCAN_SCAN_INTERVAL

    both to 160

Related