{"id":10826,"date":"2024-05-26T09:24:18","date_gmt":"2024-05-26T13:24:18","guid":{"rendered":"https:\/\/dronebotworkshop.com\/?p=10826"},"modified":"2024-05-26T09:24:18","modified_gmt":"2024-05-26T13:24:18","slug":"esp32-bluetooth","status":"publish","type":"post","link":"https:\/\/dronebotworkshop.com\/esp32-bluetooth\/","title":{"rendered":"Bluetooth Classic & BLE with ESP32"},"content":{"rendered":"\n
<\/a> <\/a> <\/a><\/p>\n Today, we will learn how to use the ESP32’s Bluetooth and BLE features to communicate with phones, tablets, sensors, and other ESP32 boards.\u00a0<\/span><\/p>\n In 1994, engineers Nils Rydbeck, Johan Ullman, and Dr. Jaap Haartsen at the Swedish technology company Ericsson conceived a method to replace the ever-expanding jungle of computer cables with a wireless system. Four years later, Ericsson partnered with IBM, Intel, Nokia, and Toshiba to form the <\/span>Bluetooth Special Interest Group (SIG).<\/span><\/a> One year later, in 1999, the specifications for Bluetooth 1.0 were released.<\/span><\/p>\n <\/p>\n Bluetooth (named after the 10th-century king Harald “Bluetooth” Gormsson, who united Denmark and Norway) was initially designed to replace RS-232 (serial) cables with a 2.4GHz wireless link. It has since been expanded to include audio streaming and direction finding.<\/span><\/p>\n In 2010, Bluetooth Low Energy, or BLE, was introduced as part of the Bluetooth 4.0 specifications.\u00a0 This low-powered technology was designed for short data bursts like sensor data.\u00a0 BLE devices have very low power consumption, and the technology is used in items like smartwatches and fitness trackers.<\/span><\/p>\n The Espressif ESP32 microcontroller is an ideal component for hobbyists who want to build Bluetooth and BLE projects. It\u2019s inexpensive, easy to use, and well-documented.\u00a0<\/span><\/p>\n Today, we will see how easy it is to get started with Bluetooth on the ESP32.<\/span><\/p>\n Both Bluetooth (now called <\/span>Bluetooth Classic<\/span><\/i>) and BLE (formerly known as <\/span>Bluetooth Smart<\/span><\/i>) use the 2.4 GHz ISM (Industrial\u2014Scientific\u2014Medical) band, which is available worldwide for non-licensed low-powered radio communications.<\/span><\/p>\n Bluetooth (both flavors) is designed to communicate over short distances, making it ideal for connecting computers and mobile devices to peripherals.<\/span><\/p>\n The two protocols are used for different purposes:<\/span><\/p>\n Both protocols have their roots in the original Bluetooth 1.0 standard. They share several characteristics but also have differences.\u00a0 They can also be used concurrently, for example, an audio device may use Bluetooth Classic for audio transmission while also using BLE for control signals.<\/span><\/p>\n Both Bluetooth Classic and BLE operate in the 2.4GHz ISM band, which extends from 2.400 GHz to 2.4835 GHz. However, the two standards differ in how they divide the bandwidth and are thus incompatible with one another.<\/span><\/p>\n <\/p>\n Both protocols are limited in distance. Bluetooth Classic is rated to exchange data up to 100 meters, while BLE has half that range. However, in real life, distances are usually much shorter, especially indoors.\u00a0<\/span><\/p>\n Device Discovery<\/span><\/i> is the process by which Bluetooth devices make their presence known to one another.\u00a0<\/span><\/p>\n <\/p>\n The concept is pretty basic, but each protocol does it differently.<\/span><\/p>\n In Bluetooth Classic, 32 of the 79 channels can be used to establish a connection between devices. When a device wants to be discovered, it sends signals on these channels.<\/span><\/p>\n BLE only has three channels that can be used to establish a connection. A BLE device that wants to be discovered sends signals on channels 37, 38, and 39, which are known as the <\/span>Primary Advertising Channels<\/span><\/i>.<\/span><\/p>\n Bluetooth Classic and BLE <\/span>Device Pairing<\/span><\/i> is a secure process that enables two devices to communicate by establishing a trusted connection. Once again, both protocols do it differently.<\/span><\/p>\n <\/p>\n In Bluetooth Classic, pairing is initiated manually by the user. The devices then exchange a shared secret, known as a<\/span> link key<\/span><\/i>. Once the link key is exchanged, the devices are paired and can establish a secure connection whenever they\u2019re in range.\u00a0<\/span><\/p>\n Some devices may require a PIN for pairing, while others use Secure Simple Pairing (SSP), simplifying the process and improving security.<\/span><\/p>\n BLE uses a process called \u201cbonding\u201d to create a trusted connection between devices. To lower power consumption, the devices exchange and store a <\/span>long-term key<\/span><\/i>. This key is used to encrypt future connections, ensuring that only bonded devices can communicate with each other.\u00a0<\/span><\/p>\n BLE supports three pairing methods: Just Works, Passkey Entry, and Out of Band (OOB). The method used depends on the capabilities of the devices involved.<\/span><\/p>\n In classic Bluetooth, data exchange occurs over a stable, continuous connection between two paired devices. It creates channels called profiles, which determine how devices interact. Examples are sending music (A2DP profile) or controlling hands-free devices(HFP profile). These connections are maintained for extended data flow, such as streaming.<\/span><\/p>\n <\/p>\n BLE is designed for low-power, intermittent data transfer. Data is exchanged using the <\/span>Generic Attribute Profile (GATT)<\/span><\/i>.\u00a0<\/span><\/p>\n GATT operates using <\/span>Services<\/span><\/i> and <\/span>Characteristics<\/span><\/i>. A Service is a collection of data and associated behaviors to accomplish a particular function or feature of a device. Characteristics are the individual pieces of data that can be read, written to, or notified of changes by the device.\u00a0<\/span><\/p>\n GATT is like an apartment building mailbox with compartments: <\/span>Services<\/span><\/i> are the mailboxes themselves, and <\/span>Characteristics<\/span><\/i> represent the mail in the box. You can read, write, or receive notifications when data in a Characteristic changes, perfect for sensors or remote controls sending occasional data.<\/span><\/p>\n We will examine GATT in more detail when we work with BLE.<\/span><\/p>\n The ESP32 is an excellent choice for working with both Bluetooth Classic and BLE.\u00a0 It supports both protocols, and Espressif provides libraries and code samples to get you up and working quickly.<\/span><\/p>\n There are many models of the ESP32; most of them support Bluetooth, but a few do not. Check out our 2024 ESP32 Guide to find a Bluetooth-capable processor that suits your requirements.<\/span><\/p>\n The ESP32 has a wealth of I\/O ports, both digital and analog. It\u2019s available in several configurations from Espressif and other manufacturers.<\/span><\/p>\n The ESP32 microcontroller features a versatile 2.4 GHz radio section that is capable of handling both Wi-Fi and Bluetooth communication. This radio operates in the license-free 2.4 GHz ISM band.\u00a0<\/span><\/p>\n <\/p>\n As there is only one radio module, using Bluetooth and Wi-Fi simultaneously might not seem possible, but it can be done. The ESP32 has a built-in \u201ccoexistence mechanism\u201d to manage the shared radio resource. This involves intelligent scheduling and time-slicing to give both Wi-Fi and Bluetooth a chance to transmit and receive data.<\/span><\/p>\nIntroduction<\/span><\/h2>\n
Bluetooth Classic & BLE<\/span><\/h2>\n
Similarities and Differences<\/span><\/h3>\n
\n
Frequency<\/span><\/h4>\n
\n
Device Discovery<\/span><\/h4>\n
\n
Device Pairing<\/span><\/h4>\n
Data Exchange<\/span><\/h4>\n
ESP32 with Bluetooth Classic & BLE<\/span><\/h3>\n
ESP32 2.4GHz Radio<\/span><\/h4>\n