[ESP32]ESP32 Arduino BLE调试 / 与安卓蓝牙数据交互

帖子导航

[ESP32]ESP32 Arduino开发环境搭建

首先得有支持包吧

ESP32 BLE支持包

进入arduino IDE -> 文件 -> 首选项 -> 项目文件位置
到该路径下,打开git工具,项目地址 https://github.com/espressif/arduino-esp32.git
git命令git clone https://github.com/espressif/arduino-esp32.git
等到下载完成之后进入tools文件夹 -> 点击get.exe进行安装

不会使用git工具的,可以参考一下教你如何备份代码之——版本管理工具Git;

测试上位机

用的BLE Utility APP工具,大家可以上各大安卓市场进行下载安装

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);
}

测试结果

能够和手机进行数据交互,能使用BLE进行数据收发
在这里插入图片描述
在这里插入图片描述

refer: https://www.instructables.com/id/ESP32-BLE-Android-App-Arduino-IDE-AWESOME/

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页