How to discover a service, disconnect from it and discover another one ?

CS_prog.zipRSSI_prog.zip

Hello,

I'm working on Windows 11, with the nRF Connect for VS Code v2025.5.512 extension, with ncs v3.0.2. I'm using the nRF54L15-dk (PCA10156; 0.9.2; 2025.7) development kits.

So, I want to use the channel sounding sample to measure a distance and then send a data if it's below a threshold. I first tried to implement that program with the BLE RSSI instead of using the CS and it works well (cf. RSSI_execution picture).

So my question is how to discover a service, disconnect from it and discover another one ?

Since the CS is using a service, I want to first discover the ranging service then if the measured distance is below the threshold, I want do discover my custom service to send my data. I tried discovering my custom service at the beginning and at the end of the loop that gets the distance in the main (while(true)) but then either it doesn't want to measure the distance and it sends the value directly or it gets the distance but can't send the data. (cf custom_begin and custom_end pictures respectively).

Do you know how I can fix those errors ? I've put my programs and pictures in the zip folders below for both the RSSI and the CS.

Thanks and best regards,

Patrick

Parents
  • Hello Patrick

    So my question is how to discover a service, disconnect from it and discover another one ?

    There is no need to disconnect from the device to discover other services. 

    Is it so that you have both the services working as it should in different applications? If so, look at how the service discovery is done in them. Try combining the two into the same central application. 

    Service discovery should only be done once per connection, and you can trigger it in the connected callback event, like it is done in most of our central application samples (e.g. using gatt_discover() in our central_uart sample). 

    Then, you can use your application logic to start sending notifications from the peripheral to the central whenever the distance is low enough.

    Best regards,

    Edvin

  • Hello Edvin,

    Thanks for the answer:

    In my program, I'm chaining the service and characteristics discoveries like that : ranging service -> custom service -> write characteristic -> confirm characteristic for the notification. So how can I switch from the ranging service to my custom when the distance is below a certain threshold ? Maybe it's due to the call of discover_custom_service() function that is misplaced in my program ? 

    Indeed, I don't think I need to disconnect from the first service to my custom one. 

    I don't know if my problem is clearer for you, now ?

    Best regards, 

    Patrick

  • Hello Patrick,

    Can you please upload your current application, so that I can have a look, and reproduce the issue that you are seeing?

    My initial guess is that it is waiting for the "Security Changed" event, which never occurs, but without seeing your application, it is difficult to say. 

    You still shouldn't need to disconnect -> reconnect in order to use other services, but I am not really sure what your application looks like at this point, or what it is trying to do.

    Best regards,

    Edvin

  • CS3_prog.zip

    Hello Edvin, 

    By the application, you meant both programs with the initiator and reflector ? 
    If yes, here is in the CS3_prog.zip both of them that I'm using on two nRF54L15-dk. 

    I tried putting the security changed in the connected_cb function with the init_channel_sounding function after the first iteration but it didn't work. It still block after the second connection was done. 

    You still shouldn't need to disconnect -> reconnect in order to use other services

    And no, I'm not disconnecting to discover both services, I followed the central_uart and the peripheral_ancs sample (to discover two services one after the other) and adapted it for my application. 

    Best regards, 

    Patrick

  • Hello,

    Running your applications, this is what I see in the initiator's log:

    *** Booting nRF Connect SDK v3.0.2-89ba1294ac9b ***
    *** Using Zephyr OS v4.0.99-f791c49f492c ***
    I: Starting Channel Sounding Initiator Sample
    I: SoftDevice Controller build revision:
    I: 89 9a 50 8a 95 01 9c 58 |..P....X
    I: fc 39 d2 c1 10 04 ee 02 |.9......
    I: 64 ce 25 be             |d.%.
    I: HW Platform: Nordic Semiconductor (0x0002)
    I: HW Variant: nRF54Lx (0x0005)
    I: Firmware: Standard Bluetooth controller (0x00) Version 137.20634 Build 2617349514
    I: Identity: F7:36:2B:14:6E:A9 (random)
    I: HCI: version 6.0 (0x0e) revision 0x30f3, manufacturer 0x0059
    I: LMP: version 6.0 (0x0e) subver 0x30f3
    I: NUS Client module initialized
    I: Filters matched. Address: E8:85:AF:8C:2B:33 (random) connectable: 1
    I: Connecting
    I: Connected to E8:85:AF:8C:2B:33 (random) (err 0x00)
    I: Security changed: E8:85:AF:8C:2B:33 (random) level 2
    I: MTU exchange success (498)
    I: Discovery completed for RANGING service (context == NULL)
    I: CS capability exchange completed.
    I: CS config creation complete. ID: 0
    I: CS security enabled.
    I: CS procedures enabled successfully
    I: Application completed its workflow
    I: CS procedures enabled:
     - config ID: 0
     - antenna configuration index: 0
     - TX power: -16 dbm
     - subevent length: 28198 us
     - subevents per event: 1
     - subevent interval: 0
     - event interval: 2
     - procedure interval: 10
     - procedure count: 0
     - maximum procedure length: 1000
    I: Distance estimates : ifft: 0.585532
    I: Device is close enough, initiating CUS discovery
    Starting CUSTOM discovery
     I: CS procedures disabled.
    I: Discovery completed for CUSTOM service
    [SUBSCRIBED]
    Confirmation received : 1234
    Write success !
    I: All operations complete, disconnecting...
    I: Disconnected (reason 0x16)
    W: opcode 0x2094 status 0x02
    Cycle connection-disconnection : 4307 ms

    And this is repeating forever. 

    This is not what you are seeing if you run it on 2x nRF54l15DKs?

    BR,

    Edvin

  • Hello Edvin, 

    Yes, I have the same log on the initiator. Now you're seeing that I'm using the reboot function in order to start a new scan after a 5 seconds' delay. But I want to scan again without rebooting my device since it consumes some power. How can I achieve that ? 

    Best regards, 

    Patrick

  • Hello,

    Sorry for the late reply.

    Yes. I noticed the reboots, since the boot banner was being printed repeatedly as well. 

    Patrick49 said:
    But I want to scan again without rebooting my device

    You want to scan what? Do another distance measurement? Or do you want to use the custom service?

    You already have all the handles (I presume). So why can't you just use them?

    Best regards,

    Edvin

Reply
  • Hello,

    Sorry for the late reply.

    Yes. I noticed the reboots, since the boot banner was being printed repeatedly as well. 

    Patrick49 said:
    But I want to scan again without rebooting my device

    You want to scan what? Do another distance measurement? Or do you want to use the custom service?

    You already have all the handles (I presume). So why can't you just use them?

    Best regards,

    Edvin

Children
No Data
Related