Bluetooth LE & Bluetooth

[中文]


When porting example gatt_server, an error occurred indicating head file does not exist. What could be the reasons?

When porting example gatt_server, an error occurred as fatal error: esp_gap_ble_api.h: No such file or directory, but this file is already included in the head file.

  • Check sdkconfig to see whether sdkconfig.defaults is ported from the example or not. By default, Bluetooth® is disabled in SDK and needs to be enabled manually.

  • If you are using cmake, the link configurations in the CMakeLists.txt file should be copied from the example too.


Does ESP32 support Bluetooth® 5.0?

No, the ESP32 hardware only supports Bluetooth LE 4.2.

The ESP32 has passed the Bluetooth LE 5.0 certification, but some of its functions are still not supported on ESP32 (there will be a future chip which supports all functions in Bluetooth LE 5.0).


After the Bluetooth® LE starts advertising, why some mobile phones cannot successfully scan them?

  • Please check whether your mobile phone supports Bluetooth LE function. Some mobile phones, such as iPhones, display Classic Bluetooth only in “Settings” -> “Bluetooth” (by default), and the Bluetooth LE advertisement will be filtered out.

  • It is recommended to use a dedicated Bluetooth LE application to debug the Bluetooth LE function. For example, LightBlue application can be used on iPhone.

  • Please check whether the advertising packet conforms to the specified format. Mobile phones tend to filter out packets that do not conform to the specified format and display only the correct ones.


Can I process OTA through Bluetooth® on ESP32?

Yes, please operate basing on bt_spp_acceptor and bt_spp_initiator.

If using Bluetooth LE, please operate basing on ble_spp_server and ble_spp_client.


How do ESP32 Bluetooth® and Bluetooth® LE dual-mode coexist and how can I use this coexistence mode?

The ESP32 Bluetooth and Bluetooth LE dual-mode does not require complex configurations. For developers, it is simple as calling Bluetooth LE API for Bluetooth LE, and calling Classic Bluetooth API for Classic Bluetooth.

For specifications on Classic Bluetooth and Bluetooth LE coexistence, please refer to ESP32 BT&BLE Dual-mode Bluetooth.


What is the throughput of ESP32 Bluetooth® LE?

  • The throughput of ESP32 Bluetooth LE depends on various factors such as environmental interference, connection interval, MTU size, and the performance of peer devices.

  • The maximum throughput of Bluetooth LE communication between ESP32 boards can reach up to 700 Kbps, which is about 90 KB/s. For details, please refer to example ble_throughput in ESP-IDF.


Does ESP32 support Bluetooth® 4.2 DLE (Data Length Extension)?

Yes, Bluetooth 4.2 DLE is supported in all versions of ESP-IDF. There is no sample code provided currently. You can implement this by calling corresponding APIs directly. Please refer to esp_ble_gap_set_pkt_data_len.


How do ESP32 Bluetooth® and Wi-Fi coexist?

In the menuconfig, there is a special option called Software controls WiFi/Bluetooth coexistence, which is used to control the coexistence of Bluetooth and Wi-Fi for ESP32 using software, thus balancing the coexistence requirement for controlling the RF module by both the Wi-Fi and Bluetooth modules.

  • Please note that if Software controls WiFi/Bluetooth coexistence is enabled, the Bluetooth LE scan interval shall not exceed 0x100 slots (about 160 ms). If the Bluetooth LE and Wi-Fi coexistence is required, this option can be enabled or disabled. However, if this option is not enabled, please note that the Bluetooth LE scan window should be larger than 150 ms, and the Bluetooth LE scan interval should be less than 500 ms.

  • If the Classic Bluetooth and Wi-Fi coexistence is required, it is recommended to enable this option.


How can I get ESP32 Bluetooth® Compatibility Test Report?

Please contact sales@espressif.com.


What is the transmit power of ESP32 Bluetooth®?

