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,

    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

  • 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

Related