No device found with bt_le_scan_start and observer

Dear,

I am using nRF5340 and I am trying to scan devices around me. With the example observer my device founds other devices around me, but when I use the same configuration and the same functions into my project the device doesn't find anything. The code is the same, exactly the same but I can't see anything.

The prj.conf is

#Peripherals
CONFIG_GPIO=y
CONFIG_SPI=y

CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y

CONFIG_PRINTK=y


# Bootloader
CONFIG_BOOTLOADER_MCUBOOT=y

# Bluetooth configuration
CONFIG_BT=y
#CONFIG_BT_PERIPHERAL=y
#CONFIG_BT_DEVICE_NAME="Test"
#CONFIG_BT_DEBUG_LOG=y
#CONFIG_BT_CENTRAL=y
#CONFIG_BT_SCAN=y
CONFIG_BT_OBSERVER=y

# Other 
CONFIG_MAIN_STACK_SIZE=2048

CONFIG_LOG=y

And the code is:

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>


#if (APP_DEBUG_ON == 1)
#include <zephyr/sys/printk.h>
#endif

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

static void deviceFound (const bt_addr_le_t *addr, 
                         int8_t rssi, 
                         uint8_t type,
                         struct net_buf_simple *buf)
{
    char dev[BT_ADDR_LE_STR_LEN];

    // if (type == BT_GAP_ADV_TYPE_ADV_SCAN_IND)
    // {
        bt_addr_le_to_str(addr, dev, sizeof(dev));
        printk("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",dev, type, buf->len, rssi);
    // }


}

void Bluetooth_init (void)
{
    int err = 0;

    err = bt_enable(NULL);
    if (err) 
    {
#if (APP_DEBUG_ON == 1)
        printk("ERROR: Bluetooth init failed (err %d)\n", err);
#endif
        goto bluetooth_init_error;
    }

#if (APP_DEBUG_ON == 1)
    printk("BLUETOOTH0: Bluetooth initialized\n");
#endif

	struct bt_le_scan_param scanParam = 
    {
        .type       = BT_LE_SCAN_TYPE_PASSIVE,
        .options    = BT_LE_SCAN_OPT_FILTER_DUPLICATE,
        .interval   = BT_GAP_SCAN_FAST_INTERVAL,
        .window     = BT_GAP_SCAN_FAST_WINDOW,
    };

    err = bt_le_scan_start(&scanParam, deviceFound);
    if (err) 
    {
#if (APP_DEBUG_ON == 1)
        printk("ERROR: Scanning failed to start (err %d)\n", err);
#endif
        goto bluetooth_init_error;
    }
#if (APP_DEBUG_ON == 1)
    printk("BLUETOOTH0: Scanning successfully started\n");
#endif

bluetooth_init_error:
    if (err)
    {
        // TODO
    }
}

The output log is:

BLUETOOTH0: Bluetooth initialized
BLUETOOTH0: Scanning successfully started

Where is it the error? Haw can I debug this issue?

Thanks

Marco

