一个SimpleBLEPeripheral项目。用到了UART,蓝牙通信不是用的串口,CC2541实现蓝牙通信可以不用串口。
- <p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">attWriteReq_t req;</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">req.pValue = GATT_bm_alloc( simpleBLEConnHandle, ATT_WRITE_REQ, 1, NULL );
- if ( req.pValue != NULL )
- {
- req.handle = simpleBLECharHdl;
- req.len = 1;
- req.pValue[0] = simpleBLECharVal;
- req.sig = 0;
- req.cmd = 0;
- status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );</span></p>
复制代码
蓝牙主模块、CC2541连接成功。 CC2541往蓝牙主模块发送数据,蓝牙主模块是可以接收到的。 发送代码: - <p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">if(BL_Flag==9)</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">{</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">BL_Flag=0;</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">HAL_UART_Transmit (&huart1 ,(uint8_t *)(START),1,1500);</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">flag=1;</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">}</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">if(BL_Flag==1)</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">{</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">BL_Flag=0;</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">HAL_UART_Transmit (&huart1 ,(uint8_t *)(STO),1,1500);</span></p><p style="line-height: 28px; color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""><span style="font-size: 16px;">}</span></p>
复制代码
接收代码: - attWriteReq_t req;
- req.pValue = GATT_bm_alloc( simpleBLEConnHandle, ATT_WRITE_REQ, 1, NULL );
- if ( req.pValue != NULL )
- {
- req.handle = simpleBLECharHdl;
- req.len = 1;
- req.pValue[0] = simpleBLECharVal;
- req.sig = 0;
- req.cmd = 0;
- status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );
复制代码
|