The ESP32 Bluetooth has 8 transmit power levels, corresponding to -12 ~ 9 dBm of transmit power, with a 3 dBm interval. The controller software limits the transmit power and selects the power level according to the corresponding power level declared by the product.


Could ESP32 realize bridging between Wi-Fi and Bluetooth® LE?

Yes, this function is developed on the application layer. You can retrieve data through Bluetooth LE and send them out via Wi-Fi. For detailed information, please refer to Wi-Fi and Bluetooth LE Coexist demo.


What is the operating current of ESP32 Bluetooth® LE?

Current

MAX (mA)

Min (mA)

Average (mA)

Advertising: Adv Interval = 40 ms

142.1

32

42.67

Scanning: Scan Interval = 160 ms, Window = 20 ms

142.1

32

44.4

Connection(Slave): Connection Interval = 20 ms, latency = 0

142.1

32

42.75

Connection(Slave): Connection Interval = 80 ms, latency = 0

142.1

32

35.33


What kinds of Bluetooth® LE profiles does ESP32 support?

Currently, ESP32 Bluetooth LE fully supports some basic profiles, such as GATT/SMP/GAP, as well as some self-defined profiles. The ones that have already been implemented include Bluetooth LE HID (receiving side), Bluetooth LE SPP-Like, Battery, DIS, BluFi (Bluetooth Network Configuration-transmitting side), and so on.


How do I connect mobile phones and play music using ESP32 Bluetooth®?

ESP32 is used as an A2DP receiver when connected to a cell phone to play music. Please note that the A2DP Sink Demo uses a mobile phone to obtain SBC encoded data stream only. In order to play sounds, you will also need to decode the data and some peripherals, including codec modules, D/A converter, and speaker.


How is the ESP32 SPP performance?

When we use two ESP32 boards to run SPP, one-way throughput can reach up to 1900 Kbps (about 235 KB/s), which is close to the theoretical value in the specifications.


What is the maximum transmission rate for ESP32 Bluetooth® LE?

The transmission rate of ESP32 Bluetooth LE can reach 700 Kbps when it is tested in a shielded box.


How does ESP32 Bluetooth® LE enter Light-sleep mode?

On the hardware level, a 32 kHz external crystal should be added, or the Light-sleep mode will not take effect.

On the software level (SDK4.0 and later versions), the following configurations should be enabled in menuconfig:

  • Power Management:| menuconfig > Component config > Power management > [*] Support for power management

  • Tickless Idle:| menuconfig > Component config > FreeRTOS > [*] Tickless idle support (3) Minimum number of ticks to enter sleep mode for (NEW)

Note

Tickless idle needs to be enabled to allow automatic light-sleep mode. FreeRTOS will enter Light-sleep mode if no tasks need to run for 3 ticks (by default), that is 30 ms if tick rate is 100 Hz. Configure the FreeRTOS tick rate to be higher if you want to allow shorter duration of light-sleep mode, for example: menuconfig > ``Component config > FreeRTOS > (1000) Tick rate (Hz).

  • Configure external 32.768 kHz crystal as RTC clock source :| menuconfig > Component config > ESP32-specific > RTC clock source (External 32 kHz crystal)[*] Additional current for external 32 kHz crystal

Note

The “additional current” option is a workaround for a hardware issue on ESP32 that the crystal can fail in oscillating. Please enable this option when you use external 32 kHz crystal. This hardware issue will be resolved in the next ECO chip.

  • Enable Bluetooth modem sleep with external 32.768kHz crystal as low power clock :| menuconfig > Component config > Bluetooth > Bluetooth controller > MODEM SLEEP Options > [*] Bluetooth modem sleep

Are there any documentation references for ESP32 BluFi networking?

For BluFi networking, please refer to ESP32 Blufi. For BluFi networking examples, please refer to Blufi.


What is the maximum transmission rate for ESP32 Classic Bluetooth® SPP?

In an open environment, the transmission rate for ESP32 Classic Bluetooth SPP can reach 1400+ Kbps ~ 1590 Kbps (only for reference, please do tests based on your actual application environment) with bi-directional transmitting and receiving simultaneously.


