【发布时间】:2021-10-11 22:23:01
【问题描述】:
这是我在这个板上提出的第一个问题
项目简介:
5 个传感器,与 esp32 板相连,每秒传输 1000 个样本,每个样本有 16 位。这些值应通过 BLE 传输(使用 BLE Arduino 库和 ESP32)。连接的设备(智能手机)应该读取这些值并对其进行处理(也通过 BLE,使用以下库:https://github.com/RobotPajamas/Blueteeth)。 ESP32 是服务器! Java 用于 Android Studio!
问题:
在测试 BLE 连接时,会传输一个简单的“hello world”作为特征值。每次我在 android-device 端收到“hello world”时,都会增加一个变量:问题是,该变量在一秒钟内只增加了 4 次。这意味着(假设 1 个字符在等于 1 字节的字符串中)正在传输 11byte*4(1/s)=44byte/s。 -> 这显然是不够的(BLE 不应该传输 ~2MBit/s(减去协议数据))
代码片段
ESP32:传输价值的BLE-Server
#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.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MyESP32");
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:
delay(2000);
}
Android Studio 代码(接收源代码片段):
try
{
while(sampleBluetoothData)
{
this.selectedDevice.readCharacteristic(MainActivity.characteristicUUID, MainActivity.serviceUUID, (response, data) ->
{
if (response != BlueteethResponse.NO_ERROR) {
return;
}
Log.d("AUSGANG", new String(data) + "times: "+ i);
i++;
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
ESP32 端的写入是 Arduino IDE 的空白示例代码,Android 端的读取由 BLE-Library 发布者进行。是的,Log.d 会影响性能,但不会降低太多。
Android代码的变量“data”是接收到的char-array。蓝牙读取在后台线程上运行。
我现在问自己的问题:
- 问题是 Android-Studio 库还是 Arduino 库
- 这是一种正常行为吗,如果特征值没有改变,它的传输速度会很慢。
- 更新特征值的速度有多快
提前致谢!
【问题讨论】:
-
您应该使用特征通知而不是特征读取。
标签: android arduino bluetooth bluetooth-lowenergy esp32