CC26X2获取RSSI方法二-连接事件获取法

5 篇文章 0 订阅
3 篇文章 0 订阅

  在Ti的蓝牙协议栈里面有这么一个结构体:

/**
 * Report describing connection event Returned via a @ref pfnGapConnEvtCB_t.
 */
typedef struct
{
  GAP_ConnEvtStat_t     status;   //!< status of connection event
  uint16_t              handle;   //!< connection handle
  uint8_t               channel;  //!< BLE RF channel index (0-39)
  GAP_ConnEvtPhy_t      phy;      //!< PHY of connection event
  int8_t                lastRssi; //!< RSSI of last packet received
  /// Number of packets received for this connection event
  uint16_t              packets;
  /// Total number of CRC errors for the entire connection
  uint16_t              errors;
  /// Type of next BLE task
  GAP_ConnEvtTaskType_t nextTaskType;
  /// Time to next BLE task (in us). 0xFFFFFFFF if there is no next task.
  uint32_t              nextTaskTime;
  uint16_t              eventCounter; // event Counter
  uint32_t              timeStamp;    // timestamp (anchor point)
  GAP_CB_Event_e        eventType;    // event type of the connection report.
} Gap_ConnEventRpt_t;

 这个结构体用于连接事件,可以看到里面有我们想要的成员:连接句柄,发出射频的通道以及相应通道上的RSSI。这个结构体类型有被一个回调函数类型使用,没错就是它:

typedef void (*pfnGapConnEvtCB_t)
(
  /// Pointer to report describing the connection event
  Gap_ConnEventRpt_t *pReport
);

 接下来我们要做的就是写这么一个回调函数,然后使用它的传参Gap_ConnEventRpt_t *pReport获取我们想要的RSSI值。回调之所以叫回调,我们就看看这个回调函数被谁注册了~可以搜到协议栈提供函数Gap_RegisterConnEventCb()注册回调函数。关于该函数的介绍如下:

/**
* Register/Unregister a connection event callback
*
* It is only possible to register for one connection handle of for all
* connection handles. In the case of unregistering, it does not matter what
* connHandle or cb is passed in as whatever is currently registered will be
* unregistered.
* It it possible to register to certain type of connection event. The types
* that are currently supported are: Connection established, PHY Update Request
* Completed (without PHY change).
*
* @warning The application owns the memory pointed to by pReport in
* @ref pfnGapConnEvtCB_t. That is, it is responsible for freeing this memory.
*
* @note The callback needs to be registered for each reconnection. It is not
* retained across a disconnect / reconnect.
* @note There is no need to unregister in order to change the type of connection
* event registered. It can be changed with a new call.
*
* @param cb Function pointer to a callback.
* @param action Register or unregister the callback.
* @param event Connection Event type to register to.
*              Supported event options: GAP_CB_CONN_ESTABLISHED, GAP_CB_PHY_UPDATE, GAP_CB_CONN_EVENT_ALL
* @param connHandle if @ref LINKDB_CONNHANDLE_ALL, apply to all connections. <br>
*        else, apply only for a specific connection.
*
* @return @ref SUCCESS
* @return @ref bleGAPNotFound : connection handle not found
* @return @ref bleInvalidRange : the callback function was NULL or action is
*         invalid
* @return @ref bleMemAllocError : there is not enough memory to register the callback.
*/
extern bStatus_t Gap_RegisterConnEventCb(pfnGapConnEvtCB_t cb,
                                         GAP_CB_Action_t action,
                                         GAP_CB_Event_e event,
                                         uint16_t connHandle);

  这个函数的作用就是将用户写的一个回调函数注册到GAP的连接事件上,每一次连接事件都会执行该函数,比如蓝牙设备之间建立连接之后会有一个连接间隔(比如45ms)保持连接,那么每45ms就会调用我们的回调函数。到此,我们便可以开始写下回调函数试试运行结果。下面是我随便写的回调:

static void BLE_vidConnEvtCB(Gap_ConnEventRpt_t *pMsg)
{
  Log_info2("connHandle %d, RSSI:%d", (uint32_t)(pMsg->handle), (uint32_t)(pMsg->lastRssi));
  ICall_free(pMsg);
}

 回调函数写好之后在APP的初始化处调用接口Gap_RegisterConnEventCb:

Gap_RegisterConnEventCb(BLE_vidConnEvtCB, GAP_CB_REGISTER, GAP_CB_CONN_EVENT_ALL, LINKDB_CONNHANDLE_ALL);

 编译运行,能看到串口不停的打印Rssi信息出来。最后顺便提一句lastRssi就是最后一次连接事件的射频Rssi(有可能携带数据,也有可能是空包)。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值