The Power of Bluetooth!

Laurentia Kayleen Christopher
6 min readMar 25, 2023

--

Hello everyone reading my blog! Welcome back to the 7th Project of me using the Arduino for many things. Now, I’m back with a new and very very interesting topic — Bluetooth!

As far as we’ve known, we just use bluetooth whenever we want to share files, listen to wireless headphones, and other basic things that are available on our phones — but what if we can establish a bluetooth connection using our ESP32?

COMPONENTS

This time, the components needed for the bluetooth project is — well, very very little. I just need :

  1. Data cable
  2. ESP32
  3. Breadboard
  4. Your smartphone

Keep in mind that by this point I haven’t thought about any innovations yet, so buckle up for the simple part of this blog! When I get my hands on more I’ll still update this!

SETUP

The setup for this simple beginning of the project is just connecting the ESP32 to your laptop with a data cable.

This is mostly it!

Now let’s get on to the code so that this will work!

#include "BluetoothSerial.h"

//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234"; // Change this to more secure PIN.

String device_name = "ESP32-BT-Slave";

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

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
SerialBT.begin(device_name); //Bluetooth device name
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
//Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
#ifdef USE_PIN
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
}

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

Now the concept behind the code is to first start the Bluetooth Serial library, which is :

#include "BluetoothSerial.h"

This way, all the codes after will be able to work. The next part is setting up. In the setup() part, remember to begin the serial with a baud of 115200. You will have to begin the bluetooth device name and set the pin that you use. Then, at the loop() part, you write the possibilities of whether or not the device is available.

Now when we upload the code into the Arduino IDE, this will be shown in the serial monitor :

This means it’s ready to pair!

Now, you download the application called “Serial Bluetooth Terminal” and open it up, look at the devices, and pair it with the ESP32 name shown. This is how it went for me :

Yeah well it kinda got into errors for a bit hahah

Then, when you know it’s connected, you just type in some words you want to show in the serial monitor, just like what I did, and here it is :

Tada!

Now this is the simple version. But I decided to take the plunge and try a BLE or Bluetooth Low Energy — which will need another ESP32. Now here’s how it’s done.

The lower ESP32 is the server one!

First, we need to create an ESP32 BLE Server. To create the ESP32 BLE Server, we just need to write this code, which is available in the Arduino IDE.

To create a BLE server, the code should follow these steps:

  1. Create a BLE Server. In this case, the ESP32 acts as a BLE server.
  2. Create a BLE Service.
  3. Create a BLE Characteristic on the Service.
  4. Create a BLE Descriptor on the Characteristic.
  5. Start the Service.
  6. Start advertising, so it can be found by any other devices.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");

BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);

pCharacteristic->setValue("Hello World says Neil");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising();
// this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06);
// functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}

After we have the BLE Server established, what we need to do next is to create an ESP32 BLE Scanner.

The upper ESP32 is the scanner one!

Make another sketch for this, which is also provided inside the Arduino IDE. This is the code :

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};

void setup() {
Serial.begin(115200);
Serial.println("Scanning...");

BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}

void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}

This program examines the area for nearby devices while initializing the ESP32 as a BLE device. To your ESP32, upload this code. To make sure that you’re uploading the code to the correct ESP32 board, you may have to briefly disconnect the other ESP32 from your computer so that it won’t get mixed up.

Once it is uploaded now we have a ‘server’ and a ‘scanner’ for each of our ESP32 boards.

Next, we take a look at the scanner ESP32 and we will find devices, one of them is the ‘MyESP32’ detected.

If you see below and take a look at the serial monitor, you’ll see the MyESP32 :D

Next, don’t forget to download the nRF CONNECT for Mobile. Enable the bluetooth in your phone and press scan on the application.

You will see this :

After you press ‘Connect’, then this will show :

Tada!

What happens next is entirely up to us, but at least it has worked and the ESP32 Bluetooth Low Energy is working!

This is all for the project, my hands are admittedly very tired, but thanks for reading! :D

--

--