Skip to Content

How To Connect ESP32 Bluetooth With A Smartphone

How To Connect ESP32 Bluetooth With A Smartphone

The ESP32 is a powerful microcontroller with wireless capabilities. In this article, I will show you how easy it is to connect a smartphone with Bluetooth to an ESP32 module over Bluetooth. 

I will take you through the Bluetooth capabilities of the ESP32 module, basic parameters you should know and some exciting applications you can build using the Bluetooth feature of the ESP32 microcontroller.

I will discuss Bluetooth connection in general and ESP32 Bluetooth capabilities.

Later, I will take you through a step-by-step procedure to connect an ESP32 module to a Bluetooth smartphone with Bluetooth. 

In the end, you will have a working code and an example project to use for your next project as a reference.

You will have a good understanding of ESP32 Bluetooth capabilities and be able to use it in your projects confidently. 

Let’s get started.

(I also have an article on How To Use Bluetooth with Arduino using an HC-05 module if you want to work with an Arduino microcontroller instead.

Components Needed To Build ESP32 And Bluetooth Smartphone Project

Hardware Components

Software

Guide

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.


Basics of The ESP32 Bluetooth Capabilities

I am sure you have heard of and used Bluetooth before.

Basics of The ESP32 Bluetooth Capabilities

You turn the Bluetooth on your phone, and it can automatically list all available Bluetooth devices.

Have you ever wondered how that happens? How does Bluetooth work? What makes it so famous and easy to use?

This section will discuss the basics of Bluetooth protocol and ESP32 Bluetooth capabilities.

Bluetooth is a short-range communication protocol. When two devices are connected over Bluetooth, you can stream video/audio, control devices and send files. 

Bluetooth communicates wirelessly at a frequency range of 2.4 GHz (2.402 GHz to 2.480 GHz). The protocol can create multiple channels spaced at 1 MHz.

Multiple channels help the protocol avoid frequency contention when various devices are involved. 

Did you know?

There are multiple Bluetooth classes based on the power level of the transponders. 

ClassMax Permitted PowerPower in dBm
110-100 mW+10 to +20 dBm
21 – 2.5 mW0 to +4 dBm
30.01 – 1 mW-20 to 0 dBm

There are several Bluetooth versions available. For example, 

  • Bluetooth 1.0
  • Bluetooth 1.1
  • Bluetooth 1.2
  • ..
  • Bluetooth 5.1
  • Bluetooth 5.2..

And more. 

Let’s see what version of Bluetooth is present in ESP32.

ESP32 C3, ESP32 S3, and ESP32 S3-mini support Bluetooth LE v5.0. 

Older ESP32 modules support Bluetooth LE v4.2 version.

What is LE next to the Bluetooth name?

The Bluetooth protocol supports the Low Energy feature. Since the explosion of IoT and wearable devices, there has been a need to improve the battery life of Bluetooth devices and wearables.

Bluetooth smart

So, the Bluetooth association came up with new hardware architecture and software stack entirely focussing on low power consumption, compromising the range of communication.

Are there any differences between Bluetooth Classic and Bluetooth Low Power energy?

Yes. You can find the differences between classic Bluetooth and Bluetooth low-power energy.

ParameterConventional BluetoothLow Energy Bluetooth
CommunicationBidirectional and ContinuousShort data transfers, unidirectional
RangeUp to 100 mLess than 100 m
Energy ConsumptionUp to 1 W10 to 500 mW
Data rate1 – 3 Mbits/s125 kbits/s – 2 Mbits/s
Latency100 ms6 ms
Voice capabilityYesNo

Also note that not all the features of BLE are implemented in ESP32.

BLE on ESP32

You can refer to the link for more updates on BLE on ESP32.

ESP32 Bluetooth Features

In the next section,  I will list all the features of BLE present on the ESP32 S3 module. 

Bluetooth LE Radio and PHY

Bluetooth Low Energy radio and PHY in ESP32-S3 support:

  • 1 Mbps PHY
  • 2 Mbps PHY for high transmission speed and high data throughput
  • Coded PHY for high RX sensitivity and long-range (125 Kbps and 500 Kbps)
  • Class 1 transmit power without external PA
  • HW Listen before talk (LBT)

Bluetooth LE Link Layer Controller

Bluetooth Low Energy Link Layer Controller in ESP32-S3 supports:

  • LE advertising extensions to enhance broadcasting capacity and broadcast more intelligent data
  • Multiple advertisement sets
  • Simultaneous advertising and scanning
  • Multiple connections in simultaneous central and peripheral roles
  • Adaptive frequency hopping and channel assessment
  • LE channel selection algorithm #2
  • Connection parameter update
  • High-duty cycle non-connectable advertising
  • LE privacy 1.2
  • LE data packet length extension
  • Link layer extended scanner filter policies
  • Low duty cycle directed advertising
  • Link layer encryption
  • LE Ping

Applications Of Bluetooth Low Energy

Fitness trackers – Fitness trackers such as FitBit and others track your heart rate, sleeping pattern, steps taken, energy burnt, distance travelled and more. These devices are so small that they can’t carry heavy batteries. 

Bluetooth low energy does find applications intensely in these products due to their low power consumption. 

Indoor Positioning – limited range helps. The devices can act as beacons to the neighbour Bluetooth connected device. Based on the visibility, and signal levels, one can track the position of the module kut like GPS (Global Positioning Systems). 

It also helps identify customers and interested parties nearby a display, understand their walking patterns and suggest the best products with discounts on the products they showed more interest in. 

Asset Tracking: You can track the assets enabled with Bluetooth low-energy devices. Did you know you can power up a machine with BLE without a battery? You can use RFID technology for that! When you move assets around big campuses and office spaces. 

You can install readers at the main entry and exit doors, which can shortly power BLE devices to read the IDs. This will help in tracking the assets.

Step-By-Step Instructions To Connect The ESP32 To A Smartphone Over Bluetooth

I will show you how to connect your smartphone to the ESP32 over Bluetooth, step-by-step. Let’s begin.

Step 1: Install ESP32 on the Arduino IDE

Please follow the step-by-step guide presented in this article to install the ESP32 on Arduino IDE.

Once installed, you can program ESP32 the same way as how you program an Arduino board.

Step 2: Program ESP32 with the code below

Below is the basic Bluetooth example code. You can open the example by following the steps below.

basic Bluetooth example code
  1. Open Arduino IDE and click on File Menu
  2. Select Examples
  3. Select BluetoothSerial
  4. Select SerialToSerialBT
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

Step 3: Understand the Code

This code creates a bridge between Serial and Classical Bluetooth (SPP) and demonstrates that the BluetoothSerial library has the same functionalities as the normal Serial library.

The first line is the header file “BluetoothSerial.h” which provides the functionality to communicate over Bluetooth using the Serial protocol.

We next check if Bluetooth is enabled on the device. If Bluetooth is not enabled, it will throw an error message.

The code then declares an instance of the BluetoothSerial class called SerialBT.

In the setup() function, the Serial communication is initiated with a baud rate of 115200 using the function Serial.begin(). Then, the SerialBT communication is initiated with the name “ESP32test” using the SerialBT.begin() method. 

In the loop() function, the code checks if there is any data available on the Serial communication port using the Serial.available() method. 

If there is data available, the code reads the data using the Serial.read() method and writes it to the BluetoothSerial port using the SerialBT.write() method.

Similarly, we check if there is any data available on the BluetoothSerial communication port using the SerialBT.available() function.

If there is data available, the code reads the data using the SerialBT.read() method and writes it to the Serial port using the Serial.write() method.

Finally, we add a slight delay of 20 ms using the delay() method to avoid overloading the system with continuous communication.

Step 4: Prepare the Mobile Application

Choose one of these apps from your Apple or Android App Store:

1. Serial Bluetooth Terminal

Serial Bluetooth Terminal

2. Arduino Bluetooth Terminal

Arduino Bluetooth Terminal

Install the serial Bluetooth terminal application from the app market. My preference is the first app.

Step 5: Select the Device and Communicate

Go to Settings → Devices

Go to Settings → Devices

Select Pair New Device and wait for the device ESP32test to appear. You can change the name to whatever you wish. You have to modify the below line in the code.

  SerialBT.begin("ESP32test"); //Bluetooth device name

Select Pair New Device and wait for the device ESP32test to appear. You can change the name to whatever you wish. You have to modify the below line in the code.

ESP32test

You can now type messages in the text window and press the send button. Meanwhile, open the Arduino serial terminal, and you will be able to see the message appear on the serial terminal.

type messages in the text window
type messages in the text window
type messages in the text window
type messages in the text window

You can also send messages from the serial terminal to the mobile. 

I hope you verified the connection, connected and were able to send and receive messages. 

Step 6: What Next?

Congratulations on completing all the basic steps to communicate with a smartphone. What next?

You can build:

  • A project where ESP32 reads the sensor values and sends it over to the mobile phone over Bluetooth
  • Control switches using ESP32 and mobile
  • Control a lamp, read the status of equipment etc

FAQs About The ESP32 And Bluetooth Capabilities

I have included a list of the most frequently asked questions about the Bluetooth and ESP32 Bluetooth capabilities. If you have more questions, please drop them in the comments.

1. What is the maximum range of Bluetooth on ESP32?

The maximum range of Bluetooth on ESP32 depends on various factors such as the environment, obstacles, and the power output of the module. The maximum range of Bluetooth on ESP32 is around 30 feet, in general. 

2. What version of Bluetooth is supported by ESP32?

ESP32 supports Bluetooth 4.2 and Bluetooth 5.0 versions.

3. How many devices can ESP32 connect to via Bluetooth?

ESP32 can connect to multiple devices simultaneously via Bluetooth. However, the number of devices it can connect to depends on the available memory and processing power of the microcontroller.

4. Is it possible to use ESP32 as a Bluetooth peripheral?

Yes, ESP32 can be used as a Bluetooth peripheral, which means it can advertise its services and be discovered by other Bluetooth devices.

5. Can ESP32 connect to a Bluetooth speaker or headphones?

Yes, ESP32 can connect to a Bluetooth speaker or headphones and can be used to stream audio wirelessly.

6. Can ESP32 be used to control other Bluetooth devices?

Yes, ESP32 can be used to control other Bluetooth devices, such as smartphones, speakers, and smart home appliances using the Bluetooth protocol.

7. Can the firmware of ESP32 be updated via Bluetooth?

Yes, it is possible to update the firmware of ESP32 wirelessly via Bluetooth using the Over-The-Air (OTA) update feature.

8. How can I program ESP32 to work with Bluetooth?

There are various ways to program ESP32 to work with Bluetooth, such as using Arduino IDE, ESP-IDF, or third-party libraries such as Bluetooth Serial Library.

9. Is it possible to secure the Bluetooth connection on ESP32?

Yes, ESP32 supports various security features such as encryption, authentication, and authorization to secure the Bluetooth connection between devices.

Conclusion

In this article, I have covered the basics of Bluetooth technology and the Bluetooth capabilities of the ESP32 controller.

I have given a step-by-step procedure to connect a smartphone to an ESP32 Bluetooth device.

Let me know in the comments section below what projects you will build in the future involving Bluetooth.

Please share your feedback about the article and what you would love to learn next. 

If you find this helpful article, please share it with your fellow ESP32 enthusiast. Never stop learning!

Frank

Thursday 4th of January 2024

[ES] Hola AutorPuneeth Kumar,

quería agradecerle por tan gran artículo y el gran esfuerzo, maravilloso. Tengo una pregunta respecto al punto 5. ¿Se puede conectar el ESP32 a un altavoz o auriculares Bluetooth? He probado de conectar un ESP32-DevkitC-v4 (Esp32-Wroom-32) utilizando Bluetooth 5.0 para conectarse a un altavoz Bluetooth 5.0, todo funcionó correctamente. He utilizado la librería A2DP de pschatzmann.

Ahora he adquirido un ESP32-S3 (solo compatible con BLE). Mi pregunta es si sabría decirme usted si es posible conectar un ESP32-S3 con un altavoz Bluetooth para transmitir música.

Muchas gracias por su gran labor. Espero tener respuestas suyas, Att, Frank.