首先由于芯片自身问题导致CC2541 可以最多一主三从 ,CC2640可以一主八从。
我们这里以CC2541为例子
1.扫描从机保存从机mac地址
//首先主机需要去扫描设备
GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_ACTIVE_SCAN, DEFAULT_DISCOVERY_WHITE_LIST )
//当扫描完成会进入
GAP_DEVICE_DISCOVERY_EVENT
//请查看
uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )
{
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) );
}
}
2.建立链接,通过上面的程序我们可以把从机的信息保存下来并保存链接句柄
uint8 addrType;
uint8 *peerAddr;
simpleBLEScanIdx = devMacResult[0];
// connect to current device in scan result
peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
simpleBLEState = BLE_STATE_CONNECTING;
GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,
addrType, peerAddr );
//在GAP_LINK_ESTABLISHED_EVENT事件中保存连接的handle:
MultiConnHandle[num]=ppEvent->linkCmpl.connectionHandle;
3.保存后的句柄可以用于数据的发送以及特征值的读写,连接的断开
attWriteReq_t req;
req.handle = BLE_NotifyChar_Handle+1;
req.len = 1;
req.value[0] = 0x01;
req.sig = 0;
req.cmd = 0;
status = GATT_WriteCharValue( MultiConnHandle[0], &req, simpleBLETaskId );
GAPCentralRole_TerminateLink( MultiConnHandle[0]);
4.附上TI提供的多路连接的例程链接: