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

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino ESP32 BLE蓝牙和安卓端蓝牙数据交互实验相关的知识,希望对你有一定的参考价值。

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


调试工具

实例代码

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
 
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"); 
  
  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  
  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);
  
  // Create a BLE Characteristic
  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());
  
  // Start the service
  pService->start();
  
  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
  }
 
void loop() {
  if (deviceConnected) {
    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蓝牙和安卓端蓝牙数据交互实验的主要内容,如果未能解决你的问题,请参考以下文章

arduino ESP32 AndroidStudio BLE低功耗蓝牙 物联网

arduino ESP32 AndroidStudio BLE低功耗蓝牙 物联网

Android + ESP32 通过蓝牙 (BLE) 发送数据

esp32蓝牙功耗

ESP32 BLE蓝牙 微信小程序通信发送大于20字符数据

esp32不使用蓝牙助手连接蓝牙