Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验

Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验


  • ESP32低功耗(BLE)蓝牙实验.
  • ✨根据esp32技术规格书上描述,esp32芯片的蓝牙协议栈支持蓝牙 v4.2 BR/EDR(传统蓝牙) 和 Bluetooth LE 标准。

调试工具

  • 安卓系统BLE调试工具:BLEAssist
  • APP下载地址:http://www.wch.cn/downloads/BLEAssist_ZIP.html
    Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验_第1张图片
  • 将APP应用程序发给到手机上,然后安装上去。
  • 打开APP后,需要开启蓝牙定位两个功能。

实例代码

#include 
#include 
#include 
#include 
 
BLECharacteristic *pCharacteristic;
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"
 
class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };
 
    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};
 
class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();
 
      if (rxValue.length() > 0) {
        Serial.print("------>Received Value: ");
 
        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);
  
  // Create the BLE Device
  BLEDevice::init("ESP32 BLE Test"); 
  
  // 创建蓝牙服务器
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  
  // // 创建广播服务的UUID
  BLEService *pService = pServer->createService(SERVICE_UUID);
  
  创建广播服务的UUID
  pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
                   
  pCharacteristic->addDescriptor(new BLE2902());
  
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
  
  pCharacteristic->setCallbacks(new MyCallbacks());
  
  // 开始蓝牙服务
  pService->start();
  
  // 开始广播
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
  }
 
void loop() {
  if (deviceConnected) {//设备连接后,每秒钟发送txValue。
    memset(BLEbuf, 0, 32);
    memcpy(BLEbuf, (char*)"Hello BLE APP!", 32); 
    pCharacteristic->setValue(BLEbuf);
    
    pCharacteristic->notify(); // Send the value to the app!
    Serial.print("*** Sent Value: ");
    Serial.print(BLEbuf);
    Serial.println(" ***");
  }
  delay(1000);
}


  • 代码烧录成功后,手机端打开蓝牙调试助手-会自动搜索蓝牙设备-点击connect进行连接
    Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验_第2张图片
  • 给蓝牙设备被发送消息
    Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验_第3张图片
  • 内容信息
    Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验_第4张图片
  • 串口调试窗口打印收到的信息

Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验_第5张图片

你可能感兴趣的:(Arduino,ESP32,入门实例教程,esp32蓝牙)