This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Getting the rssi value (on peripheral side) of a phone connected to the peripheral

Dear Sir,

I have connected a phone to a peripheral device using NRFconnect App.

Now I want to know the rssi value of the phone on the peripheral side. 

I want to get the peer address(of the phone) and corresponding rssi value.

I have done the following code for the same.

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO("Connected hrs.");
            NRF_LOG_INFO_ARRAY(p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr,BLE_GAP_ADDR_LEN);
            NRF_LOG_INFO_ARRAY(peak_rssi.peer_addr.addr,BLE_GAP_ADDR_LEN);
           
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);
            sd_ble_gap_rssi_start(m_conn_handle,1,0);
            
            break;

        case BLE_GAP_EVT_RSSI_CHANGED:
        NRF_LOG_INFO("In BLE_GAP_EVT_RSSI_CHANGED ");
        c_rssi = p_ble_evt->evt.gap_evt.params.rssi_changed.rssi;
        c_addr = p_ble_evt->evt.gap_evt.params.connected.peer_addr;
        NRF_LOG_INFO_ARRAY(p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr,BLE_GAP_ADDR_LEN);
        NRF_LOG_INFO("RSSI = %d ",c_rssi);
        NRF_LOG_INFO("In BLE_GAP_EVT_RSSI_CHANGED End");
        break;

But here two cases are there 

1)case BLE_GAP_EVT_CONNECTED:

2)case BLE_GAP_EVT_RSSI_CHANGED:

In both these cases I am printing the variable  "p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr".(as I want to get the peer address of the phone connected to the peripheral)

In  case BLE_GAP_EVT_CONNECTED: it shows the same address ( after multiple connect-disconnect operations)

But in case BLE_GAP_EVT_RSSI_CHANGED the same variable is changing continuously (even if the device is connected to a phone).

So which will give the correct peer address of the phone?

Since the first one shows almost the stable one, I think I can relay on that. Am I correct?

Then the problem is which variable will give me the correct rssi value of the phone connected to the peripheral?

Please guide me in this and please give answers to 3 questions mentioned above.

Thank you 

  • Why do you need to know the peer address?

    A BLE (non mesh) connection is only ever 1-to-1 - so there is no need to distinguish different peers in the connection?

  • I am trying to implement the following scenario.

    One periheral

    Multiple phones.

    Each phone tries to connect to the peripheral.

    I want to connect to the one with highest rssi value.

    I was planning to do the following

    Once the advertising start , the initial 30 seconds I will use to collect the addresses of the phones (whoever tries to connect to the peripheral) and their rssi values. (here i will do connect-disconnect operation to get the details, for 30 secs(for example i am taking this value))

    Then sort it and select the one with highest rssi. 

    After 30 seconds when everyone tries to connect I will check the address of each one and will allow only the one whose adddress match with the selected one ( which has the highest rssi value).

    Will you please tell me if there is any other ways to tackle this scenario(if my approach is not good)?

  • Hi,

    In the BLE_GAP_EVT_RSSI_CHANGED case, p_ble_evt->evt.gap_evt.params.connected.peer_addr doesn't contain the data you think it is. 

    Please have a look at the `ble_gap_evt_t` structure and note how unions are used to store different information for different events, but in the same place in memory:

    /**@brief GAP event structure. */
    typedef struct
    {
      uint16_t conn_handle;                                     /**< Connection Handle on which event occurred. */
      union                                                     /**< union alternative identified by evt_id in enclosing struct. */
      {
        ble_gap_evt_connected_t                   connected;                    /**< Connected Event Parameters. */
        ble_gap_evt_disconnected_t                disconnected;                 /**< Disconnected Event Parameters. */
        ble_gap_evt_conn_param_update_t           conn_param_update;            /**< Connection Parameter Update Parameters. */
        ble_gap_evt_sec_params_request_t          sec_params_request;           /**< Security Parameters Request Event Parameters. */
        ble_gap_evt_sec_info_request_t            sec_info_request;             /**< Security Information Request Event Parameters. */
        ble_gap_evt_passkey_display_t             passkey_display;              /**< Passkey Display Event Parameters. */
        ble_gap_evt_key_pressed_t                 key_pressed;                  /**< Key Pressed Event Parameters. */
        ble_gap_evt_auth_key_request_t            auth_key_request;             /**< Authentication Key Request Event Parameters. */
        ble_gap_evt_lesc_dhkey_request_t          lesc_dhkey_request;           /**< LE Secure Connections DHKey calculation request. */
        ble_gap_evt_auth_status_t                 auth_status;                  /**< Authentication Status Event Parameters. */
        ble_gap_evt_conn_sec_update_t             conn_sec_update;              /**< Connection Security Update Event Parameters. */
        ble_gap_evt_timeout_t                     timeout;                      /**< Timeout Event Parameters. */
        ble_gap_evt_rssi_changed_t                rssi_changed;                 /**< RSSI Event Parameters. */
        ble_gap_evt_adv_report_t                  adv_report;                   /**< Advertising Report Event Parameters. */
        ble_gap_evt_sec_request_t                 sec_request;                  /**< Security Request Event Parameters. */
        ble_gap_evt_conn_param_update_request_t   conn_param_update_request;    /**< Connection Parameter Update Parameters. */
        ble_gap_evt_scan_req_report_t             scan_req_report;              /**< Scan Request Report Parameters. */
        ble_gap_evt_phy_update_request_t          phy_update_request;           /**< PHY Update Request Event Parameters. */
        ble_gap_evt_phy_update_t                  phy_update;                   /**< PHY Update Parameters. */
        ble_gap_evt_data_length_update_request_t  data_length_update_request;   /**< Data Length Update Request Event Parameters. */
        ble_gap_evt_data_length_update_t          data_length_update;           /**< Data Length Update Event Parameters. */
      } params;                                                                 /**< Event Parameters. */
    } ble_gap_evt_t;
    

    This means that on a BLE_GAP_EVT_RSSI_CHANGED event, you cannot expect to find information about a BLE_GAP_EVT_CONNECTED in the p_ble_evt variable. The value at p_ble_evt->evt.gap_evt.params.connected.peer_addr is old at best, and total garbage at worst. 

    Regarding how to get RSSI and peer address from nearby phones I see that you got help on that here: https://devzone.nordicsemi.com/f/nordic-q-a/38148/connecting-the-ble-device-to-a-phone-app-with-highest-signal-strength/

  • HI Martin, Thank you for your time and reply.

    Yes I got the help. But still I am facing issues with the implementation of whitelist.

    I have referred https://devzone.nordicsemi.com/f/nordic-q-a/18608/whitelisting-in-s132-v3-for-advertising-filter-policy/71827#71827 this also.

    But still I am facing some issues.

    I have posted query here .

    https://devzone.nordicsemi.com/f/nordic-q-a/38488/issue-in-implementing-whitelist 

    Please give me some guidance if possible.

    Thanking you in advance,

    with regards,

    Geetha

  • A colleague of mine will help you out in the other thread. 

Related