Is ESP32 Bluetooth® compatible to Bluetooth® ver2.1 + EDR protocol?

Yes. The ESP32 Bluetooth is downward-compatible, you can do tests using our official Bluetooth examples.


How many Bluetooth® clients can be connected to ESP32?

The Bluetooth LE server supports up to nine client connections, please check the configuration of parameter ble_max_conn for applications. For stable connection, three clients should be good.


How can I get the MAC address of Bluetooth® devices for ESP32?

You can get the MAC address configured by Bluetooth via API esp_bt_dev_get_address(void);, also the system pre-defined MAC address types via API esp_err_t esp_read_mac(uint8_t* mac,esp_mac_type_ttype);.


What is the default Bluetooth® transmit power for ESP32 SDK?

  • By default, the power level of ESP32 SDK is 5, and the corresponding transmit power is +3 dBm.

  • The power level of ESP32 Bluetooth ranges from 0 to 7, with the corresponding transmit power ranges from -12 dBm to 9 dBm. Each time the power level increases by 1, the corresponding transmit power will increase by 3 dBm.


Is it possible to use Wi-Fi Smartconfig and Bluetooth® LE Mesh for ESP32 simultaneously?

It is not recommended to use them simultaneously.

  • The Smartconfig will need to receive the networking data, thus occupying the antenna all the time. If it is used together with Bluetooth LE Mesh, there will be an extremely high rate of failure.

  • The Bluetooth LE Mesh can be used together with BluFi. So it is recommended to use BluFi for networking.


What is the operating current for ESP32 Classic Bluetooth®?

A2DP (Single core CPU 160 Mhz,DFS = false,commit a7a90f)

Current

Maximum (mA)

Minimum (mA)

Average (mA)

Scanning

106.4

30.8

37.8

Sniff

107.6

31.1

32.2

Play Music

123

90.1

100.4


How can I modify the transmit power for ESP32 Bluetooth®?

The Bluetooth transmit power can be configured via function esp_ble_tx_power_set();. Please refer to esp_bt.h.


How is the networking compatibility of ESP32 Bluetooth® LE? Is it open-sourced?

  • ESP32 Bluetooth networking, BluFi networking for short, has a good compatibility as Bluetooth LE and is compatible with many mainstream mobile phones such as Apple, HUAWEI, Mi, OPPO, MEIZU, OnePlus, ZTE and etc.

  • Currently, the BluFi protocol and phone application code is open-sourced.


When I execute example bt_spp_acceptor on ESP32, the IOS device cannot find the ESP32 device during scanning. What could be the reasons?

  • Apple has opened Bluetooth® as: A2DP, HID’s keyboard, avrcp, SPP (need MFI), high-level Bluetooth LE and ANCS for Bluetooth LE.

  • If the IOS device expects to communicate with the end device via SPP, the SPP of the end device should have the MFI certificate. However, ESP32 SPP does not have the MFI certificate, thus the IOS device cannot find ESP32 during scanning.


How is the security of ESP32 Bluetooth® LE/Bluetooth® Secure Simple Pairing (SSP) compared to legacy pairing?

  • Secure Simple Pairing (SSP) is more secure than legacy pairing.

  • The legacy pairing uses symmetric encryption algorithm, while Secure Simple Pairing (SSP) uses asymmetric cryptography algorithm.


How can I confirm the MTU size of ESP32 Bluetooth® LE?

  • By default, the MTU size of ESP32 Bluetooth LE is 23 bytes, and can be configured to reach 517 bytes.

  • For phones, the MTU size can be self-defined. Then, the end device with a smaller MTU will be chosen for communication.


When advertising in ESP32 Bluetooth® LE mode, an error occurred as “W (17370) BT_BTM: data exceed max adv packet length”. How can I resolve such issue?

  • This is because the advertising data has exceeded the maximum advertising packet length.

  • The maximum data length of advertising payload is 31 bytes. If the actual data length exceeds 31 bytes, the Bluetooth protocol stack will drop some data and generate an error warning.

  • If the data to be advertised exceeds the maximum packet length, the extra data can be put in the scan response packet.


