bluetooth sample central_uart, how to assign UUID value of scan filter at run time?

Hi,

ncs v2.2.0, sample central_uart. 

It define NUS UUID as scan filter to connect peripheral_uart.

If I want to change the UUID value for scan filter to another value (not nus UUID) at run time,

can you  guide me how to do it?

	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE);

  • Hi Felix, 

    To change UUID filter, you can simply call bt_scan_filter_remove_all(), and then add all the necessary scan filters back.

    To avoid unwanted callback during the short time the filters are not setup correctly after the remove_all function, it is a good idea to temporarily stop the scan before the remove_all call, then start it again after the filters are setup.

    Hieu

  • Hi Hieu,

    I am confused which structure type should I declare at run time to store UUID value and used as bt_scan_filter_add() parameter?

    If I declare struct bt_uuid_128 type, it seems not correct.

    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE);

  • Hi Felix,

    I got it to work with a setup like this:

    #define BT_UUID_NUS_VAL \
    	BT_UUID_128_ENCODE(0x6e400001, 0xb5a3, 0xf393, 0xe0a9, 0xe50e24dcca9e)
    	
    static void setup_filter_1() {
    	bt_scan_filter_remove_all();
    	static struct bt_uuid_128 nus_uuid = {
    		.uuid = {.type = BT_UUID_TYPE_128},
    		.val = {BT_UUID_NUS_VAL}
    	};
    	uint32_t err_code = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, &nus_uuid);
    	if (err_code != 0) {
    		printk("bt_scan_filter_add %d\n", err_code);
    		while(1); // Infinite loop trap for debugging purpose. Do not use in actual production code :)
    	}
    }

    It makes use of the utility macro BT_UUID_128_ENCODE provided by uuid.h. Depends on your use-case, you might want to use the equivalent for 16-bit or 32-bit UUID. Of course, you could also just write your entire UUID as literal byte/char/uint8_t array.

Related