BLE SCAN UUID Custom and Vendor Specific

I am using nRF52832 and Nordic connect sdk. I want filter scan for the beacon having specific uuid. I have programmed the dk with this code

#include <zephyr/types.h>
#include <stddef.h>
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/settings/settings.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/sys/byteorder.h>
#include <bluetooth/scan.h>


#define TARGET_UUID BT_UUID_128_ENCODE(0x2F234454, 0xCF6D, 0x4A0F, 0xADF2, 0xF4911BA9FFA6)

#define BT_UUID_BEACON_SCAN BT_UUID_DECLARE_128(TARGET_UUID)


static void scan_filter_match(struct bt_scan_device_info *device_info,
                              struct bt_scan_filter_match *filter_match,
                              bool connectable)
{
    char addr[BT_ADDR_LE_STR_LEN];
    bt_addr_le_to_str(device_info->recv_info->addr, addr, sizeof(addr));
    printk("Filters matched. Address: %s, Connectable: %d\n", addr, connectable);
}

static void scan_connecting_error(struct bt_scan_device_info *device_info)
{
    printk("Connecting failed\n");
}

static void scan_connecting(struct bt_scan_device_info *device_info,
                            struct bt_conn *conn)
{
    // Do nothing
}

BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL,
                scan_connecting_error, scan_connecting);

static int scan_init(void)
{
    int err;
    struct bt_scan_init_param scan_init = {
        .connect_if_match = 0,
    };

    bt_scan_init(&scan_init);
    // Register scan callbacks
    bt_scan_cb_register(&scan_cb);

    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_BEACON_SCAN);
    if (err) {
        printk("Scanning filters cannot be set (err %d)\n", err);
        return err;
    }

    err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
    if (err) {
        printk("Filters cannot be turned on (err %d)\n", err);
        return err;
    }

    printk("Scan module initialized\n");
    return err;
}

static void scan_start(void)
{
    int err;

    err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
    if (err) {
        printk("Scanning failed to start (err %d)\n", err);
    } else {
        printk("Scanning successfully started\n");
    }
}

int main(void)
{
    int err;

    err = bt_enable(NULL);
    if (err) {
        printk("Bluetooth init failed (err %d)\n", err);
        return 0;
    }
    printk("Bluetooth initialized\n");

    if (IS_ENABLED(CONFIG_SETTINGS)) {
        int rc = settings_load();
        if (rc) {
            printk("settings_load() failed (err %d)\n", rc);
            return 0;
        }
    }

    err = scan_init();
    if (err != 0) {
        printk("scan_init failed (err %d)\n", err);
        return 0;
    }

    printk("Starting BLE UUID scanning example\n");

    scan_start();

    return 0;
}

The serial terminal is showing this

It is not showing any error or device found although i can scan my device on other applications. It once shows the scan filter match and MAC address of the device but then never displays that again. Could you please inform which might be the issue. Additionally, In case of this custom UUID if i want to use vendor specific uuid how i will do this, can i implement it with the same code?

Thankyou,

Regards.

Parents Reply Children
  • Hi,

    I am using the nRF52840 as a central device for scanning beacons. Therefore, I don't see the need for advertising on my end. Additionally, I am using the same code for the scan filter that you provided, but it is not showing any results. My beacon is advertising its UUID correctly.

    Could you provide further guidance on this?

  • Please make sure to add the correct custom UUID in the advertisement packet on the peripheral side. 

    Is your beacon using extended advertisements? If so, make sure your central device is set up to scan for these.

    Does your beacon use non-connectable advertising mode? If so, it can't be connected to. If your central device is set up to only connect to connectable devices, it won't see these beacons.

    Also, check your beacon's advertising interval. If the beacon's advertising interval is too long, the central device might not pick it up during its scan window. You might need to increase the duration of your scan window or decrease the advertising interval on the beacon.

    If you're still having trouble, you might find it helpful to use a BLE sniffer tool to capture the advertising packets being sent by the beacon. This can help you verify that the beacon is advertising correctly and give you more information about what might be going wrong.

Related