nRF Connect Check if bonded

How do I check to see if a peripheral is inside the list of bonded units of a central?

I know I can just scan -> see unit -> connect -> change security and if it IS bonded then it will not try to attempt a re-pairing. It will use the stored keys. However, i want ot prevent either making a connection by something being broadcasted in the advertisement i an scan and compare. Or if i have to connect, I would like to check to see if it is paired BEFORE i do a change security.

Trying to implement a means to pair on request and then auto-connect to devices already bonded. I am not seeing anything available. 

What i do right now for pair/bonding, I advertise either a 0 or a 1 in manufacturer data as a pair bit. if the central sees a 1, then establish a connection and pair/bond with device. If it sees a 0, it should check to see if the device is in its bond list and then connect if it is.

I have not gone thru the process of establishing a whitelist. There are many ways of doing this by establishing my own whitelist. but i was hoping, if i am bonding, the device knows it is bound to an object, i should be able to get that same info prior to attempting to connect and change security.

I have a bunch of very code intensive methods i can think of to solve this, but i am hoping there is just an isBonded() i can call of sorts.

Parents
  • I found something. I do not know if it is good. Maybe someone can offer some constructive criticism on if this is a proper way to accomplish what I want. It does work. not sure if elegant or proper bluetooth methodology

    I have a function check_bonds. it compares 2 bt_addr_le_t.addr elements. (i assume these are unique identifiers for all bonded units.) I then call bt_foreach_bond() where i want to check if bonded before calling for the connection. The advertising data i am inspecting is not looking to pair, only connect if bonded. So if a result from the check bonds function returns true, then go ahead and make a connection with the advertising device.

    void check_bonds(const struct bt_bond_info *info, void *user_data){
    	struct bond_checker *bc = user_data;
    
    	bt_addr_le_t *addr = &bc->addr;
    	bt_addr_le_t *dest = &info->addr;
    
    	if(!bt_addr_le_cmp(dest, addr)){
    		printk("Bond Found\n");
    		bc->found = true;
    	}
    }
    

    bc.addr = (*addr);
    bc.found = false;
    bt_foreach_bond(BT_ID_DEFAULT, check_bonds, &bc);
    
    if(bc.found){
    	err = bt_le_scan_stop();
    
    	if (err) {
    		printk("Stop LE scan failed (err %d)\n", err);
    		break;
    	}
    	param = BT_LE_CONN_PARAM_DEFAULT;
    	err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
    				param, &default_conn);
    	if (err) {
    		printk("Create conn failed (err %d)\n", err);
    		start_scan();
    	}
    
    	return false;
    }

Reply
  • I found something. I do not know if it is good. Maybe someone can offer some constructive criticism on if this is a proper way to accomplish what I want. It does work. not sure if elegant or proper bluetooth methodology

    I have a function check_bonds. it compares 2 bt_addr_le_t.addr elements. (i assume these are unique identifiers for all bonded units.) I then call bt_foreach_bond() where i want to check if bonded before calling for the connection. The advertising data i am inspecting is not looking to pair, only connect if bonded. So if a result from the check bonds function returns true, then go ahead and make a connection with the advertising device.

    void check_bonds(const struct bt_bond_info *info, void *user_data){
    	struct bond_checker *bc = user_data;
    
    	bt_addr_le_t *addr = &bc->addr;
    	bt_addr_le_t *dest = &info->addr;
    
    	if(!bt_addr_le_cmp(dest, addr)){
    		printk("Bond Found\n");
    		bc->found = true;
    	}
    }
    

    bc.addr = (*addr);
    bc.found = false;
    bt_foreach_bond(BT_ID_DEFAULT, check_bonds, &bc);
    
    if(bc.found){
    	err = bt_le_scan_stop();
    
    	if (err) {
    		printk("Stop LE scan failed (err %d)\n", err);
    		break;
    	}
    	param = BT_LE_CONN_PARAM_DEFAULT;
    	err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
    				param, &default_conn);
    	if (err) {
    		printk("Create conn failed (err %d)\n", err);
    		start_scan();
    	}
    
    	return false;
    }

Children
Related