蓝牙通信有个基本的功能,那就是使用蓝牙来进行数据通信。
数据通信可以分为:主机向从机发送数据(GATT的client主动向service发送数据);主机接收从机发来的数据;从机向主机发送数据(GATT的service主动向client发送数据);从机接收主机发送来的数据。
一、主机向从机发送数据
主机发送数据,调用的是clientAPI_AttribWrite( TClientID clientID, PWriteReq pWriteReq ) 函数,现在我们看看发送函数PWriteReq 的结构体;
typedef struct
{
uint16_t wHandle; /**< Handle of the attribute to be written */
TBlueAPI_GATTWriteType wType; /**< Write type, command or request */
uint16_t wLength; /**< Length of data to be written */
uint8_t *pData; /**< Data to be written */
} TWriteReq, * PWriteReq;
下面是使用发送函数例子:
TWriteReq writeReq;
clientAPI_AttribWrite( SimpClient_ClientID, &writeReq );
发送函数有两个参数,一个是client module,另外就是写结构体,调用次函数就可以将数据发送给从机了。当然,需要主机与从机连接后进行上述操作。
二、从机向主机发送数据
从机向主机发送数据,使用的是notification,RTL8762中使用的是如下发送函数:
ProfileAPI_SendData(uint8_t ServiceIdx,
uint16_t wAttribIndex,
uint8_t* pData,
uint16_t wLength)
需要正确写入函数里面的四个参数,