Bluetooth connection help for ESP32

Hello
I'm trying to make a bluetooth connection. Between the computer and the ESP32WROOM model controller
I burned a code on it that opens a Bluetooth object, the computer recognizes it but does not bring up the option to connect, unlike other devices available to me, for this connection there is not even a "connect" button
Maybe someone knows what the problem is?

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

const int ledPin = 16;
const int ledPin1 = 18;
void setup() {
  pinMode(ledPin1, OUTPUT);
  digitalWrite(ledPin1, HIGH);
  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);

  if (!SerialBT.begin("ESP32_LED")) {
    Serial.println("An error occurred initializing Bluetooth");
  } else {
    Serial.println("Bluetooth initialized");
  }
  Serial.println("The device started, now you can pair it with Bluetooth!");
}

void loop() {
  digitalWrite(ledPin1, HIGH);
  if (SerialBT.available()) {
    char incomingChar = SerialBT.read(); 
    Serial.print("Received: ");
    Serial.println(incomingChar);

    if (incomingChar == '1') {
      digitalWrite(ledPin, HIGH);
      SerialBT.println("LED is ON");
    }

    else if (incomingChar == '0') {
      digitalWrite(ledPin, LOW);
      SerialBT.println("LED is OFF");
    }
    else {
      SerialBT.println("Invalid command");
    }
  }
}

Please do not post pictures of code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

This is because you are using the damn :sweat_smile: BluetoothSerial in order to emulate a classic serial port.

Your operating system should enumerate an additional BT serial port and you have to use it as usual.
image

By the way, for controlling a LED this is a non-sense as my opinion... you should define your device as a BLE device exposing an ON/OFF LED control characteristic and possibly also another one for level (if dimmable).

1 Like

Can you explain me more?
:upside_down_face:This is the first time I set up a Bluetooth connection myself.

Well! Let's leave the boring work to AI and recap how BLE works:

Bluetooth Low Energy (BLE) is a wireless communication protocol designed for low power consumption, making it ideal for battery-powered devices like fitness trackers, smartwatches, and sensors.

Here's a simplified overview of how BLE works:

  1. Advertising: BLE devices broadcast small packets of data called advertisements, which contain information about the device and its capabilities. These advertisements are sent periodically on specific advertising channels.
  2. Scanning: Other BLE devices, like smartphones or tablets, listen for these advertisements and can then initiate a connection with the advertising device.
  3. Connection and Data Exchange: Once connected, devices can exchange data using a structured format called Generic Attribute Profile (GATT). GATT organizes data into services and characteristics:
  • Service: A collection of related characteristics that represent a specific functionality or feature of the device (e.g., Heart Rate Service, Battery Service).
  • Characteristic: A specific piece of data within a service (e.g., Heart Rate Measurement, Battery Level). Characteristics can be read, written, or notified (sent periodically).

Analogy:

Think of a BLE device as a restaurant.

  • Services are like the different sections of the menu (appetizers, main courses, desserts).
  • Characteristics are like the individual items on the menu (Caesar salad, steak, cheesecake).

When you connect to a BLE device, you're essentially browsing its menu to see what it offers and then ordering specific items (reading or writing characteristics).

Regarding the start code, you can find all inside the ESP32 Arduino Core starting by:

BLE/examples/Write/Write.ino

or also:
BLE/examples/Server/Server.ino

In the Arduino world, peripherals are also referred to as "servers".
I have always found this to be confusing, because in my opinion BLE is not a client-server protocol (the same device can be both Peripheral and Central at the same time).

The person who wrote the code for me is of course GPT........
And yet, according to you, an essential part of the definitions is missing.
I should mention that I returned questions to him about the fact that the connection was not created properly and he was unable to solve it for me,
It turns out that you can't just trust him, but you also need to know how things are built.
I would appreciate it if you could tell me what the missing part is
Thanks

I'm not sure I understood your request 100%, however since these problems with BLE are quite widespread due to the terrible tutorials found online (which always and only do everything using a Serial over Bluetooth), some time ago I had already prepared an example that covers some of the most common needs plus a very basic Android APP made with MIT App Inventor.

esp32-ble-peripheral-mitAI.zip (234.9 KB)

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