可拖拽配置蓝牙控制app
如果觉得有用,麻烦点赞收藏,您的支持是博主创作的动力。
1. 前言
写app也快10年了,最近在玩玩esp32 ble相关知识点。对于物联网来说,经常有各种控制UI页面,那么可不可以说弄一个拖拽的简单app。说干就干!!!!!
app下载链接,0积分下载,亲测可用
BLE调试助手APP
2. 参考资料
使用到了一个网友开源的拖拽框架,在这基础上修改了部分代码达到现在的效果。
3. App源码
需要app源码同学直接留言邮箱,见到处理。
4. ESP32 BLE测试代码
把以下代码烧录到ESP32-C3或者有BLE功能的ESP32即可。
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLECharacteristic *pCharacteristicRead;
BLECharacteristic *pCharacteristicNotify;
BLECharacteristic *pCharacteristicWrite;
BLEServer *pServer;
bool deviceConnected = false;
char BLEbuf[32] = {0};
uint32_t cnt = 0;
#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"
#define CHARACTERISTIC_UUID_READ "6E400003-B5A3-F393-E0A9-E50E24DCCA9F"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyWriteCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.print("------>Received Value: ");
Serial.println(rxValue.c_str());
// for (int i = 0; i < rxValue.length(); i++) {
// Serial.print(rxValue[i]);
// }
Serial.println();
if (rxValue.find("A") != -1) {
Serial.print("Rx A!");
}
else if (rxValue.find("B") != -1) {
Serial.print("Rx B!");
}
Serial.println();
}
}
};
void setup() {
Serial.begin(115200);
// 创建一个ble设备
BLEDevice::init("BLE_TEST");
BLEDevice::setMTU(100);
// 创建一个ble服务器
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// 创建一个ble服务
BLEService *pService = pServer->createService(SERVICE_UUID);
// 创建一个ble特征,用于通知对方
pCharacteristicNotify = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
// 添加一个特征描述符
pCharacteristicNotify->addDescriptor(new BLE2902());
// 创建一个只读ble特征
pCharacteristicRead = pService->createCharacteristic(CHARACTERISTIC_UUID_READ, BLECharacteristic::PROPERTY_READ);
memset(BLEbuf, 0, 32);
memcpy(BLEbuf, (char*)"只读数据!", 32);
pCharacteristicRead->setValue(BLEbuf);
// 创建一个ble特征,用于收到对方数据
pCharacteristicWrite = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
pCharacteristicWrite->setCallbacks(new MyWriteCallbacks());
// 启动服务器
pService->start();
// 启动广播
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
Serial.print("MTU:");
Serial.println(BLEDevice::getMTU());
}
void loop() {
if (deviceConnected) {
memset(BLEbuf, 0, 32);
memcpy(BLEbuf, (char*)"Hello BLE APP!", 32);
pCharacteristicNotify->setValue(BLEbuf);
pCharacteristicNotify->notify(); // Send the value to the app!
Serial.print("*** Sent Value: ");
Serial.print(BLEbuf);
Serial.println(" ***");
} else {
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
delay(5000);
}
5. Arduino测试结果
整体过程是通路的,后面即可在上面进行业务开发。