RSSI value through the BLE connection process in nRF connect SDK

Hi Nordic,

I am evaluating the nrf52833 DK with nrf connect SDK. I am using by dev kit here as a central devie and I want to get the RSSI values of the periherral devices through the BLE connection process. Here I don't want to initiate the scanning process, so I am connecting to that device by hard coding the MAC addresses in my code. Here I want to get the RSSI signal strength of the peripheral devices in the connection process. How do I do that in nrf connect SDK?

Thanks,

Pranathi.

  • Hi Pranathi,

    To be clear, there is no way to connect without scanning. When you excplicitly attemt to connect to a specific BLE address, what happens is that scanning is started, and when you receive a connectable advertising packet from the specified address, the BLE stack will connect to it.

    With that out of the way, to obtain the RSSI in a connection you can use BT_HCI_OP_READ_RSSI. You can find an example of that in read_conn_rssi() in zephyr/samples/bluetooth/hci_pwr_ctrl/src/main.c.

    Einar

  • Hi Einar,

    I am explicitely able to connect to the BLE device with my Dev kit without scanning. And to read RSSI, I have added the function read_conn_rssi() in my code and called it.

    I am getting the Read RSSI error as -5, which is internally mentioned as I/O error. 

    CODE:

    static void read_conn_rssi(uint16_t handle, int8_t *rssi)
    {
    	struct net_buf *buf, *rsp = NULL;
    	struct bt_hci_cp_read_rssi *cp;
    	struct bt_hci_rp_read_rssi *rp;
    
    	int err;
    
    	buf = bt_hci_cmd_create(BT_HCI_OP_READ_RSSI, sizeof(*cp));
    	if (!buf) {
    		printk("Unable to allocate command buffer\n");
    		return;
    	}
    
    	cp = net_buf_add(buf, sizeof(*cp));
    	cp->handle = sys_cpu_to_le16(handle);
    
    	err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_RSSI, buf, &rsp);
    	if (err) {
    		uint8_t reason = rsp ?
    			((struct bt_hci_rp_read_rssi *)rsp->data)->status : 0;
    		printk("Read RSSI err: %d reason 0x%02x\n", err, reason);
    		return;
    	}
    
    	rp = (void *)rsp->data;
    	*rssi = rp->rssi;
    
    	net_buf_unref(rsp);
    }
    
    static void connected(struct bt_conn *conn, uint8_t err)
    {
        char addr[BT_ADDR_LE_STR_LEN];
    
    	
         
        bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
        if (err) {
            printk("Failed to connect to %s (%u)\n", addr, err);
    
            bt_conn_unref(default_conn);
            default_conn = NULL;
    	}
    	else
    	{
    
    	}
    
        if (conn != default_conn) {
            return;
        }
        printk("\nConnected: %s\n", addr);
    	int8_t rssi = 0xFF;
    	// printk("\nRSSI:%d\n",rssi);
    	read_conn_rssi(default_conn_handle, &rssi);
    	printk("Connected (%d) - RSSI = %d\n",default_conn_handle, rssi);
    }

    OUTPUT:

    How do I solve it?

    Thanks,

    Pranathi

  • Hi Einar,

    I was trying in the meanwhile I added the prj.conf 

    CONFIG_BT_CTLR_ADVANCED_FEATURES=y
    CONFIG_BT_CTLR_CONN_RSSI=y
    Now I am getting a constant value of 127 as the RSSI value.
    Atleast I am not getting the read error anymore....but am I missing something so I am only getting a 127 constant value every time I run the program?

    Thanks,

    Pranathi

  • Hi,

    You cannot read the RSSI in the connected callback. You need to wait until it has returned and call it later. See this thread for details.

  • Hi Einar,

    I tried to call it in the main loop as per the thread discussion, but I guess I didn't get it right and I am confused on what exactly to do, can you explain me clearly how to do it?

Related