主要通信的数据在
Characteristic就叫特征,特征的值就是我们有交易的数据,
服务下有特征,根据特征值手机和ESP32就能进行数据的通信
下面图有一个服务和一个特征,服务有uuid,特征有uuid,
在特征内部我们可以读写值达到数据通信的目的
手机调试下载URL:
https://apksfull.com/download/no.nordicsemi.android.mcp/3691357
esp32:调式代码
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID “4fafc201-1fb5-459e-8fcc-c5c9c331914b”
#define CHARACTERISTIC_UUID “beb5483e-36e1-4688-b7f5-ea07361b26a8”
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.print("\r\nNew value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
}
}
};
void setup() {
Serial.begin(9600);
BLEDevice::init(“ESP32 BLE example”);
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue(“Hello World”);
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println(“hi junjun”);
delay(100);
}
在这个不是用IDF的开发,而用arduino开发
主要开发环境在pio