Parents
  • Hi Marco

    Can you try enabling CONFIG_BT_HCI=y as well in your config file. I'm not quite sure why you're not able to scan as expected with this implementation, might be because I'm missing insight, but your code builds fine in hello_world on our end. What Bluetooth controller are you using on the NET core? The Zephyr BLE controller or Nordic's SoftDevice controller?

    Best regards,

    Simon

  • Hi Simon,

    I added the row into the config file. I put the start scan a little bit earlier than before and now somethings came up as you can see in the image (the black side is the app core, the white side is the network core)... but after some seconds no more can be found... nothing.

    Just to be sure of this behaviour I switched off some beacons,  start the scan, and now switch on the beacons.. and it doesn't show these devices.  

    I am sure that the app core is not blocked in a while loop or in a fail, because it does other things!

    Best regards

    Marco

  • Hi Hieu,

    sorry for the delay, but I was out-of-office for holiday.

    Your code work without any problem. I tried a new fresh project... but not works! Same code, it is only a new project! It is unbelivable for me!

    Marco

  • Hi Marco,

    The fact that my project works on your end suggests that at least the problem is somewhere in the project setup, so it might be a good sign.

    Could you run a diff/compare of the projects, see what the differences are? Remember to exclude the build folder.

    Hieu

  • Hi Hieu,

    it is very strange... there are no differences between the two project!

    I am going crazy with that!

    Marco

  • I found the difference:

    In your code, after 

    Bluetooth_init();
    the main exit but the application still alive. In My application, there is a while loop after the bluetooth initialization. Is that the problem?

    Thank

    Marco

  • Hi Marco,

    Depends on what you do in your while loop. Does it ever yield CPU to other thread? If it is always doing something, then yes, that would be a problem.

    Here is the description of the main thread from Zephyr OS documentation:

    Main thread
    This thread performs kernel initialization, then calls the application’s main() function (if one is defined).

    By default, the main thread uses the highest configured preemptible thread priority (i.e. 0). If the kernel is not configured to support preemptible threads, the main thread uses the lowest configured cooperative thread priority (i.e. -1).

    The main thread has the highest priority, and thus will not let any other thing runs unless it exits or actively yield.

    I am not well versed in the topics of Zephyr OS thread, but my guess (which you please take with a grain of salt) is that your main thread chokes the printing, not the actual Bluetooth functionality that runs on a different core.

    I am quite curious now how the setup with advertising works though. Does that setup also have the while loop?

    Hieu

Reply
  • Hi Marco,

    Depends on what you do in your while loop. Does it ever yield CPU to other thread? If it is always doing something, then yes, that would be a problem.

    Here is the description of the main thread from Zephyr OS documentation:

    Main thread
    This thread performs kernel initialization, then calls the application’s main() function (if one is defined).

    By default, the main thread uses the highest configured preemptible thread priority (i.e. 0). If the kernel is not configured to support preemptible threads, the main thread uses the lowest configured cooperative thread priority (i.e. -1).

    The main thread has the highest priority, and thus will not let any other thing runs unless it exits or actively yield.

    I am not well versed in the topics of Zephyr OS thread, but my guess (which you please take with a grain of salt) is that your main thread chokes the printing, not the actual Bluetooth functionality that runs on a different core.

    I am quite curious now how the setup with advertising works though. Does that setup also have the while loop?

    Hieu

Children
  • Hi Hieu,

    Ok, I have a problem. At this time the CPU is always doing something in the application code... (in the clear project that I used for the previous test, the while loop is empty).

    Which is the solution? Add in the main loop a ksleep or something else to switch threads (it is my first time with operating systems).

    About your last question: yes, the code is the same and there was code into the main loop.

    Best

    Marco

  • Hi Marco,

    Sorry I just now returned after a few days of sick leaves.

    It is curious why there is no issue when you add advertising. I would assume the same problem will arise.
    That being said, I too am not an expert with RTOS. I had to double check carefully with a colleague before sending my last reply.

    I am sure that adding ksleep is a solution, having tried that myself. However, I am not certain that it is the best solution. My not-an-expert suggestion is indeed to use different threads to handle different duties, rather than doing everything in a while loop. Without knowing what you are doing in that while loop, that is the best suggestion I can make right now.

    If you wish to learn more about the OS, Nordic does provide the nRF Connect SDK Fundamentals free online course. The Lesson 7 and 8 are made by the same colleague I mentioned above and should give you some idea. Though I suggest you still start from Lesson 1, and quickly skim and skip any topics you already know, rather than skipping the entire thing. There are some knowledge dependencies between the lessons.

    Best regards,

    Hieu

  • Hi Hieu,
    thank you for your suggestion, I'll study the course. If you have any news, please share it with me!
    In the meantime, I am facing another problem with USB, and I suppose that the issue is the same... but for this, I'll open a new ticket.
    Best
    Marco

  • Hi Marco,

    I consult further with the same colleague again. The lessons that I linked previously are the best resources we would currently recommend.

    Beyond the lessons, please read the documentation of the Zephyr OS. In particular, I think this page and its subpages are of immediate interest:

    Kernel Services — Zephyr Project Documentation (nordicsemi.com)

    We cannot really advise on how to setup the application in deeper details, as that depends a lot on teams'/personal preferences and the projects' natures.

    Best regards,

    Hieu

Related