nrf51822 --- 获取RSSI

1.目的

   从机连上主机后,获得RSSI信号强度

2.分析

  RSSI:Received Signal Strength Indication接收的信号强度指示

  在实际应用中可以通过RSSI来判断物体的距离,定位等。比如蓝牙防丢器就是通过RSSI来实现防丢的

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.14

硬件平台:nrf51822最小系统

例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4

4.步骤

首先,关于RSSI的API :

/**@brief Start reporting the received signal strength to the application. 
 *
 * A new event is reported whenever the RSSI value changes, until @ref sd_ble_gap_rssi_stop is called.
 *
 * @param[in] conn_handle        Connection handle.
 * @param[in] threshold_dbm      Minimum change in dBm before triggering the @ref BLE_GAP_EVT_RSSI_CHANGED event. Events are disabled if threshold_dbm equals @ref BLE_GAP_RSSI_THRESHOLD_INVALID.
 * @param[in] skip_count         Number of RSSI samples with a change of threshold_dbm or more before sending a new @ref BLE_GAP_EVT_RSSI_CHANGED event.
 *
 * @retval ::NRF_SUCCESS                   Successfully activated RSSI reporting.
 * @retval ::NRF_ERROR_INVALID_STATE       Disconnection in progress. Invalid state to perform operation.
 * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied.
 */
SVCALL(SD_BLE_GAP_RSSI_START, uint32_t, sd_ble_gap_rssi_start(uint16_t conn_handle, uint8_t threshold_dbm, uint8_t skip_count));


/**@brief Stop reporting the received signal strength.
 *
 * @note An RSSI change detected before the call but not yet received by the application 
 * may be reported after @ref sd_ble_gap_rssi_stop has been called.
 *
 * @param[in] conn_handle Connection handle.
 *
 * @retval ::NRF_SUCCESS                   Successfully deactivated RSSI reporting.
 * @retval ::NRF_ERROR_INVALID_STATE       Invalid state to perform operation.
 * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied.
 */
SVCALL(SD_BLE_GAP_RSSI_STOP, uint32_t, sd_ble_gap_rssi_stop(uint16_t conn_handle));


/**@brief Get the received signal strength for the last connection event.
 *
 * @param[in]  conn_handle Connection handle.
 * @param[out] p_rssi      Pointer to the location where the RSSI measurement shall be stored.
 *
 * @ref sd_ble_gap_rssi_start must be called to start reporting RSSI before using this function. @ref NRF_ERROR_NOT_FOUND
 * will be returned until RSSI was sampled for the first time after calling @ref sd_ble_gap_rssi_start.
 *
 * @retval ::NRF_SUCCESS                   Successfully read the RSSI.
 * @retval ::NRF_ERROR_NOT_FOUND           No sample is available.
 * @retval ::NRF_ERROR_INVALID_ADDR        Invalid pointer supplied.
 * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied.
 * @retval ::NRF_ERROR_INVALID_STATE       RSSI reporting is not ongoing, or disconnection in progress.
 */
SVCALL(SD_BLE_GAP_RSSI_GET, uint32_t, sd_ble_gap_rssi_get(uint16_t conn_handle, int8_t *p_rssi));
其中:sd_ble_gap_rssi_start();中参数的

 conn_handle:连接的handle 

 threshold_dbm :当设置为0的时候,每skip_count都会产生一个BLE_GAP_EVT_RSSI_CHANGED事件,当设置为1的时候连续skip_count个RSSI都超过+-1dbm就产生BLE_GAP_EVT_RSSI_CHANGED事件。

 skip_count:当设置为0的时候,每次超过+-threshold_dbm 都会产生BLE_GAP_EVT_RSSI_CHANGED事件,当设置x的时候,每x次都过threshold_dbm才产生BLE_GAP_EVT_RSSI_CHANGED事件。

实现代码:

在main.c

static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t                         err_code;
    
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
				//启动获取RSSI
				   sd_ble_gap_rssi_start(m_conn_handle,1,1);
            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            err_code = bsp_indication_set(BSP_INDICATE_IDLE);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            // Pairing not supported
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            // No system attributes have been stored.
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
            break;
      //获取RSSI
	case BLE_GAP_EVT_RSSI_CHANGED:
          rssi_value = p_ble_evt->evt.gap_evt.params.rssi_changed.rssi;
          break;  
				
        default:
            // No implementation needed.
            break;
    }
}


 以上是设置每2次只要超过+-1dbm就产生 
  BLE_GAP_EVT_RSSI_CHANGED 
  

注意:

  假如sd_ble_gap_rssi_start(m_conn_handle,0,0);每次获取RSSI都产生BLE_GAP_EVT_RSSI_CHANGED,那产  生BLE_GAP_EVT_RSSI_CHANGED事件的时间怎么设置呢?这个与蓝牙的连接参数有关系。交换数据的快,这个RSSI就快,慢就慢,




你可能感兴趣的:(nordic,nrf51822,蓝牙4.0)