Does ESP32 Bluetooth® LE support Client-Server mode, in which gatt server and gatt client can coexist?


What are the risks if there are over six devices connected to ESP32 Bluetooth® LE?

  • Usually it depends on the specific application scenario. In general, the ESP32 Bluetooth LE can communicate stably with three devices connected.

  • There is no exact number for maximum Bluetooth LE connections. When there are multiple devices connected to Bluetooth LE simultaneously, the RF is time-multiplexed, thus requiring the designer to ensure that each device is not overly occupied, causing other devices to timeout and disconnected.

  • The connection parameters include: connection interval, connection window, latency and timeout. It is ok for devices to not respond within the latency, but if the responding time exceeds timeout threshold, the device will be disconnected.

  • If the interval is configured to 100 and window to 5, the Bluetooth LE will be able to connect to more devices with Wi-Fi disconnected. However, If Wi-Fi is connected and the value of interval is too small, only a few devices can be connected.

  • When the Bluetooth LE supports multiple devices connected simultaneously, there will be bigger possibility for RF solt management to generate error. So when there are multiple connections for Bluetooth LE, it is necessary to debug for different scenarios.


When using ESP32 device as the server of Bluetooth® LE, how many client devices can be connected?

  • The ESP32 Bluetooth LE supports up to nine client devices for connection. It is recommended to hold this number within three.

  • Please make configurations via menuconfig > Component config > Bluetooth > Bluetooth controller > BLE MAX Connections.


How can I send files via Bluetooth® BR/EDR for ESP32?

  • Please refer to example bt_spp_acceptor or bt_spp_initiator in classic bt.


When I download example ESP_SPP_SERVER for ESP32, how can I modify the name of the Bluetooth® device?

  • The name of the Bluetooth device can be modified via adv parameter:

static const uint8_t spp_adv_data[23] = {
  0x02,0x01,0x06,
  0x03,0x03,0xF0,0xAB,
  0x0F,0x09,0x45,0x53,0x50,0x5f,0x53,0x50,0x50,0x5f,0x53,0x45,0x52,0x56,0x45,0x52};
  • The “0x0F” in the third line means the length of the following data is 15, “0x09” stands for data type (fixed) and data from “0x45” indicates the corresponding ASCII code of the device names (BLE_SPP_SERVER by default).


When I use the “BluFi” example to configure network for ESP32, the Wi-Fi cannot be connected during the distribution process via the EspBluFi application since a wrong Wi-Fi has been configured. Then the device is restarted after sending a SCAN command from the application. What is the reason?

  • The “BluFi” example stipulates that Wi-Fi “SCAN” commands cannot be sent when Wi-Fi is connected.

  • To solve this issue, you can add ESP_ERROR_CHECK(esp_wifi_disconnect()); to the first line of the ESP_BLUFI_EVENT_GET_WIFI_LIST:{}; function under the blufi_example_main.c file.


How can I specify a BLE connection/transmit operation to run on core 0 when I use ESP32?

  • Currently, ESP32’s BLE connection/transmit operation only can be run on core 1. You can enable this via menuconfig > Component config > FreeRTOS > Run FreeRTOS only on first core .

  • According to this application requirement, you can distribute tasks to a certain core using the “xTaskCreatePinnedToCore()” or “xTaskCreateStaticPinnedToCore()” API. For specific instructions, please see core assignment.


When I set name for the bluetooth of an ESP32 device using Chinese characters, messy code shows instead. What is the reason?

  • This is because the Chinese encoding format of the editor is not UTF-8 at this time, and the encoding format of the editor needs to be changed to UTF-8.


