`今天搞了一下CC2541主机连接安森美从机。
CC2541主机是4.0,部分代码:
- /*********************************************************************
- * @fn simpleBLECentralEventCB
- *
- * [url=home.php?mod=space&uid=2666770]@Brief[/url] Central event callback function.
- *
- * [url=home.php?mod=space&uid=3142012]@param[/url] pEvent - pointer to event structure
- *
- * [url=home.php?mod=space&uid=1141835]@Return[/url] none
- */
- static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
- {
- switch ( pEvent->gap.opcode )
- {
- case GAP_DEVICE_INIT_DONE_EVENT:
- {
- LCD_WRITE_STRING( "BLE Central", HAL_LCD_LINE_1 );
- LCD_WRITE_STRING( bdAddr2Str( pEvent->initDone.devAddr ), HAL_LCD_LINE_2 );
- }
- break;
- case GAP_DEVICE_INFO_EVENT:
- {
- // if filtering device discovery results based on service UUID
- if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
- {
- // if ( simpleBLEFindSvcUuid( SIMPLEPROFILE_SERV_UUID,
- // pEvent->deviceInfo.pEvtData,
- // pEvent->deviceInfo.dataLen ) )
- {
- simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
- }
- }
- }
- break;
-
- 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) );
- }
- NPI_PrintString("Devices Found
- ");
- LCD_WRITE_STRING_VALUE( "Devices Found", simpleBLEScanRes,
- 10, HAL_LCD_LINE_1 );
- if ( simpleBLEScanRes > 0 )
- {
- LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );
- NPI_PrintString("<- To Select
- ");
- }
- // initialize scan index to last device
- simpleBLEScanIdx = simpleBLEScanRes;
- }
- break;
- case GAP_LINK_ESTABLISHED_EVENT:
- {
- if ( pEvent->gap.hdr.status == SUCCESS )
- {
- simpleBLEState = BLE_STATE_CONNECTED;
- simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle;
- simpleBLEProcedureInProgress = TRUE;
- NPI_PrintValue("连接句柄 ", simpleBLEConnHandle, 10);
- // If service discovery not performed initiate service discovery
- if ( simpleBLECharHdl == 0 )
- {
- osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY );
- }
- NPI_PrintString("Connected
- ");
- LCD_WRITE_STRING( "Connected", HAL_LCD_LINE_1 );
- LCD_WRITE_STRING( bdAddr2Str( pEvent->linkCmpl.devAddr ), HAL_LCD_LINE_2 );
- // 启动定时器发送数据
- //osal_start_timerEx( simpleBLETaskId, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );// 关键
-
- }
- else
- {
- simpleBLEState = BLE_STATE_IDLE;
- simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
- simpleBLERssi = FALSE;
- simpleBLEDiscState = BLE_DISC_STATE_IDLE;
-
- LCD_WRITE_STRING( "Connect Failed", HAL_LCD_LINE_1 );
- LCD_WRITE_STRING_VALUE( "Reason:", pEvent->gap.hdr.status, 10, HAL_LCD_LINE_2 );
- }
- }
- break;
- case GAP_LINK_TERMINATED_EVENT:
- {
- simpleBLEState = BLE_STATE_IDLE;
- simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
- simpleBLERssi = FALSE;
- simpleBLEDiscState = BLE_DISC_STATE_IDLE;
- simpleBLECharHdl = 0;
- simpleBLEProcedureInProgress = FALSE;
-
- LCD_WRITE_STRING( "Disconnected", HAL_LCD_LINE_1 );
- LCD_WRITE_STRING_VALUE( "Reason:", pEvent->linkTerminate.reason,
- 10, HAL_LCD_LINE_2 );
- }
- break;
- case GAP_LINK_PARAM_UPDATE_EVENT:
- {
- LCD_WRITE_STRING( "Param Update", HAL_LCD_LINE_1 );
- }
- break;
-
- default:
- break;
- }
- }
复制代码
CC2541串口打印数据:
一开始运行,安美森板子指示灯闪烁,当连接上以后,就不闪了。
实物图:
`
|