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 Reply Children
  • 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

  • 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

Related