Arduino.read()函数干扰BLE连接。
创始人
2024-09-12 15:31:13
0

对于在使用 Arduino 的时候出现的这种问题,可以考虑使用不同的解决方案来避免 .read() 函数干扰 BLE 连接。以下是一个代码示例:

#include 
#include 
#include 

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 0;

void setup() {
  Serial.begin(115200);

  // 创建 BLE 设备
  BLEDevice::init("MyBLE");

  // 创建 BLE 服务
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // 创建 BLE 特征值
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_READ |
                      BLECharacteristic::PROPERTY_WRITE
                    );

  pCharacteristic->addDescriptor(new BLE2902());

  // 开始服务
  pService->start();

  // 开始广播
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
}

void loop() {
  // Handle BLE disconnect
  if (!deviceConnected && BLEDevice::getConnections()->begin()) {
      BLEDevice::getConnections()->clear();
      deviceConnected = true;
  }

  // Handle BLE connect
  if (deviceConnected && !BLEDevice::getConnections()->begin()) {
      deviceConnected = false;
  }

  // Update characteristic value when device connected
  if (deviceConnected) {
      txValue++;
      pCharacteristic->setValue(&txValue, 1);
      pCharacteristic->notify();
      delay(10);
  }
}

可以看到,在这个示例中,我们没有使用 .read() 方法,而是使用了 BLECharacteristic 类的 setValue() 方法来设置 BLE 特征值,在 BLE 连接建立后使用 notify() 方法来将变更

相关内容

热门资讯

透视有挂!红龙poker透视,... 透视有挂!红龙poker透视,hhpoker真的有透视吗,软件教程(起初是有挂);大神普及一款德州a...
透视ai代打!德普之星app安... 透视ai代打!德普之星app安卓版破解版,德普之星有辅助软件吗,普及教程(固有有挂);德普之星app...
辅助透视!wpk脚本辅助器,w... 辅助透视!wpk脚本辅助器,wepoker软件靠谱么,爆料教程(起初有挂)关于wpk脚本辅助器的基本...
透视新版!aapoker辅助插... 透视新版!aapoker辅助插件工具,来玩app破解版,曝光教程(本然是有挂);aapoker辅助插...
透视最新!aapoker辅助挂... 透视最新!aapoker辅助挂,hhpoker辅助挂下载,我来教教你(固有有挂)1、许多玩家不知道a...
透视透视!智星菠萝辅助器,we... 透视透视!智星菠萝辅助器,wepoker游戏下载,插件教程(其实是真的有挂);超受欢迎的智星菠萝辅助...
透视系统!德普之星透视辅助,w... 透视系统!德普之星透视辅助,wpk插件,安装教程(起初真的有挂);是一款可以让一直输的玩家,快速成为...
透视苹果版!aapoker辅助... 透视苹果版!aapoker辅助器怎么用,wepoker透视app下载,透视教程(原生真的是有挂)1、...
透视好友!hhpoker透视脚... 透视好友!hhpoker透视脚本视频,德州辅助工具到底怎么样,必备教程(往昔真的是有挂);(需添加指...
透视存在!wepoker透视脚... 透视存在!wepoker透视脚本免费,pokemmo脚本手机版,透明挂教程(确实是真的有挂);wep...