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

Issue in connecting to multiple devices after the filters are matched

Hello,

I am using nRF52840-devkit with NCS SDK V1.4.0.

In "Central_Uart" example, there is a filtering method which is used to filter devices in the environment with a specific UUID. The "connect_if_match" is set to 1, so that if the filters are matched with a specific device then it connects automatically. But there is a limitation in the code provided in "scan.c" where filters are checked and device gets connected.

Here's the limitation:  In "scan_connect_with_target" function, they call "bt_scan_stop" in order to stop scanning and then connect to the first device with which it matches the filter. This means that it will connect to just 1 device at a time.

My question is, what if after filtering the devices in the environment, I get 3 devices which are having the same UUID. In that case, instead of connecting to all 3 (which I want), it will just connect to 1 device. This is assuming I set CONFIG_BT_MAX_CONN to 3.

Finally, I found this API "bt_conn_le_create_auto" which does auto connection to all available devices in whitelist, and it doesn’t stop scanning after connect. But as that says, it’s an auto connect to a whitelist only.

Question 1:  Why does the Central UART sample, or for that matter any BLE sample, allow for multiple connections (if you set CONFIG_BT_MAX_CONN >1) when they also use scan filters?  It seems the scan filters stop scanning, preventing more than one connection unless you manually restart the scan.  And I don’t see that the samples restart the scan after a connection.  In particular, the Throughput sample comes configured with MAX_CONN=2.  I tried getting two Throughput peripherals to connect to a Throughput central, but was only able to get one to connect.  Of course, only was able to connect one device because scanning stopped after the first connection.

Question 2:  If I want my project to use a scan filter with auto connect, how can I achieve more than one connection if the scan is going to stop after each connection? Do I have to have my own code restart the scan after each connection? 

I found a similar example on DevZone https://devzone.nordicsemi.com/f/nordic-q-a/49136/multicentral-zephyr-development-examples-or-documentation but it did not provide the solution.

Parents
  • I'll just leave some quick comments before the weekend. I'll investigate both of your questions more thoroughly next week.

    Have you looked at Zephyr issue 29223? It seems to be related to the same topic.

    Would it work for you to manually start scanning again after the first connection is made?

    Best regards,

    Simon

  • Simon,

    EDIT: Using V1.3.0. 

    EDIT: My advertising device is having this option - (BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME)

    I am not using the event based code which is used in Central_Uart example. 

    I tried using your solution to start the scan again. Here is the flow

    1) Set Filters

    2) Enable Filters

    3) Start Scan

    4) get device information through scan cb

    5) match the filters - if matched, stop scan - (filter by device name)

    6) connect using just this API "bt_conn_le_create" ,

             default_conn = bt_conn_ref(conn); // even without these 2 lines, if I do not start the scan, the devices do get connected. I am assuming, what these APIs do is when you have multiple connections, you can store the connection object by creating an array of these connection objects. So right now, I am just trying to connect to 1 device and then start scanning so that my scanner can scan for 2nd device possibility. I am using BT_LE_ADV_OPT_ONE_TIME so that once the device is connected, it does not need to advertise anymore but instead of connecting, it keeps on advertising.
             
             bt_conn_unref(conn);

    7) Start Scan and repeat the from step 3

    Now in this case, the 1st connection itself does not happen

    if I remove step 7, then 1 connection happens

    But to resolve my purpose of multiple connection, I will have to start the scan again. Now, if I do that the first connection also does not occur.

    Surprisingly, the "bt_conn_le_create" API does not return an error, it returns 0 which means connection is successful. Even if I have 5 filters matching devices in the environment, it will never return an error in either 5 of them and always say that I am connected. But when I started checking on Wireshark, I saw that even for 1 device, the connection does not happen when I restart the scan after getting "0" from "bt_conn_le_create"

    I am not using any scan callback or connection callback in my code. Is it compulsory to use?

    Do I need to configure something else in order to make this work?

  • I'm sorry for the delay on this. A huge part of the support team are currently on Christmas vacaction, and you may experience delayed answers. Most of the staff will be back by january 4th and you can expect an answer after then.

    Best regards,

    Simon

  • Have you made any progress on this? I set CONFIG_BT_MAX_CONN=2 in the central_uart and tried to connect to two peripheral_uart's. I was able to connect to the two peripheral's, however, I encountered some issues on service discovery on the second connection: https://github.com/nrfconnect/sdk-nrf/blob/master/samples/bluetooth/central_uart/src/main.c#L317 

    The central_uart code is not made to handle two connections, and you need to modify it to do so.

    You should create another instance of an bt_conn, and store your other connection handle there. In addition, when the service discoveryi is completed for the second connection, you should store the handles to the different characteristics separately from the first connection (check out the function discovery_complete()).

    There may be some simpler BLE Central samples than the central_uart that is easier to modify/build on.

    Best regards,

    Simon

  • Hi Simon, please note that we have referenced a modified central_uart project supplied by Nordic to support multiple connections.  Here is the link: github.com/.../multi-NUS

Reply Children
Related