主機(jī)例子中沒有 找到現(xiàn)成的發(fā)送例子,在論壇上找了些資料看了能發(fā)送成功,但是發(fā)著發(fā)著就會(huì)出問題,再也發(fā)送不了數(shù)據(jù)了。發(fā)送程序如下:
void BleMasterSend(u8 *Data,u8 Len)
{
??? attWriteReq_t req;
??? u8 Res;
??? if(centralProcedureInProgress == FALSE)
??? {
??????? centralDoWrite=TRUE;
??????? req.cmd=FALSE;
??????? req.sig=FALSE;
??????? req.handle=centralCharHdl;
??????? req.len=Len;
??????? req.pValue=GATT_bm_alloc(centralConnHandle, ATT_WRITE_REQ, req.len, NULL, 0);
??????? if(req.pValue!=NULL)
??????? {
??????????? memcpy(req.pValue,Data,Len);
??????????? Res=GATT_WriteCharValue(centralConnHandle,&req,centralTaskId);
??????????? if(Res==SUCCESS)
??????????? {
??????????????? centralProcedureInProgress=TRUE;??? //翻轉(zhuǎn)讀寫狀態(tài)
??????????????? centralDoWrite=!centralDoWrite;???? //翻轉(zhuǎn)讀寫狀態(tài)
??????????????? tmos_start_task(centralTaskId,START_READ_OR_WRITE_EVT, DEFAULT_READ_OR_WRITE_DELAY);? //
??????????????? print(DEBUG_BLE,"\n Master Write ok");
??????????? }
??????????? else
??????????? {
??????????????????? print(DEBUG_BLE,"\n Master Write faild=%x",Res);
??????????? }
??????????? GATT_bm_free((gattMsg_t*)&req,ATT_WRITE_REQ);
??????? }
??????? else
??????? {
??????????????? print(DEBUG_BLE,"\n alloc Faild");
??????? }
??? }
??? else
??? {
??????????? print(DEBUG_BLE,"\n Flag True");
??? }
}
從打印信息看,主要是GATT_WriteCharValue 發(fā)送超時(shí)。
①發(fā)送成功不需要手動(dòng)釋放緩存,協(xié)議棧底層會(huì)進(jìn)行釋放;
②你這里將centralProcedureInProgress標(biāo)志取反,然后沒有再置1,if判斷不滿足;
③打印GATT_WriteCharValue的返回值,并作為判斷是否執(zhí)行到write函數(shù)處。如果是0x16,則重復(fù)過10ms調(diào)用發(fā)送函數(shù)。
void BleMasterSend(u8 *Data,u8 Len)
{
??? attWriteReq_t req;
??? u8 Res;
??? if(centralProcedureInProgress==TRUE)
??? {
??????? print(DEBUG_BLE,"\n Busy");
??? }
??? else
??? {
??????? req.cmd=FALSE;
??????? req.sig=FALSE;
??????? req.handle=centralCharHdl;
??????? req.len=Len;
??????? req.pValue=GATT_bm_alloc(centralConnHandle, ATT_WRITE_REQ, req.len, NULL, 0);
??????? if(req.pValue!=NULL)
??????? {
??????????? memcpy(req.pValue,Data,Len);
??????????? Res=GATT_WriteCharValue(centralConnHandle,&req,centralTaskId);
??????????? if(Res==SUCCESS)
??????????? {
??????????????? centralProcedureInProgress=TRUE;??? //翻轉(zhuǎn)讀寫狀態(tài)
??????????????? print(DEBUG_BLE,"\n Master Write ok");
??????????? }
??????????? else
??????????? {
??????????????? print(DEBUG_BLE,"\n Master Write faild=%x",Res);
??????????????? GATT_bm_free((gattMsg_t*)&req,ATT_WRITE_REQ);
??????????? }
??????? }
??????? else
??????? {
??????????????? print(DEBUG_BLE,"\n alloc Faild");
??????? }
??? }
}
我現(xiàn)在是 GATT_WriteCharValue() 這個(gè)寫入ok,但是如果在centralProcessGATTMsg()中沒有打印Write success 那么后續(xù)發(fā)送GATT_WriteCharValue() 都是返回超時(shí),所以應(yīng)該是發(fā)送失敗的情況下,底層某個(gè)地方需要處理下。
GATT_WriteCharValue這個(gè)函數(shù)返回值為0,代表數(shù)據(jù)傳入底層。
write success代表數(shù)據(jù)成功發(fā)送數(shù)據(jù)。
如果傳入底層成功,但是沒有成功發(fā)送。那可能是藍(lán)牙丟包導(dǎo)致,協(xié)議棧會(huì)進(jìn)行重復(fù)傳輸。直到底層數(shù)據(jù)傳輸完成。如果藍(lán)牙斷開,則此包數(shù)據(jù)丟失。
針對你上面的情況,一直沒有write success,建議查看一下緩存是否夠用。寫一個(gè)tmos任務(wù),查詢一下協(xié)議棧剩余的大小。
查詢BLE協(xié)議棧剩余RAM字節(jié)數(shù):
PRINT("memory_len = %d\n",tmos_memory_getlen());
調(diào)用前添加聲明:uint32_t tmos_memory_getlen(void);