【TI BLE】主從機資料互動過程-藍芽Notification方式
1、Notification與Indication的區別
GATT_Indication:
從機通知主機後,主機需要呼叫simpleprofile_writeattrcb,讀取從機的資料。
GATT_Notification:
從機直接傳送資料給主機。
從機和主機連線後,從手機端開啟indication的功能,從機會呼叫simpleprofile_writeattrcb去通過GATTServApp_ProcessCCCWriteReq函式配置當前功能是indication還是notification;
配置好後,GATT_Indication(有確認資訊)和GATT_Notification的區別就在於主機是否回confirm(確認)。
2、裝置通訊過程
前提需要確保主從機繫結連線,本篇只討論資料互動過程。
2.1、主機
2.1.1、傳送資料
uint8 sbpGattWriteString(uint8 *pBuffer, uint16 length)
{
uint8 status;
uint8 len;
// 資料包長度最大20位元組
if(length > 20)
len = 20;
else
len = length;
attWriteReq_t req;
req.handle = simpleBLECharHdl;
req.len = len;
req.sig = 0; //必須要填
req.cmd = 0; //必須要填
osal_memcpy(req.value,pBuffer,len);
// 傳送資料
status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );
return status;
}
2.1.2、接收資料
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;
}
}
static void simpleBLECentralProcessGATTMsg( gattMsgEvent_t *pMsg )
{
if ( simpleBLEState != BLE_STATE_CONNECTED )
{
return;
}
static int dataCount=0;
// 處理接收到的資料
if ( pMsg->method == ATT_HANDLE_VALUE_NOTI ||
pMsg->method == ATT_HANDLE_VALUE_IND )
{
attHandleValueNoti_t noti;
dataCount = dataCount+ 1;
// 獲取handle
noti.handle = pMsg->msg.handleValueNoti.handle;
noti.len = pMsg->msg.handleValueNoti.len;
// 將接收到的資料傳送到串列埠
osal_memcpy(¬i.value, &pMsg->msg.handleValueNoti.value,noti.len);
sbpSerialAppWrite(noti.value,noti.len);
}
......
else if ( simpleBLEDiscState != BLE_DISC_STATE_IDLE )
{
simpleBLEGATTDiscoveryEvent( pMsg );
}
}
2.2、從機
2.2.1、資料傳送函式
void sbpSerialAppSendNoti(uint8 *pBuffer,uint16 length)
{
uint8 len;
// 判斷資料包長度,最大為20位元組
if(length > 20)
len = 20;
else
len = length;
static attHandleValueNoti_t pReport;
pReport.handle=0x2E;
pReport.len = len;
osal_memcpy(pReport.value, pBuffer, len);
// 通過noti方式傳送資料
GATT_Notification( 0, &pReport, FALSE );
}
2.2.2、資料接收回調函式
// Simple GATT Profile Callbacks
static simpleProfileCBs_t simpleBLEPeripheral_SimpleProfileCBs =
{
simpleProfileChangeCB // Charactersitic value change callback
};
static void simpleProfileChangeCB( uint8 paramID )
{
uint8 newValue;
uint8 newValueBuf[20]={0};
switch( paramID )
{
case SIMPLEPROFILE_CHAR1:
// 獲取資料
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, newValueBuf );
// 傳送串列埠顯示
sbpSerialAppWrite (newValueBuf, 20);
break;
case SIMPLEPROFILE_CHAR3:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue );
break;
default:
break;
}
}