今天搞了一下蓝牙,和CC2541板子连接。
CC2541主机,沁恒板子是从机。CC2541发数据,沁恒板子收数据。
代码:
CC2541:
if ( keys & HAL_KEY_UP )
{
// Start or stop discovery
NPI_PrintString(" [KEY UP pressed!]\r\n");
//if (connectedPeripheralNum < MAX_PERIPHERAL_NUM)
{
if ( !simpleBLEScanning )//不在扫描中则进行扫描
{
simpleBLEScanning = TRUE;
simpleBLEScanRes = 0;
LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
LCD_WRITE_STRING( "", HAL_LCD_LINE_3 );
LCD_WRITE_STRING( "", HAL_LCD_LINE_4 );
GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
DEFAULT_DISCOVERY_ACTIVE_SCAN,
DEFAULT_DISCOVERY_WHITE_LIST );
}
else // 如果在扫描中,则停止扫描, 这个时候会返回已扫描到的设备
{
GAPCentralRole_CancelDiscovery();
}
}
}
if ( keys & HAL_KEY_CENTER ) //中间按键
{
#if 1// 连续自动连接已扫描到的从机
LCD_WRITE_STRING("Connecting......", HAL_LCD_LINE_8);
simpleBLEUpdateCnt = 0;
simpleBLESendValue = 0;
simpleBLEScanIdx = 0;
performPeriodicTask_AutoConnect(); // 执行动连接
osal_start_timerEx( simpleBLETaskId, SBP_AUTO_CONNECT_EVT, SBP_PERIODIC_EVT_AUTO_CONNECT );
HalLedBlink (HAL_LED_1, 1, 50, 1000);//这个的意思是, 1000ms内,以50%的占空比闪烁1次, 实际就是点亮50ms
#else
uint8 addrType;
uint8 *peerAddr;
BLE_DEV *p = &(gDev[simpleBLEScanIdx]);
NPI_PrintString(" [KEY_CENTER]\r\n");
NPI_PrintValue("Idx=", simpleBLEScanIdx, 10);
NPI_PrintValue("State=", p->simpleBLEState, 10);
NPI_PrintValue("num=", connectedPeripheralNum, 10);
NPI_PrintValue("res=", simpleBLEScanRes, 10);
// Connect or disconnect
//if ( p->simpleBLEState == BLE_STATE_IDLE
// || connectedPeripheralNum < MAX_PERIPHERAL_NUM)
if ( p->simpleBLEState == BLE_STATE_IDLE )
{
// if there is a scan result
if ( simpleBLEScanRes > 0
&& simpleBLEScanIdx < simpleBLEScanRes )
{
// connect to current device in scan result
peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
p->simpleBLEState = BLE_STATE_CONNECTING;
//osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
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 ( p->simpleBLEState == BLE_STATE_CONNECTING ||
p->simpleBLEState == BLE_STATE_CONNECTED ||
p->simpleBLEState == BLE_STATE_DISCONNECTING)
{
// disconnect
p->simpleBLEState = BLE_STATE_DISCONNECTING;
gStatus = GAPCentralRole_TerminateLink( p->simpleBLEConnHandle );
LCD_WRITE_STRING( "Disconnecting", HAL_LCD_LINE_1 );
}
#endif
}
// smartRF开发板上的 S1 对应我们源码上的HAL_KEY_SW_6
// 这里设置成发送数据功能了
if ( keys & HAL_KEY_SW_6 )
{
char str[32] = {0};
sprintf(str, "Sent CHAR to PHER.");
HalLcdWriteString(str, HAL_LCD_LINE_5 );
simpleBLEScanIdx = 0;
// 启动定时器发送数据
osal_start_timerEx( simpleBLETaskId, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );// 关键
LCD_WRITE_STRING("Key DOWN = Read RSSI", HAL_LCD_LINE_8);
}
沁恒板子:
static void simpleProfileChangeCB( uint8 paramID )
{
uint8 newValue;
switch( paramID )
{
case SIMPLEPROFILE_CHAR1:
//PRINT("profile ChangeCB CHAR1..\n");
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue );
PRINT("profile ChangeCB CHAR1:%d\n",newValue);
break;
case SIMPLEPROFILE_CHAR3:
PRINT("profile ChangeCB CHAR3..\n");
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue );
break;
default:
// should not reach here!
break;
}
}
效果图:
|