一,ESP32程序, 实现数据上传和接收
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// 使用你提供的UUID
#define SERVICE_UUID "0000fff0-0000-1000-8000-00805f9b34fb"
#define WRITE_CHARACTERISTIC_UUID "0000fff2-0000-1000-8000-00805f9b34fb"
#define NOTIFY_CHARACTERISTIC_UUID "0000fff1-0000-1000-8000-00805f9b34fb"
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pNotifyCharacteristic;
BLECharacteristic *pWriteCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
unsigned long previousMillis = 0;
const long interval = 2000; // 发送间隔2秒
// 自定义回调类,用于处理写入事件
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String value = pCharacteristic->getValue(); // 获取String类型的值
if (value.length() > 0) {
Serial.print("Received from APP: ");
Serial.println(value); // 直接打印String
// 如果需要转换为std::string
// std::string stdValue(value.c_str());
// 然后可以使用stdValue
}
}
};
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Device connected");
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Device disconnected");
// 重启广播以便重新连接
pServer->startAdvertising();
}
};
void setup() {
Serial.begin(115200);
// 初始化BLE设备
BLEDevice::init("ESP32-BLE");
// 创建BLE服务器实例
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// 添加服务到服务器
pService = pServer->createService(SERVICE_UUID);
// 创建可写的特征值并设置回调
pWriteCharacteristic = pService->createCharacteristic(
WRITE_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE
);
pWriteCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
// 创建带通知功能的特征值
pNotifyCharacteristic = pService->createCharacteristic(
NOTIFY_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
// 将Client Characteristic Configuration Descriptor添加到notify特征值上
pNotifyCharacteristic->addDescriptor(new BLE2902());
// 启动服务
pService->start();
// 开启广播让其他设备能发现我们
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("Waiting for a client connection...");
}
void loop() {
// 处理设备连接状态变化
if (!deviceConnected && oldDeviceConnected) {
delay(500); // 给蓝牙栈一个缓冲时间
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
// 如果设备已连接,定期发送"hello"
if (deviceConnected) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// 发送"hello"消息
String message = "hello";
pNotifyCharacteristic->setValue(message);
pNotifyCharacteristic->notify();
Serial.println("Sent: hello");
}
}
}
esp32接收来自app的数据
二,对接的APP源码只需要参考UUID即可
private static final String ecCharacteristicWriteUUID = "0000fff2-0000-1000-8000-00805f9b34fb";
private static final String ecCharacteristicNotifyUUID = "0000fff1-0000-1000-8000-00805f9b34fb";
APP端接收到的数据