simpleBLECentral想做成自动连接
您好,我用TI例程simpleBLECentral做修改,在按下HAL_KEY_UP,搜索到合适的蓝牙从机后,原例程是按下HAL_KEY_LEFT,显示有几个设备得到了连接,再按下HAL_KEY_CENTER才建立连接。我现在不想按左键了,而是直接搜索完成设备后就去与当前设备列表的从机去连接,修改如下:
static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
{
。。。。
case GAP_DEVICE_DISCOVERY_EVENT:
{
// discovery complete
simpleBLEScanning = FALSE;
// if not filtering device discovery results based on service UUID
if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
{
// Copy results
simpleBLEScanRes = pEvent->discCmpl.numDevs;
osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList,
(sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
}
LCD_WRITE_STRING_VALUE( "Devices Found", simpleBLEScanRes,
10, HAL_LCD_LINE_1 );
if ( simpleBLEScanRes > 0 )
{
LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );
}
// initialize scan index to last device
simpleBLEScanIdx = simpleBLEScanRes;
//3秒后建立连接
osal_start_timerEx( simpleBLETaskId, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
}
break;
。。。。
}
然后在uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )里
新增定时事件:
if(events & SBP_PERIODIC_EVT)
{
/***************************************************************/
uint8 addrType;
uint8 *peerAddr;
// Connect or disconnect
if ( simpleBLEState == BLE_STATE_IDLE )
{
// if there is a scan result
if ( simpleBLEScanRes > 0 )
{
// connect to current device in scan result
peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
//NPI_WriteTransport ( peerAddr, osal_strlen(peerAddr));
addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
simpleBLEState = BLE_STATE_CONNECTING;
GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,
addrType, peerAddr );
LCD_WRITE_STRING( "Connecting", HAL_LCD_LINE_1 );
LCD_WRITE_STRING( bdAddr2Str( peerAddr ), HAL_LCD_LINE_2 );
}
}
else if ( simpleBLEState == BLE_STATE_CONNECTING || simpleBLEState == BLE_STATE_CONNECTED )
{
// disconnect
simpleBLEState = BLE_STATE_DISCONNECTING;
gStatus = GAPCentralRole_TerminateLink( simpleBLEConnHandle );
LCD_WRITE_STRING( "Disconnecting", HAL_LCD_LINE_1 );
}
/***************************************************************/
return ( events ^ SBP_PERIODIC_EVT );
} 原封不动复制的HAL_KEY_CENTER处代码,
现在的现象是程序能正常走完,但是主机却显示连接的是0x000000000;从机显示未连接,事实也确实没连接,请问这是为什么?
想做还没做,帮顶
//simpleBLEScanIdx = simpleBLEScanRes; //屏蔽此行
simpleBLEScanIdx =0; //增加
试试。
问题应该是simpleBLEDevList下标问题。
照您说的,问题确实解决了,我该好好想想就好了,谢谢