10.Classic Bluetooth serial port SPP

The sample code mainly demonstrates the mutual forwarding of information between the Bluetooth serial port and the serial port, which is derived from the official demo of ESP32, which is simple and easy to understand. So the description mainly explains the concepts that appear in the code.

1. Bluetooth protocol

At present, the main Bluetooth protocols are divided into two categories, traditional Bluetooth (HS/BR/EDR) based on RFCOMM and Bluetooth low energy (BLE) based on GATT.

Traditional Bluetooth is faster and has many specific application protocols, such as audio-oriented A2DP, Bluetooth serial port SPP, etc. However, the power consumption is high, and access to Apple devices requires MFi (Made For iOS) chips and certification.

Bluetooth Low Energy (BLE) can define various GATT profiles by itself, and it is also equipped with commonly used profiles (such as device information, battery, etc.). It has low power consumption and is widely used. It can be used on Apple devices. The disadvantages is that it is slower than traditional Bluetooth. Bluetooth low energy is mostly used on devices with low data volume but sensitive to power consumption, such as bracelets/smart watches/beacons.

2. Classic Bluetooth serial port (SPP)

This demo uses the SPP protocol based on traditional Bluetooth, which comes with all serial port protocols. When the computer or Android phone is connected and paired, a serial port number will be automatically generated in the system for communication, and the experience is not much different from that of a normal wired serial port.

//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("BTSPP_Test");          //Bluetooth device name
  Serial.println("The device started,");
  Serial.println("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);
}

The bluetooth low energy serial port will be demonstrated in the next chapter. In essence, it is a profile configured with a serial port and requires host software support.

Last updated

#524: Add a demo in Melody Creation

Change request updated