Skip to content

Commit c8f4bf5

Browse files
add BLE performance guide
1 parent 1458d80 commit c8f4bf5

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

docs.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,9 @@
834834
},
835835
{
836836
"path": "docs/api/connectivity/bluetooth/SecurityManager.md"
837+
},
838+
{
839+
"path": "docs/api/connectivity/bluetooth/Optimising_for_performance.md"
837840
}
838841
]
839842
},
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Optimising application for throughput and power consumption
2+
3+
BLE devices are usually battery powered so performance might mean different things in different applications. You have
4+
to decide what is more important in yours - minimising power consumption on one or both sides of the communication or
5+
maximising throughput and/or latency. Some optimisation steps can in fact achieve both.
6+
7+
This guide will discuss some trade-offs that should be considered and best practices that improve performance on all
8+
fronts.
9+
10+
## Power consumtpion
11+
12+
Any radio activity will consume power. Depending on what the stack is doing you have to power the radio even when no
13+
data is being sent. It is important to understand when radio is active.
14+
15+
### Connections
16+
17+
The most intuitive power consumption rate to understand is when using connections. Each device needs to send a packet at
18+
below supervision timeout frequency to maintain a connection regardless of data transfer. This is the absolute minimum
19+
power consumption. Additionally, normally power is consumed at every connection event (exchange of packets between
20+
central and peripheral).
21+
22+
It's worth considering if keeping the connection active is worth it. Connection in BLE is extremely fast and if you plan
23+
to only send a quick burst of data every minute it is better to connect and disconnect each time.
24+
25+
The cost of the connection is proportionate to the negotiable connection interval. This can be set during `connect` or
26+
later through `updateConnectionParameters`. The higher the interval the more often radio is active. This is especially
27+
important for the peripheral which needs to enable the radio to receive packets.
28+
29+
This can be further helped through setting a high `slaveLatency` parameter. This allows the peripheral to skip
30+
connection events and save power not just by not sending any packets but by not even listening. This is not free for
31+
central as it increases latency of data transmission. Central may have to attempt sending data multiple times before the
32+
peripheral accepts the transmission.
33+
34+
### Advertising and scanning
35+
36+
Power draw during advertising is proportional to the advertising interval. Additionally, if the advertising is
37+
connectable or scannable it means the advertiser needs to listen on the radio after each advertisement for potential
38+
connection of scan requests.
39+
40+
Scanning power draw is proportional to time spent scanning. The interaction between scanning an advertising means that
41+
the less power the advertiser spends advertising, the more power the scanner will have to spend to see the advertising
42+
packets. The decision on balance will be dictated by your design of your devices (which one is more constrained).
43+
44+
### Connection vs advertising
45+
46+
Instead of connecting to the device you can consider transferring data in advertising packets. This depends on the
47+
nature of the data.
48+
49+
A transfer over a connection will allow you to use the ATT protocol, this can handle acknowledgement for you. This might
50+
be a good choice if you're sending data that must get through reliably.
51+
52+
If your data is non-critical then advertising might be cheaper. You might have to accept less reliability and no built
53+
in acknowledgment. Additional benefit is that multiple devices may receive the data and each scanner may make their own
54+
decisions about power consumption.
55+
56+
### Periodic advertising
57+
58+
Periodic advertising allows you to get best of both worlds by having the power characteristics of advertising for the
59+
peripheral but also saving power for the scanner. After finding periodic advertising through `createSync` the scanner
60+
will only have to turn on the radio when the expected packet is due.
61+
62+
## Increasing throughput
63+
64+
### Modulation schemes
65+
66+
Depending on controller support different modulation schemes are available in BLE through `setPreferredPhys()` and
67+
`setPhy()`. While the coded PHY will increase reliability in noisy environments and increase range 2M PHY will increase
68+
the throughput saving power ber bit. If both devices support it and the signal quality is good then this is recommended
69+
to be enabled.
70+
71+
### Data length and ATT_MTU
72+
73+
Packet overhead strongly affects throughput. Newer controllers allow you to negotiate bigger MTUs thus decreasing the
74+
fragmentation overhead.
75+
76+
There are two separate MTUs to consider: the `ATT_MTU` (which affects ATT protocol operations) and data length extension
77+
(which affects transport packets). Increasing the sizes will increase memory usage but greatly increase throughput.
78+
`ATT_MTU` and data length are independent of each other.
79+
80+
The size of ATT_MTU doesn't have any other overhead than memory and should only be limited by your biggest attribute
81+
and available memory.
82+
83+
The default value of data length supported by all controllers is 23 octets. If both controllers support data length
84+
extension and a higher value is negotiated, the BLE stack will call `onDataLengthChange` in the `Gap::EventHandler`
85+
registered by the user. The supported length is set in the link layer with a maximum of 251. For Cordio Link Layer it
86+
is derived from the config option `cordio_ll.max-acl-size`.
87+
88+
Larger data length greatly increases throughput (although diminishing returns quickly set in above 80). The only
89+
potential drawback is in noisy environments where longer packets may cause slower effective transfer due to
90+
retransmissions (this is only related to data length, ATT_MTU does not affect this).
91+
92+
### Packet timings
93+
94+
If you're not constrained by battery power it might be tempting to use maximum/minimum values where possible.
95+
Advertising at maximum frequency and scanning continuously will speed up connecting. Setting intervals on connections
96+
will minimise latency and maximise number of connection events.
97+
98+
One key thing to consider when setting the connection interval low is that you are creating a boundary between which a
99+
sequence of packets must fit. This means that the last transfer must end before the next connection event starts. This
100+
dead time may become significant if the connection interval is short and packet length is long.
101+
102+
The connection interval shouldn't be shorter than what your data requires in terms of latency.
103+
104+
# Test and measure
105+
106+
Due to complexity of the stack the only reliable way to truly maximise performance is to test your application with
107+
representative data and measure the throughput and power usage. It's important to keep it mind that tweaking
108+
parameters by trial and error and fine tuning them will only be reliable as long as you control both devices (and even
109+
then the environment can change). If your device needs to communicate with an unknown device your fine-tuning should
110+
give way to sound principles since stack behaviour varies and you cannot test against all stacks.

0 commit comments

Comments
 (0)