1.主机发送数据方式
调用 GATT_WriteCharValue( 0, &AttReq, simpleBLETaskId );
eg: attWriteReq_t AttReq;
simpleBLEChar6DoWrite = FALSE;
AttReq.handle = simpleBLECharHd6;
AttReq.len = numBytes;
AttReq.sig = 0;
AttReq.cmd = 0;
osal_memcpy(AttReq.value,buf,numBytes);
GATT_WriteCharValue( 0, &AttReq, simpleBLETaskId );
2.从机发送数据方式
(1)使用SimpleProfile_SetParameter ,这种速度慢
eg: simpleBLEChar6DoWrite2 = FALSE;
SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR6,numBytes, buf );
(2)使用 GATT_Notification ,这种速度快
eg: static attHandleValueNoti_t pReport;
pReport.len = numBytes;
pReport.handle = 0x0035;
osal_memcpy(pReport.value, buf, numBytes);
GATT_Notification( 0, &pReport, FALSE );
3.主机接收数据
当有系统事件 SYS_EVENT_MSG时,读取消息调用pMsg =osal_msg_receive( )提取消息,
如果消息中有pMsg->event==GATT_MSG_EVENT事件时,如果pMsg->method == ATT_HANDLE_VALUE_NOTI
则表示有从机消息到。由于系统每次循环都能查询些事件,所以从机使用NOTIFICATION方法发送数据,主机响应速度会更快。而从机使用修改Charactoristic会慢些。
eg:
if ( events & SYS_EVENT_MSG )
{
uint8 *pMsg;
if ( (pMsg = osal_msg_receive( simpleBLETaskId )) != NULL )
{
simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
// Release the OSAL message
VOID osal_msg_deallocate( pMsg );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
static void simpleBLECentral_ProcessOSALMsg( osal_event_hdr_t *pMsg )
{
switch ( pMsg->event )
{
case KEY_CHANGE:
simpleBLECentral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );
break;
case GATT_MSG_EVENT:
simpleBLECentralProcessGATTMsg( (gattMsgEvent_t *) pMsg );
break;
}
if ( ( pMsg->method == ATT_HANDLE_VALUE_NOTI ) ) //通知
{
if( pMsg->msg.handleValueNoti.handle == 0x0035) //CHAR6的通知 串口打印
{
NPI_WriteTransport(pMsg->msg.handleValueNoti.value, pMsg->msg.handleValueNoti.len );
}
}
}
4.从机接收数据
从机通过在初始化时注册的一个GATT Profile Callbacks函数做接收到数据时的处理。回调函数会得到指示当时哪个Charactor变化的参数。
VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs );
// Simple GATT Profile Callbacks
static simpleProfileCBs_t simpleBLEPeripheral_SimpleProfileCBs =
{
simpleProfileChangeCB // Charactersitic value change callback
};
static void simpleProfileChangeCB( uint8 paramID )
{
uint8 newValue;
uint8 newChar6Value[SIMPLEPROFILE_CHAR6_LEN];
uint8 returnBytes;
switch( paramID )
{
case SIMPLEPROFILE_CHAR1:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue, &returnBytes );
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10, HAL_LCD_LINE_3 );
#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
break;
case SIMPLEPROFILE_CHAR3:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue, &returnBytes );
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10, HAL_LCD_LINE_3 );
#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
break;
case SIMPLEPROFILE_CHAR6:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR6, newChar6Value, &returnBytes );
if(returnBytes > 0)
{
NPI_WriteTransport(newChar6Value,returnBytes);
}
break;
default:
// should not reach here!
break;
}
}