[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/

  • 9
    点赞
  • 116
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
ESP32Arduino之间的蓝牙通信可以通过使用ESP32作为蓝牙服务器和Arduino作为蓝牙客户端来实现。以下是一个简单的例子,演示了如何在ESP32Arduino之间进行蓝牙通信: 1. 首先,需要在ESP32上启用蓝牙服务器功能。可以使用Arduino IDE和ESP32BLE库来实现。以下是一个简单的代码片段,演示了如何在ESP32上启用蓝牙服务器功能: ```c++ #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; uint8_t txValue = 0; #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void setup() { Serial.begin(115200); BLEDevice::init("ESP32_BLE_Server"); pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); BLEService *pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); pCharacteristic->addDescriptor(new BLE2902()); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->addServiceUUID(pService->getUUID()); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); Serial.println("Waiting for a client connection to notify..."); } void loop() { if (deviceConnected) { pCharacteristic->setValue(&txValue, 1); pCharacteristic->notify(); txValue++; delay(10); // bluetooth stack will go into congestion, if too many packets are sent } if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising Serial.println("start advertising"); oldDeviceConnected = deviceConnected; } if (deviceConnected && !oldDeviceConnected) { // do stuff here on connecting oldDeviceConnected = deviceConnected; } } ``` 2. 接下来,需要在Arduino上编写蓝牙客户端代码。以下是一个简单的代码片段,演示了如何在Arduino上连接到ESP32蓝牙服务器并发送数据: ```c++ #include <SoftwareSerial.h> SoftwareSerial BTSerial(10, 11); // RX | TX void setup() { Serial.begin(9600); BTSerial.begin(9600); // HC-05 default speed in AT command more } void loop() { if (BTSerial.available()) { Serial.write(BTSerial.read()); } if (Serial.available()) { BTSerial.write(Serial.read()); } } ``` 在这个例子中,Arduino使用SoftwareSerial库将数据发送到ESP32蓝牙服务器。可以使用Serial Monitor来查看从ESP32返回的数据
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值