When I upload sub-packages to the Bluetooth channel using ESP32, the maximum transmission data length of a packet is 253 (MTU is set to 263). This results in slower transmission when a large number of data packets are transmitted for multi-packet reading. Is there a BluFi extension protocol that can support the transmission of a larger length of data in one packet, or are there other solutions to increase the transmission rate?

  • The transmission is slow When a large number of data packets on the Bluetooth channel are transmitted for multi-packet reading. You can improve the transmission speed by adjusting the Bluetooth connection parameters.

  • The BLE packet length setting depends on the ESP_GATT_MAX_MTU_SIZE setting, please refer to the Description.

  • The configured MTU size will affect the data transmission rate. The effective MTU length needs to be changed by MTU exchange to change the default MTU size. The MTU size used in the final MTU exchange is used as the MTU size for the communication between the two devices. You can check the value of the MTU after exchange, such as the follows:

case ESP_GATTS_MTU_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU%d", param->mtu.mtu);

What profile does ESP32’s classic Bluetooth® support?

  • Currently, it supports A2DP, AVRCP, SPP, HFP, and HID.


How many stable connections can be reached for ESP32-C3’s Bluetooth® LE (BLE)?

  • We recommend the connection number does not exceed four.


How can I adjust the BLE advertising interval?

  • The advertising interval is decided by adv_int_min and adv_int_max parameters in BLE advertising struct, which configures the minimum and maximum advertising interval respectively.

  • The advertising interval ranges from 0x0020 to 0x4000 and the default value is 0x0800. The interval time is the value * 0.625 ms, i.e., 20 ms to 10.24 sec.

  • If the values of adv_int_min and adv_int_max are different, the advertising interval is within the range of the two values. If the values are the same, the interval will be this fixed value.


How can I input the PIN code via mobile phone during ESP32’s Classic Bluetooth Pairing mode?

You can disable Secure Simple Pairing to support only Legacy Pairing.

  • From esp-idf v3.3 to v4.0 (not include v4.0): Component config > Bluetooth > Bluedroid Enable > [*] Classic Bluetooth > [ ]Secure Simple Pairing

  • esp-idf v4.0 and above: Component config > Bluetooth > Bluedroid Options > [ ] Secure Simple Pairing


How much memory does ESP32 Bluetooth occupy?

  • Controller:

    • BLE single mode: 40 KB

    • BR/EDR single mode: 65 KB

    • Dual mode: 120 KB

  • Main equipment:

    • BLE GATT Client (Gatt Client demo): 24 KB (.bss+.data) + 23 KB (heap) = 47 KB

    • BLE GATT Server (GATT Server demo): 23 KB (.bss+.data) + 23 KB (heap) = 46 KB

    • BLE GATT Client & GATT Server: 24 KB (.bss+.data) + 24 KB (heap) = 48 KB

    • SMP: 5 KB

    • Classic Bluetooth (Classic Bluetooth A2DP_SINK demo, including SMP/SDP/A2DP/AVRCP): 48 KB (.bss+.data) + 24 KB (heap) = 72 KB (an additional 13 KB is added when the example is running)

Note

The above heap (Heap) all include the task stack (Task Stack), because the task stack is allocated from the heap and considered as a heap.

  • Optimized PSRAM version:

In ESP-IDF v3.0 and later versions, if you open the PSRAM related options of the Bluetooth menu in menuconfig, and put part of the .bss/.data section and heap of Bluedroid (Host) into PSRAM, almost 50 KB memory space can be saved.


When I use the “gattc_gatts_coex.c” example on ESP32 to test BLE multi-connection, it can only connect to four devices even after I set the BLE Max connections in menuconfig to five. What is the reason?

  • Please set the BT/BLE MAX ACL CONNECTION in menuconfig to five.


Does ESP32-C3 BLE support master and slave mode at the same time? What is the number of connections in master mode and slave mode?

IDF: release/v4.3, master

  • ESP32-C3 supports master and slave mode at the same time, which share 8 connections. For example, if ESP32-C3 connects to 4 slave devices, it can be connected by 8 - 4 = 4 master devices.

  • In addition, when ESP32-C3 is used as a slave, it can be connected by 8 master devices; when used as a master, it can connect to 8 slave devices.


