Hallo,
Ich versuche mit einem ESP32 und der Arduino Entwicklungsumgebung Bluetoothdaten an eine Appinventor App zu senden und anzuzeigen.
Mein Problem: wenn ich den ESP mit meinem Smartphone verbinden möchte, kann ich die Geräte zwar Koppeln aber in den Bluetootheinstellungen des Smartphones wird die verbindung mit dem ESP nicht angezeigt.
In der nRF Connect App jedoch werden sowohl die Verbindung als auch die zu sendenden Daten angezeigt.
Währe sehr nett wenn mir jemand helfen könnte.
#include "sdkconfig.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
int txValue = 0;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer){
deviceConnected = true;
};
};
void setup() {
Serial.begin(9600);
BLEDevice::init("ESP32");
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_NO_BOND;
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
uint8_t key_size = 16;
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
pService->start();
pServer->getAdvertising()->start();
Serial.println("Warte auf Verbindung");
// put your setup code here, to run once:
}
void loop() {
if (deviceConnected){
txValue = random(-10, 20);
char txString[8];
dtostrf(txValue, 1, 2, txString);
pCharacteristic->setValue(txString);
pCharacteristic->notify();
Serial.println("Sent value: " + String(txString));
delay(500);
}
// put your main code here, to run repeatedly:
}