Good morning community,
This is my first post in the English section, and I don't want to seem rude for asking such a question.
I have already tried to muddle through here via the search and could not really find what I was looking for. Is probably also because I am the total noob.
Now to my question and problem.
I have a Bluetooth device which connects to an APP.
When I start this APP a profile is detected. There are two profiles
I would like to transfer the Bluetooth device to an ESP. What I have already managed, but the ESP always connects to the wrong second profile.
In the Arduino code I adjusted the UUID and the device name and the connection worked.
I think the Bluetooth device always starts with the first profile by default and the second one needs another query in the code.
Unfortunately I have no idea how the query for the second profile must look like, so that it switches to the profile, or connects immediately with the correct profile.
Maybe someone is so kind and can help me how the query must look like.
I attach the Arduino code I use. I made the UUIDs unrecognizable, for security, because I don't know if you are allowed to show them.
I was able to read more information from Wireshark from the original Bluetooth device. This must be the moment where the change to the second profile takes place.
I hope nobody feels embarrassed that I ask so directly.
Greetings
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <BLEService.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "xxxxxx60-xx00-xx22-xxx1-xxxxxxxxxxeb"
#define CHARACTERISTIC_UUID "xxxxxx61-xx00-xx22-xxx1-xxxxxxxxxxeb"
#define CHARACTERISTIC_UUID2 "xxxxxx62-xx00-xx22-xxx1-xxxxxxxxxxeb"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
BLEDevice::startAdvertising();
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("Name vom Bluetooth Gerät");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// notify changed value
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}
Here is the data from Wireshark