天天看點

esp32 ble 入門

主要通信的資料在

Characteristic就叫特征

esp32 ble 入門

,特征的值就是我們有交易的資料,

服務下有特征,根據特征值手機和ESP32就能進行資料的通信

下面圖有一個服務和一個特征,服務有uuid,特征有uuid,

在特征内部我們可以讀寫值達到資料通信的目的

手機調試下載下傳URL:

https://apksfull.com/download/no.nordicsemi.android.mcp/3691357

esp32 ble 入門

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開發