What is the maximum MTU Size of ESP32 Classic Bluetooth?

  • ESP32 Classic Bluetooth has two protocols, namely A2DP and SPP. The maximum MTU Size setting of BT A2DP (default) is 1008 bytes, of which the header occupies 12 bytes and the actual amount of data transmitted by the application layer is 1008 - 12 = 996 (bytes); the maximum MTU Size of BT SPP (default) Set to 990 bytes.


How can I resolve the frequently occurred ELxXX error (such as ELx200) when Wi-Fi and Ble co-exit?

CHIP: ESP32

  • It has been fixed in commit 386a8e37f19fecc9ef62e72441e6e1272fa985b9. Please switch to the corresponding commit to test.


How does BLE capture packets?

  • There are many available tools, such as:

    • TI Packet sniffer

    • NRF Packet sniffer


When I use an ESP32 development board to test several versions of bluefi example under ESP-IDF for networking, the following error kept printing. What is the reason?

E (117198) BT_L2CAP: l2ble_update_att_acl_pkt_num not found p_tcb
W (117198) BT_BTC: btc_blufi_send_encap wait to send blufi custom data
  • When this error occurs, please modify the esp_ble_get_cur_sendable_packets_num(blufi_env.conn_id) to esp_ble_get_sendable_packets_num() in the components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_prf.c file.

  • This bug has been fixed in all branches, you can update ESP-IDF to the latest release version.


When I use ESP32, can Light-sleep mode be enabled for Bluetooth and can Bluetooth be kept connected in Light-sleep mode?


How can I modify the Bluetooth device name of ESP32?

  • The structure to be modified is as follows:

static uint8_t raw_adv_data[] = {

/* flags*/

0x02, 0x01, 0x06,

Tx power*/

0x02, 0x0a, 0xeb,

/* service uuid*/

0x03, 0x03, 0xFF, 0x00,

/* device name*/

0x0f, 0x09,'E','S','P','_','G','A','T','T','S','_','D','E ','M','O'

};
  • The above /* device name*/ is the modified item. Among them, 0x0f is the total length of the field type plus specific content, and 0x09 indicates that this type refers to the device name. Subsequent’E’,’S’,’P’,’_’,’G’,’A’,’T’,’T’,’S’,’_’,’D’,’E’, ‘M’,’O’ are the ASCII code of the broadcast device name.


What is the maximum supported broadcast length of BLE 5.0 broadcast after it is set to legacy mode?

  • The maximum supported length is 31-byte.


How can I set a BLE broadcast package as unconnectable package?

CHIP: ESP32

  • please reffer to the gatt_server demo,and set adv_type as ADV_TYPE_NONCONN_IND.

    static esp_ble_adv_params_t adv_params = {
      .adv_int_min        = 0x20,
      .adv_int_max        = 0x40,
      .adv_type           = ADV_TYPE_NONCONN_IND,
      .own_addr_type      = BLE_ADDR_TYPE_PUBLIC,
      //.peer_addr            =
      //.peer_addr_type       =
      .channel_map        = ADV_CHNL_ALL,
      .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
      }
    

How can I send Bluetooth HCI commands directly to ESP32-WROOM-32D module through the serial port?

  • Please refer to controller_hci_uart_esp32.

  • When ESP32 is used as a controller, and the other device serves as a host, HCI commands can be sent to ESP32 via UART.


Does ESP32 support transmitting audio stream using A2DP?

Yes, please refer to example a2dp_source.


How many devices can be connected at the most as suggested by the White List of ESP32 Bluetooth LE?

  • The maximum supported number is 12.


Can ESP32 Bluetooth LE use PSRAM?

To enable Bluetooth LE to use PSRAM, please go to Component config > Bluetooth > Bluedroid Options and enable BT/BLE will first malloc the memory from the PSRAM


No answer found?
Go to ESP forum or ESP GitHub to find more information.

Suggestion on this document? Provide feedback!