October 24, 2018 | 15min read
Bluetooth Low Energy Sniffing Guide
Nowadays, Bluetooth Low Energy is one of the most popular protocols designed for low-powered and short-range communication between smart devices. As the Internet of Things is steadily gaining popularity, there are even more reasons to learn how it works from the ground up. At the end of this guide, you will gain the confidence to effortlessly and effectively debug and analyze BLE communication for your project.
Bluetooth Low Energy Sniffing in the context of this article is basically a way to analyze packets which are sent between master (Peripheral) and slave (Central Manager). This knowledge is essential to debug critical errors, point out performance bottlenecks or reverse engineer protocol of your interest. I assume you have at least some elementary knowledge of how Bluetooth Low Energy works. My plan is to extend it with the following steps:
- Prepare low-cost Bluetooth Low Energy Sniffer setup based on very popular nRF51 or nRF52 boards managed from the Wireshark application.
- Introduce you to the most common BLE commands, which are used in practice with proper links to specification to extend your knowledge if you are curious.
- Analyze the example output from TI CC2541 Sensor Tag.
- Automate most tedious tasks with Lua scripts in Bluetooth Sniffer Wireshark.
There are a lot of options to choose from if you are looking for Bluetooth Low Energy sniffers. I decided to base this guide on nRF family boards, as they are easy to use, quite popular, low-cost and have good integration with Wireshark. There are nRF51 DK (PCA10028), nRF52840 DK (PCA10056) and Adafruit Bluefruit LE Friend (nRF51822) at my disposal and I have tested this setup with them. Other nRF51/nRF52 boards should work as well.
To enable the Bluetooth Sniffing functionality, we have to flash our boards with the latest nRF Sniffer v2 firmware. You can find a detailed user guide both for nRF & Adafruit and follow it if you have any problems with the steps below. I want to include them for reference here:
- Download the nRF Sniffer v2 package from [Nordic website](https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Sniffer-for-Bluetooth-LE/Download#infotabsNordic website). In my case, I got the version 2.0.0-1.beta.
- Extract the downloaded zip file, go to
segger_jlinkfolder and install the driver for your operating system. All of my below command’s paths assume that
segger_jlinkis the current directory.
- Run Jlink.exe (JLink Commander on Windows) or jlinkexe (Linux/MacOS). You should get J-Link> command prompt. Make sure that
VTrefis around 3.3V (the board is powered on) and then type the following commands based on your hardware version:
PCA10056 (nRF52840 Preview DK) and PCA10040 (nRF52 DK):
erase- erase the device memory
nRF52832_XXAA- select the device type
s- set the SWD target interface
1000- set the interface speed in kHz
loadfile ../hex/sniffer_pca10040_51296aa.hex- flash firmware
r- reboot the device
g- run the board firmware
PCA10028 (nRF51 DK) and Adafruit Bluefruit LE Friend (nRF51822):
erase- erase device memory
nRF51422_XXAC- select device type
s- set SWD target interface
1000- set interface speed in kHz
loadfile ../hex/sniffer_pca10028_51296aa.hex- flash firmware
r- reboot device
g- run the board firmware
To test our board configuration properly, we must install Wireshark application with the nRF plugin. You can do that with the following steps:
- Install Wireshark from the official website. In my case, I got version 2.6.3.
- Open the application and select
Help > About Wiresharkfrom the menu. On macOS
Wireshark > About Wireshark.
- Click on
Extcap path- this should open
extcapfolder in your files explorer.
- Extract content from the
extcapfolder from nRF Sniffer v2 package to the opened folder from the previous point.
- Make sure that you have installed Python2.
- Install a serial module dependency:
pip2.7 install pyserial.
nrf_sniffer.bat from extcap folder should return with following output:
No arguments given! That means nRF plugin is correctly installed and all dependencies are downloaded. Now, plug in your board, restart Wireshark and you should see the following screen with an option to select your board to start Bluetooth sniffing:
Double click on nRF Sniffer and select the device, which we would like to analyze, from the top menu. We are ready to go!
The best way is to learn by example! I’ll use the Texas Instruments CC2541 SensorTag and nRF Connect application to capture the traffic between them. The last one will help us scan, connect and send “read/write” requests so that we can have something interactive to analyze. You can find nRF Connect both on Android and iOS stores. In this section, I’ll also point you out to the Bluetooth Core Specification (CS) and Bluetooth Core Specification Supplement (CSS). You definitely should have them to be able to confirm your assumptions and check parts you don’t understand during Bluetooth sniffing. We will start with the Low Energy Controller Link Layer specifciation (CS Vol 6, Part B), which describes the Link Layer protocol. It is used directly for an advertisement, a connection procedure, and a link configuration.
In practice, before any connection takes place, we want to find our device first. For example, your mobile application might want to know if a specific type of BLE-enabled peripheral is in range. Most of them send broadcasts on three channels (37, 38 and 39) periodically, so that they can be detected. The exemplary advertisement BLE packet sniffer is defined in the following way:
Wireshark captures Bluetooth Low Energy Link Layer format (
btle) as the outermost layer of the packet. It contains Access Address, PDU and CRC calculated based on the PDU content:
- Access address (AA) - it is a unique address intended for each Link Layer connection between any two devices. Advertisement channel packets have fixed AA which equals 0x8E89BED6 (data is encoded in little-endian for all L2CAP packets).
- PDU - it can be Advertising Channel PDU (CS Vol 6, Part B, 2.3) or Data Channel PDU (CS Vol 6, Part B, 2.4). Its maximum size can reach from 2 to 257 bytes.
- CRC - 24-bit CRC, calculated over the PDU (CS Vol 6, Part B, 3.1.1).
Advertisement Channel PDU’ header determines further information (CS Vol 6, Part B, 2.3):
0x00- it defines PDU type equal to ADV_IND, which means it is connectable and unidirectional advertisement event. The address contained in the payload is public.
0x09- the length of Advertising Channel PDU’s payload. As this is only one-byte, the maximum size of any PDU’s payload is up to 255 bytes.
Finally, the specific PDU type I sniffed (
- Advertising address (AdvA) - in our case it is the public MAC address of TI SensorTag equal to
- Advertisement data - (up to 31 bytes) containing advertisement data fields (described in detail in CSS v7, Part A). Each field starts with one-byte length (
0x02), one-byte type (
0x01- flags) and type-specific data (
0x05-BD/EDR Not Supported and LE Limited Discoverable Mode).
Advertisement data is the most important part of the BLE packet. Its content usually contains flags, the short name of a device and the manufacturer-specific data. Fortunately, you don’t have to remember all bit fields and format from the specification to be able to read these. Wireshark has a built-in dissector which does most of the job for you:
In classic profile (selectable on a bottom right side of the Wireshark’s window) view is split into three parts: the list of captured packets, the decoded description of the packet and the hexadecimal form of the packet. Select
View > Interface Toolbars > nRF Sniffer to see nRF Sniffer Toolbar at the top if it’s not enabled already. Feel free to check devices which are advertising around you. There are even more PDU types you can see in your sniffer logs:
ADV_DIRECT_IND- connectable directed advertising event. It contains both Advertisement Address and Target Address. This packet does not hold any data as it is directed only at Target Address.
ADV_NONCONN_IND- non-connectable and non-scannable undirected advertising event. It has the same structure as ADV_IND.
ADV_SCAN_IND- scannable undirected advertising event. It has the same structure as
- Other Extended Advertising Payload packets (CS Vol 6, Part B, 2.3.4).
The device is connectable when it can accept connections and it is scannable when it can accept scan requests (
SCAN_REQ). The last option is very often utilized when information about the device can’t fit in 31 bytes (advertisement data part of
ADV_IND) and the additional response (
SCAN_RSP ) is used to send additional 31 bytes:
Scan request contains information about scanner address (random
4b:63:d5:89:14:10) and advertisement address (public
Scan response data has the same fields as advertisement data except flags data type (CCS Part A, 1.0). I received following values from my TI SensorTag:
- SRP (#1) - complete local name (
- SRP (#2) - connection interval range (
0x12): 100ms (
0x50_ 1.25ms) - 1000ms (
- SRP (#3) - tx power level (
0x0A) - 0 dB.
With the data gathered from advertisement packets seen above, the Central Manager can filter and finally recognize device with which it can establish a connection. It’s worth to note that the selected data types of advertisement are visible and can be processed by applications on iOS phones only. Therefore, the MAC address is not available. What’s important, the distinguishing information such as manufacturer’s company and product id are usually provided in manufacturer data type (
Open nRF Connect application and press side button on TI SensorTag. Then when the device is visible on your list click “CONNECT” to establish a link. Make sure to have opened Wireshark application with the applied device filter to only capture TI’s packets - you can set it in the top nRF Sniffer Toolbar.
Don’t be surprised if you don’t see anything for the first time. Both nRF51 and nRF52 boards are limited to listen to one channel simultaneously. It may happen that a connection request event occurred in the different channel than the one which the device was listening to. In that case, just disconnect and retry the whole procedure.
A lot of events will appear after connection, so make sure to pause Bluetooth sniffing to analyze carefully what really happened. First one of them should be a connection request which is the last, not mentioned, type of PDU of Advertisement Channel PDUs. It is responsible for creating a connection, which will continue on a different channel. It has the following structure:
There are a lot of fields to cover so start slowly, one by one:
- Initiator’s device address (InitA) - It’s a MAC address of a device, which is trying to connect to TI SensorTag. In this case, based on the header and this field’s value, we know that it has the following random address:
- Advertiser’s device address (AdvA) - It’s a MAC address of a device, to which we are connecting. It’s TI SensorTag address.
- Access address (CS Vol 6, Part B, 2.2) - This is an address specific to the newly established Link Layer connection. Every successive packet will use this address in the header until the connection terminates.
- CRC initialization value (CS Vol 6, Part B, 3.1.1) - this is an initialization value for linear feedback shift register used to implement 24-bit CRC polynomial.
- Transmit window size (CS Vol 6, Part B, 4.5.3,
0x03* 1.25ms = 3.75ms) - after
CONNECT_INDis sent, a master can specify a duration of the time window when the slave should receive the first message. This first message, called anchor point defines when connection event starts.
- Transmit window offset (CS Vol 6, Part B, 4.5.3,
0x09* 1.25ms = 11.25ms) - transmit window start is defined by an offset plus delay (1.25ms for
CONNECT_IND). Therefore, this payload declares that the first transmit window will start 12.5ms from
CONNECT_INDmessage and be closed 16,25ms from
CONNECT_IND(based on above transmit window size). A slave must respond to the message in 1 out of 6 transmission windows, which are started once again every connection interval.
- Connection interval (CS Vol 6, Part B, 4.5.1,
0x0018* 1.25ms = 30ms) - the time between each connection event. Connection event is a time window when both slave and master can exchange packets. When this procedure is done, both devices have to wait till the next connection event occurs. Higher connection interval value decreases the power consumption but increases the connection latency.
- Connection slave latency (CS Vol 6, Part B, 4.5.1,
0x0000) - allows a slave to skip the specified number of connection events. In this case, TI Sensor Tag must listen to every connection event.
- Connection supervision timeout (CS Vol 6, Part B, 4.5.2,
0x0048* 10ms = 720ms) - it defines the maximum time between two Data Packet PDUs before the connection is considered lost. Before the connection is established, this value is equal to 6 times connection interval value.
- Channel map field (ChM, CS Vol 6, Part B, 1.4.1,
0xFFFFFFFF1F) - the map indicating the used (bit set to 1) and the unused (bit set to 0) data channels during connection. This connection uses all channels except 37, 38, 39 which are equal to RF channels: 0,12, 39 and are reserved for advertisement purposes.
- Hop increment & Master’s sleep clock accuracy (CS Vol 6, Part B, 188.8.131.52 & 4.2.2,
0x30= 001 10000b = 1 SCA & 16 Hop) - hop increment defined in the first 5 bits is an additional field which is used to calculate next channel index in use based on the current channel index. New channel in our case is calculated using following formula:
newChannelIndex = (currentChannelIndex + hopIncrement) % 37, which results in 16, 32, 11 and so on. You can read about algorithm variants and various edge cases in the Core Specification. Master’s sleep clock is defined in the last 3 bits and specified as PPM value. In this payload value of 1 means 151 to 250 ppm accuracy. It is used to calculate time delta relative to window start and end when the receiver should start and end listening.
Timing parameters like window size, window offset, connection interval, and connection slave latency might be hard to grasp without an example. The following graph shows the beginning of connection establishment with all relevant timings marked and annotated:
CONNECT_IND message, the first packet should appear inside a green area called transmit window. If that message won’t appear, a successive green window can be used to retry connection procedure. In our case, as the first packet arrived in time, it is not used. Master’s packets are blue rectangles and slave’s packets are red rectangles. Horizontal size should mark timings, but to make actual packets visible they are larger than expected. For example, the first connection event lasted 128μs (
LL_VERSION_IND) + 150μs (
T_IFS) + 80μs (
EMPTY_PDU) = 358μs which is only ~1,2% of available time in the first connection event. Once a Data Channel packet is received from the peer device (~14ms mark in our case), the connection is considered established.
Data Channel PDUs are split into Link Layer Data PDUs and Link Layer Control PDUs. The last one can be used both by master and slave after successful connection establishment to change Link Layer connection parameters, ask for supported features, change maximum PDU size, terminate connection etc. My TI SensorTag exchanged 1 packet of that type with the mobile application just after
CONNECT_IND. It was sent in both directions to determine the supported version of the Bluetooth specification:
Data Channel PDU can be only transmitted over physical channels which are not used for advertisement data. That’s why the Data Channel Header can be completely different than the one previously discussed in Advertisement Data PDUs. It has the following fields:
0b11, CS Vol 6, Part B, 2.4) - this value defines the type of the Data Channel PDU:
10- the start of LL Data PDU, which contains the whole L2CAP message or first fragment of it (explained in next section).
01- the continuation of LL Data PDU or Empty PDU (length set to zero). The remaining fragments of L2CAP messages are sent by this packet if Empty PDU is not specified.
11- LL Control PDU.
- NESN (1bit:
0b00: , CS Vol 6, Part B, 4.5.9) - the next sequence number expected. It is used by the slave device to acknowledge the received packet or request resending the last Data Channel PDU sent. If SN doesn’t match NESN packet, it is ignored by slave.
- SN (1bit:
0b00, CS Vol 6, Part B, 4.5.9) - Sequence number. It is used to identify packets sent by a master. If SN matches NESN, the previous packet is retransmitted by master.
- MD (1bit:
0b00, CS Vol 6, Part B, 4.5.6) - More data flag, which can be set both by slave and a master. If the master sets this value to 0, the connection event is closed and no more packets are exchanged.
- Length (1 byte:
0x06) - the length of a payload (excluding header).
The first byte of LL Control Payload (CS Vol 6, Part B, 2.4.2) is an opcode, which defines both type and length of the remaining part. In my captured trace 0x0C defines
LL_VERSION_IND. The master notified that it supports Bluetooth LE version 5.0 (
0x09) and uses Broadcom (
0x0F) controller with
0x420E version. The slave notified that it supports Bluetooth LE version 4.0 (
0x06) and uses Texas Instruments (
0x0D) controller with
0x0140 version. All the available Link Layer Controls PDUs are described in CS Vol 6, Part B, 2.4.2.
Today, we’ve learned how the low-level packets specific to Low Energy host controller are structured. We know how to recognize and analyze the advertisement procedure as well as the connection establishment. We’ve learned how to manage the link layer configuration. Additionally, you can start to sniff bluetooth packets with Wireshark application by yourself. In the upcoming second part, we will learn about L2CAP and some other higher level protocols which work on top of ED/BDR as well. We will cover the discovery procedure and the characteristic operations by example.
Staff Software Engineer