I'm at a loss. I thought the Nano ESP32 would be fun... Did not know BluetoothSerial would be unavailable (i.e. I can't use BluetoothClient, I now have to use BluetoothLE extension). I cannot for the life of me get a connection to the Arduino working, where I can simply pull some pins HIGH by clicking buttons on the app. Any advice greatly appreciated!
Search this forum for ESP32 and you will find plenty of examples.
Then, if you still have problems, explain why your project is different and what you have tried. Attach an .aia and maybe an .ino and there will be plenty of help.
Base blocks to connect esp32 via BLE
And code for esp32 for ArduinoIDE
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// Объявление глобальных переменных для BLE и светодиода
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t receivedValue = 0;
#define LED_PIN 8 // Пин светодиода
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
// Функция для мигания светодиода
void blinkLED(int times, int duration) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_PIN, HIGH);
delay(duration);
digitalWrite(LED_PIN, LOW);
delay(duration);
}
}
// Класс для обработки событий подключения и отключения
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
blinkLED(3, 300); // Три мигания при подключении
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
blinkLED(2, 300); // Два мигания при отключении
}
};
// Класс для обработки записи данных в характеристику
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
uint8_t *value = pCharacteristic->getData();
if (value) {
receivedValue = *value;
blinkLED(1, 100); // Одно мигание при получении данных
}
}
};
void setup() {
Serial.begin(115200);
// Настройка пина для светодиода
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Отключаем светодиод в начале
// Инициализация BLE устройства
BLEDevice::init("ESP32_BLE_Server");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Создание BLE сервиса и характеристики
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
pCharacteristic->addDescriptor(new BLE2902());
// Запуск сервиса и начало рекламирования
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// Проверка изменения состояния подключения
if (!deviceConnected && oldDeviceConnected) {
// Устройство было отключено, перезапускаем рекламирование
delay(500);
pServer->startAdvertising(); // Перезапуск рекламирования
Serial.println("Start advertising");
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
// Устройство было подключено
oldDeviceConnected = deviceConnected;
Serial.println("Device connected");
}
delay(10);
}
Please read all of the tips in the Topic linked to yours. You can download the Project file I supplied "BLE_connect_edit2.aia".
For example, how and in which order Permissions are requested is important. The power supply to the ESP32 is important. The environment is important for BLE to perform. Replace your Sketch with one only designed to achieve a connection for now - see 'Server.ino' on the linked Topic.