How to scan multiple peripheral devices and get connected to one.

Hi,

I'm using nRF5340 DK, one board is programmed as central and 2 boards are programmed as peripherals. When I flash the central program it is selecting a random board and getting connected to it. All I wanted to do is after scanning the central should display all the peripherals it has scanned, later it should ask the user to input the peripheral address of choice and once user inputs the data, needs to get connected to that particular board. I'm stuck with this. Can you please help me?

Regards

Karthik Kumar

  • Hello Karthik,

    From your application you can choose whether or not to connect to the devices that you are receiving advertisements from. 

    If you study the sample found in NCS\nrf\samples\bluetooth\central_hr_coded, you can see a device that is scanning. As you can see in scan_init(), it sets .connect_if_match = 0, which makes it receive the callback scan_filter_match(), from which it tries to connect to the device that matched the filters using bt_conn_le_create(). All the functions that I mentioned are in the main.c file of this sample.

    So then it is up to you how you want to communicate with the application. How does it show you the filter matches, and how do you select which one to connect to. You mentioned buttons, so you would need a button handler (look at how the NCS\nrf\samples\bluetooth\peripheral_uart sets up and uses the button_changed() callback). 

    Best regards,

    Edvin

  • Hi Edvin,

    I'm using the,

    http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/bluetooth/central_hr_coded/README.html

    http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/bluetooth/peripheral_hr_coded/README.html

    Was trying to modify in the same main program where I can select which device to get connect to, upon the display of all the available devices on the terminal window with there address. Later I wanted to choose a particular device and get that connected to. But here the connection is happening randomly.

    Regards

    Karthik Kumar

    Bluetooth: Central Heart Rate Monitor with Coded PHY

  • Hi Edvin,

    After the flash, I'm getting the below statements on the terminal window.

    *** Booting Zephyr OS build v2.7.0-ncs1 ***

    Bluetooth initialized Test

    Scanning successfully started

    Filters matched

    Address: C8:57:53:55:1B:50 (random) connectable: yes

    Here only one of the board is been chosen and printed randomly on the terminal, but I wanted it to print all the devices it has scanned.

    Enter the device Address:

    At this point i wanted to pass the address of the board that i wanted to connect amongst the  2 boards.

    Connection pending

    Connected: C8:57:53:55:1B:50 (random), tx_phy 4, rx_phy 4

    post which the connection should happen with the selected board. How can I do this?

    Regards

    Karthik Kumar

  • KarthikKumar said:

    Here only one of the board is been chosen and printed randomly on the terminal, but I wanted it to print all the devices it has scanned.

    That is because the filter_matched function stops the scanning. Try to restart the scanning, or to comment out the stop scanning. Just be aware that you need to stop scannign before you decide to connect to a device. 

    KarthikKumar said:

    At this point i wanted to pass the address of the board that i wanted to connect amongst the  2 boards.

    How do you want to pass on the address?

    KarthikKumar said:

    post which the connection should happen with the selected board. How can I do this?

    I understand what you want to do, but there are a million different ways to acheive this. Do you want your device to have it's own keyboard, do you intend to receive the data over UART? Do you want to control it via buttons? Whatever solution you go with, it requires some work for your application to do what you want it to. This is why I ask the questions above. 

  • Hi Edvin,

    How do you want to pass on the address?

    I want to pass it via the terminal window, PuTTy. Manually enter the data.

    I understand what you want to do, but there are a million different ways to acheive this. Do you want your device to have it's own keyboard, do you intend to receive the data over UART? Do you want to control it via buttons? Whatever solution you go with, it requires some work for your application to do what you want it to. This is why I ask the questions above.

    Wanted to control it via keyboard.

Related