gpt4 book ai didi

ios - 如何从 ios 应用程序通过 BLE 打开连接到 arduino 的 LED?

转载 作者:行者123 更新时间:2023-11-29 05:34:09 33 4
gpt4 key购买 nike

这是场景。我有一个 esp32、2 个 LED 和一个在网上找到的 ios 示例应用程序 here 。目前,如果我按下 esp32 上的按钮,它会通知 ios 应用程序显示“1”,如果再次按下,则会显示“0”。这非常有效。

棘手的部分是这个 ios 应用程序允许我向 esp32 发送写入命令。我想做到这一点,如果发送“1”,LED A 打开,LED B 关闭,然后当发送 0 时,LED A 关闭,LED B 打开。但我无法做到这一点。尽我所能,我无法弄清楚这个项目链中哪里出了问题。也许是 esp32 上的代码,或者也许是我不确定的应用程序。

这是我的arduino代码。 (还有更多未提及的代码,我实际上有 4 个 LED,但我只想在发送写入命令时打开 2 个特定的 LED)。

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
boolean oldState = LOW;
uint32_t value = 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;
}
};

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("1") != -1) {
digitalWrite(13, HIGH);
digitalWrite(27, LOW);
}
else if (rxValue.find("0") != -1) {
digitalWrite(13, LOW);
digitalWrite(27, HIGH);
}
}
}
};

const int bt1 = 14;
boolean bt1g = true;
int bt1t = 0;


void setup() {
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);
pinMode(33, OUTPUT);
pinMode(27, OUTPUT);
pinMode(bt1, INPUT_PULLUP);
Serial.begin(9600);

BLEDevice::init("ESP32");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);

pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);

pCharacteristic->addDescriptor(new BLE2902());

pService->start();

BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}

void loop()
{


if (bt1g) {
if (digitalRead(bt1) == LOW ) {
bt1t = (bt1t + 1) % 2;
Serial.println(bt1t);
bt1g = false;

}
}
if (!bt1g) {
if (digitalRead(bt1) == HIGH) {
bt1g = true;
}
}

if (bt1t == 0) {
digitalWrite(15, LOW);
digitalWrite(33, HIGH);

}
}

boolean newState = digitalRead(15);

if (deviceConnected) {
if (newState != oldState) {
if (newState == LOW) {
pCharacteristic->setValue("1");

}
else {
pCharacteristic->setValue("0");

}
pCharacteristic->notify();
};

oldState = newState;

}
delay(50);
}

看起来 ios 应用程序的整个代码太长,无法提交到这篇文章,所以 here是github

我真的很不确定并且陷入困境。如有任何帮助,我们将不胜感激!

最佳答案

难道你没有发现正确的特征吗?看来您只有一项服务和一项特征。

关于ios - 如何从 ios 应用程序通过 BLE 打开连接到 arduino 的 LED?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225895/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com