ESP32 BLE query issue

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


image


image

No one here who can help me ???
I am very thankful for any tip.

Hi, I've read your post a couple times but it's a bit hard (for me) to understand what you are asking. Maybe if you can clarify a few things we can figure this out. So maybe explain these points a bit more:

  1. What do you mean by "APP"? Are you refering to an Android/iPhone app?
  2. Can you explain what you mean by "profile"? Are you referring to the BLE characteristic UUIDs as different profiles or Bluetooth profiles of different devices or?
  3. "I would like to transfer the Bluetooth device to an ESP." Maybe you can try retranslating this? An ESP32 is already a Bluetooth device, so this sentence doesn't make sense.

PS: As for your delay for Bluetooth congestion, this is dependant on what is receiving the BLE data. Android for instance has a connectionPriority setting which can be changed to enable faster BLE updates if this important to you.

Hello, I am glad that someone has answered.

Ok. I'll try to explain it better again.
I have a Bluetooth PCB which communicates with an Android device.
I start the app on my Android device, the APP recognizes the Bluetooth PCB, but the PCB can two different profiles.

After a short time, the app switches to the correct profile that I need.

If I compile the sketch from my first post on my ESP then it works so far, but the APP stays on the wrong profile. It should switch to the correct profile a short time later.
I have also made a conversion of the PCB to the ESP

Actually, I do not need the change from profile 1 to profile 2 if you can program it so that the correct profile is used.

I think but I do not know exactly that it has something to do with the data from Wireshark. Possibly gatts and handle ?

I have one Servive UUID
And two Characteristics UUIDs

I hope I could express myself reasonably, and you understand what I mean, because my English is not the best.

Your English is good, it's only this is technical and detailed work.

So, if I understand correctly: The Android App recognizes the correct "profile" when you connect using the Bluetooth PCB, but only after a short time has passed. However, when the Android App connects to the ESP32, but it never recognizes the correct profile.

I apologize, I still don't understand what you mean by "profile" is here.

Also, what Android App is this?

Yes, that is exactly right.
I think there is a specific query missing in the ESP code, but I have no idea which one.

When the Android APP connects to the original Bluetooth PCB, a player card is loaded or detected.
It is detected whether player card 1 or player card 2.

I need the correct player card, but the ESP always gets the wrong one.

Sorry again to ask but you refer to this "profile" as a "player card". What exactly are we talking about here?

I agree with you there is probably something missing in the ESP32 code, but I still don't understand what this thing is. Are we talking about a physical piece of hardware? If so, can you link the item or something similiar to it?

You have a privat message

Here can be closed.

I have found my mistake.

There was a value missing in a clamp ( )

Thanks

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.