本篇文章记录使用CC2642作外设,连接手机,获取手机蓝牙的RSSI信号强度。本次的实验是在ProjectZero例程基础上编写实现的,我的思路是打算周期性的读取RSSI值,所以我会先定义一个定时器,然后再定时器的回调事件中去发送读RSSI的命令。本实验获取RSSI的步骤如下:
// Internal Events for RTOS application
#define PZ_ICALL_EVT ICALL_MSG_EVENT_ID // Event_Id_31
#define PZ_APP_MSG_EVT Event_Id_30
#define CUSTOM_TIMER_EVT Event_Id_11
#define CUSTOM_TIMER_EVT_PERIOD 1000//定时器循环周期,单位:ms
/*定义结构体.....................*/
static Clock_Struct structCustomClock;
/*初始化定时器...................*/
Util_constructClock(&structCustomClock,
(void*) test_customclockHandler,
CUSTOM_TIMER_EVT_PERIOD, 0 ,false,
CUSTOM_TIMER_EVT);
static void test_customclockHandler(UArg param)
{
Util_startClock(&structCustomClock);//重启定时器
Event_post(syncEvent,param);//时间一到发送事件
}
在函数ProjectZero_taskFxn中加入一下的语句:
if(events & CUSTOM_TIMER_EVT)
{
uint16_t connectionHandle = 0;
//这里我暂时把connectionHandle设置成0,表示现在想要获取的是0号设备的Rssi
//调用下边的函数成功的话会有HCI的完成事件
VOID HCI_ReadRssiCmd(connectionHandle);
}
static void ProjectZero_processCmdCompleteEvt(hciEvt_CmdComplete_t *pMsg)
{
uint8_t status = pMsg->pReturnParam[0];
//Find which command this command complete is for
switch(pMsg->cmdOpcode)
{
case HCI_READ_RSSI:
{
int8 rssi = (int8)pMsg->pReturnParam[3];
// Display RSSI value, if RSSI is higher than threshold, change to faster PHY
if(status == SUCCESS)
{
uint16_t handle = BUILD_UINT16(pMsg->pReturnParam[1],
pMsg->pReturnParam[2]);
Log_info2("RSSI:%d, connHandle %d",
(uint32_t)(rssi),
(uint32_t)handle);
} // end of if (status == SUCCESS)
break;
}
......
}
}
上面的这个Read Rssi命令的完成处理时例程写好的,这里只是贴出来记录下而已。
本实验是获取蓝牙连接设备的RSSI值,所以我把定时器的开启函数放在了蓝牙建立连接的case上:
static void ProjectZero_processGapMessage(gapEventHdr_t *pMsg)
{
......
case GAP_LINK_ESTABLISHED_EVENT:
{
gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;
Util_startClock(&structCustomClock);
// Display the amount of current connections
Log_info2("Link establish event, status 0x%02x. Num Conns: %d",
pPkt->hdr.status,
linkDB_NumActive());
......
}