Multi Filter Scan match callback not triggering

Hello All,

I am attempting to use the scanning module to scan with a multiple filters. UUID and NAME filters to be exact.

I have done some testing with the code below and I have verified that the filters alone work but when I set the filter mode to multi filter by setting the match all filed in the bt_scan_filter_enable to be true.

However, given both the uuid and the name on my devices being kept the same and active at the same time the filter match callback doesn't trigger.

My code for the scanner is below:

```

//--------------SCAN TESTING START---------------------//

        // int err = 0;
        err = bt_enable(bt_ready_cb);
        if (err) {
                LOG_ERR("Bluetooth init failed (err %d)", err);
                return 0;
        }

        while(!btReady);

        const struct bt_scan_init_param bt_scan_init_opts = {
                        .scan_param = NULL, //default config
                        .connect_if_match = true,
                        .conn_param = NULL, //default config
        };
       
        bt_scan_init(&bt_scan_init_opts);
        BT_SCAN_CB_INIT(scan_cb, scan_filter_match, scan_filter_no_match, scan_connecting_error, scan_connecting);
        bt_scan_cb_register(&scan_cb);

        //--------------------UUID FILTER

        err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_LBS);
        if (err) {
                LOG_ERR("UUID scanning filters cannot be set (err %d)", err);
                return err;
        }

        //------------------SHORT NAME FILTER

        // struct bt_scan_short_name short_name_filter_data = {
        //         .name = TARGET_DEVICE_NAME,
        //         .min_len = sizeof(TARGET_DEVICE_NAME),
        // };

        // err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_SHORT_NAME,&short_name_filter_data);
        // if (err) {
        //      LOG_ERR("Short Name scanning filters cannot be set (err %d)", err);
        //      return err;
        // }

        //------------------NAME FILTER
        err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, TARGET_DEVICE_NAME);
        if (err) {
                LOG_ERR("Name scanning filters cannot be set (err %d)", err);
                return err;
        }


        err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER | BT_SCAN_NAME_FILTER, true);
        // err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);

        if (err) {
                LOG_ERR("Filters cannot be turned on (err %d)", err);
                return err;
        }

        LOG_INF("Scan module initialized");


        err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
        if (err) {
                LOG_ERR("Scanning failed to start (err %d)", err);
                return 0;
        }

        LOG_INF("Scanning successfully started");

        //--------------SCAN TESTING END---------------------//

The below is my filter match call back code:


void scan_filter_match(struct bt_scan_device_info *device_info, struct bt_scan_filter_match *filter_match, bool connectable)
{
   
    int err;
   
    if(filter_match->uuid.match){
        LOG_INF("UUID: ");
        for (int i = 0; i < filter_match->uuid.count; i++){
            LOG_INF("%d", filter_match->uuid.uuid[i]->type); // Logging as hexadecimal
            if (filter_match->uuid.uuid[i]->type == BT_UUID_TYPE_128) {
                LOG_INF("128 bit uuid found");
                struct bt_uuid_128 *u128 = (struct bt_uuid_128 *)filter_match->uuid.uuid;
                printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
                        // u128->val[0], u128->val[1], u128->val[2], u128->val[3],
                        // u128->val[4], u128->val[5],
                        // u128->val[6], u128->val[7],
                        // u128->val[8], u128->val[9],
                        // u128->val[10], u128->val[11], u128->val[12], u128->val[13], u128->val[14], u128->val[15]
                       
                        u128->val[15], u128->val[14], u128->val[13], u128->val[12],
                        u128->val[11], u128->val[10],
                        u128->val[9], u128->val[8],
                        u128->val[7], u128->val[6],
                        u128->val[5], u128->val[4], u128->val[3], u128->val[2], u128->val[1], u128->val[0]
                        );
            }
        }
    }

    if(filter_match->name.match){
        LOG_INF("NAME: ");
        for (int i = 0; i < filter_match->name.len; i++){
            LOG_INF("%c", filter_match->name.name[i]);
        }

        LOG_INF("\n");
    }

   

    //two options: perform a manual connect operation or allow the auto connect to work?
    if (connectable) {
        struct bt_conn *conn;
        err = bt_conn_le_create(device_info->recv_info->addr,
                                BT_CONN_LE_CREATE_CONN,
                                device_info->conn_param,
                                &conn);

        if (!err){
            if (!err) {
                default_conn = bt_conn_ref(conn);
                bt_conn_unref(conn);
            }
        }
    }
};

Why doesn't the match callback trigger? Am I suppose to set something else the match callback to trigger when I want multiple filters to be true?

Any guidance on this is much appreciated.

Thanks,

Harman

  • Hi,

    Why doesn't the match callback trigger?

    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_LBS);
    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, TARGET_DEVICE_NAME
    err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER | BT_SCAN_NAME_FILTER, true);

    I see you added UUID and Name to the scan filter and both filters must be matched before generating BT_SCAN_EVT_FILTER_MATCH when this flag is set to true. 

    So, I would suggest you to add logs to help debug to see if you can get the correct UUID and Name both as you want. 

    Regards,
    Amanda H.

Related