【Beetle ESP32 C6迷你开发板】--3.蓝牙串口服务
<div class='showpostmsg'> 本帖最后由 dirty 于 2024-5-17 23:08 编辑<p> 本篇讲述开发板创建蓝牙串口服务。</p>
<p><strong><span style="color:#0000cc;">一.代码准备</span></strong></p>
<p>1.蓝牙服务UUID、串口收发属性UUID宏定义</p>
<pre>
<code>#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"</code></pre>
<p>2.串口初始化、蓝牙服务创建、串口属性创建、启动服务、启动蓝牙广播,这其中注册了蓝牙连接与断开回调,写接收回调</p>
<pre>
<code>class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.print("BLE Connected\r\n");
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
Serial.print("BLE Disconnected\r\n");
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********\r\n");
Serial.print("Received Values: \r\n");
for (int i = 0; i < rxValue.length(); i++)
Serial.print(rxValue);
Serial.println();
Serial.println("*********\r\n");
}
}
};
void setup() {
Serial.begin(115200);
Serial.print("UART Init ");
// Create the BLE Device
BLEDevice::init("UART Service");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
Serial.println();
Serial.print("Start the service");
// Start advertising
pServer->getAdvertising()->start();
Serial.println();
Serial.println("Waiting a client connection to notify...");
}</code></pre>
<p>3.主函数,未连接时广播,连接后使能时通知</p>
<pre>
<code>void loop() {
if (deviceConnected) {
pTxCharacteristic->setValue(&txValue, 1);
pTxCharacteristic->notify();
txValue++;
delay(1000); // bluetooth stack will go into congestion, if too many packets are sent
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println();
Serial.println("start advertising\r\n");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}</code></pre>
<p> </p>
<p><strong><span style="color:#0000cc;">二编译烧录测验</span></strong></p>
<p>1.IDE工具设置。USB CDC On Boot: 选Enabled: 通过USB接口打印串口数据,否则USB串口没有输出</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图1:USB串口设置</div>
<p>2.蓝牙广播如下</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图2:蓝牙广播</div>
<p>3.蓝牙串口服务及收发属性UUID</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图3:蓝牙串口服务及收发属性UUID</div>
<p>4.在nRF Connect工具点击连接,可看到蓝牙连接事件触发,断开亦如此。在RX Characteristic属性Write写数据,可看到设备收到数据。完整日志如下</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图4:蓝牙串口日志</div>
<p> 至此,蓝牙串口服务功能演示实现。</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]