請問CC2541 central與peripheral連接的動作functions
我先匯整一下我的想法,central與peripheral要連接,應是
1.peripheral端發送廣播
2.central收到,送scan request
3.peripheral收到,送scan response
4.central端收到,發起connect request
在我的系統中,central端與peripheral端的MAC address我均已知,於是我在central端內宣告一array peripheral[6] ={0x01,0x02,0x03,0x04,0x05,0x06},
並在case GAP_DEVICE_INFO_EVENT: 不管掃到甚麼UUID,我均添加,
simpleBLEScanIdx=0;
simpleBLEAddDeviceInfo( &(PeripheralMACaddr[0]), 0x01 );
而peripheral只有單純執行廣播。
接著我透過改寫performPeriodicTask()如下並設置斷點,其GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,addrType, peerAddr );確認有被執行到,可仍然無法連接起來。
static void performPeriodicTask( void )
{
uint8 addrType;
uint8 *peerAddr;
BLE_DEV *p = &(gDev[simpleBLEScanIdx]);
if(ConnectFlag){
if ( p->simpleBLEState == BLE_STATE_IDLE ){
peerAddr = simpleBLEDevList[0].addr;
addrType = simpleBLEDevList[0].addrType;
p->simpleBLEState = BLE_STATE_CONNECTING;
GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,
addrType, peerAddr );
}
else {
GAPCentralRole_GetParameter(GAPCENTRALROLE_BD_ADDR,&(MACaddr[0])); //get MAC-self
osal_memcpy(&MACaddrPower[0],&MACaddr[0],6);
OneConnetedDevice_WriteCharX(simpleBLEScanIdx, BLE_CHAR6, (uint8*)MACaddrPower, 7);
}
}
但我改用一button 中斷,發送connect request,如下,卻有時候可以連接起來,我不明白這到底是哪邊出了狀況。
if ( keys & HAL_KEY_SW_6 )
{
uint8 addrType;
uint8 *peerAddr;
BLE_DEV *p = &(gDev[simpleBLEScanIdx]);
peerAddr = simpleBLEDevList[0].addr;
addrType = simpleBLEDevList[0].addrType;
p->simpleBLEState = BLE_STATE_CONNECTING;
GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,
addrType, peerAddr );
}
Huazhen Jiang,
periodic task里面循环发送连接请求的方式是不好的。
如果要做自动连接,比较合理的方法是在你搜索到你需要的设备的时候,发起连接就行,只要发一次连接请求就行了。 搜索到设备是在
case GAP_DEVICE_INFO_EVENT: , 这里可以比较地址和发起连接。