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

Example use of sd_ble_gap_scan_start

I need to scan for nearby beacons. This was featured in the mbed examples but does not seem to be described anywhere else

I am using SDK12 at the moment.

I found the definition of this API but cannot find any example of its use

Is there one published anywhere? thx.

  • Hi,

    Our BLE central examples use the function sd_ble_gap_scan_start(...)

    You can find the BLE central examples in the folder

    <SDK_InstallFolder>\examples\ble_central
    

    I would recommend taking a look at the Nordic UART Service Client example, in the folder

    <SDK_InstallFolder>\examples\ble_central\ble_app_uart_c
    
  • thx for that. sorry I forgot to mention that I had done this with a BLE peripheral on mbed which is why I was looking in the SDK's ble_peripheral folder. Maybe it's more appropriate to do it as central as I only want to know which beacons are present. It looks as if I need to connect to each to obtain its address. I tried the SDK 12.3 ble_app_uart_c example with s132 on 52-DK and so far it does not print the start message.

  • You don't need to connect to get the BLE address of the beacons. The BLE address is part of the advertising packet, and you can access when you get the event BLE_GAP_EVT_ADV_REPORT.

    Looking at the function ble_evt_handler in the ble_app_uart_c example, you can just comment out the connect request(sd_ble_gap_connect()), and then print the BLE address:

    NRF_LOG_INFO("Beacon address: %02x%02x%02x%02x%02x%02x",
             p_adv_report->peer_addr.addr[0],
             p_adv_report->peer_addr.addr[1],
             p_adv_report->peer_addr.addr[2],
             p_adv_report->peer_addr.addr[3],
             p_adv_report->peer_addr.addr[4],
             p_adv_report->peer_addr.addr[5]
             );
    

    You can also remove the is_uuid_present() function to be able to scan beacons that dont have the NUS UUID present in the ADV packet.

    You should be able to do this with a BLE peripheral example also, but it will require some work to add all the BLE central specific files to the project. But if you need an example that is both BLE central and BLE peripheral you should take a look at the ble_central_and_peripheral examples in the ble_central_and_peripheral folder.

  • Works great thx @Sigurd. I'll leave as a central for now.

Related