BLE advertising and scanning in IOT Protocols - Time & Space Complexity
When devices use BLE advertising and scanning, they send and listen for signals repeatedly.
We want to understand how the time spent grows as more devices advertise or scan.
Analyze the time complexity of the following BLE scanning process.
startScanning() {
for each channel in BLE_channels {
listenForAdvertisements(channel, scanWindow);
}
collectAdvertisements();
}
This code listens on multiple BLE channels one by one during a scan window and collects advertisements from devices.
Look for repeated actions in the scanning process.
- Primary operation: Loop over BLE channels to listen for advertisements.
- How many times: Once per channel, usually 3 channels in BLE.
The scanning time grows with the number of channels scanned and the number of advertisements received.
| Input Size (n devices advertising) | Approx. Operations |
|---|---|
| 10 | Listening on 3 channels + collecting 10 advertisements |
| 100 | Listening on 3 channels + collecting 100 advertisements |
| 1000 | Listening on 3 channels + collecting 1000 advertisements |
Pattern observation: Listening time is fixed by channels, but collecting advertisements grows with device count.
Time Complexity: O(n)
This means the time to process advertisements grows linearly with the number of devices advertising.
[X] Wrong: "Scanning time grows with the number of channels scanned only."
[OK] Correct: The number of channels is fixed and small, but the time to handle advertisements depends on how many devices send signals.
Understanding how scanning time grows helps you design efficient BLE applications and shows you can think about real device communication limits.
"What if we scanned multiple channels simultaneously instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand BLE advertising role
BLE advertising sends small data packets repeatedly to announce presence.Step 2: Differentiate from scanning and connection
Advertising is broadcasting, not scanning or connecting.Final Answer:
To broadcast small packets of data repeatedly to nearby devices -> Option AQuick Check:
Advertising = broadcast small data [OK]
- Confusing advertising with scanning
- Thinking advertising establishes connections
- Assuming advertising encrypts data
bluetoothctl?Solution
Step 1: Recall bluetoothctl scan command
The correct command to start scanning is exactlyscan on.Step 2: Check other options for syntax
Other options are incorrect commands or invalid syntax in bluetoothctl.Final Answer:
scan on -> Option DQuick Check:
bluetoothctl scan on = start scanning [OK]
- Using 'start scan' instead of 'scan on'
- Typing commands not supported by bluetoothctl
- Confusing scan commands with advertising commands
devices = scanner.scan(5.0)
for dev in devices:
print(dev.addr)
What will this code output?
Solution
Step 1: Understand scan() usage with timeout
The scan method scans for devices for given seconds and returns device objects.Step 2: Identify printed attribute
Printingdev.addroutputs device MAC addresses found.Final Answer:
List of device addresses found during 5 seconds scan -> Option BQuick Check:
scan(5.0) returns devices; dev.addr prints addresses [OK]
- Thinking scan() takes no arguments
- Confusing device address with device name
- Assuming scan always returns empty list
scanner = Scanner()
devices = scanner.scan()
for dev in devices:
print(dev.addr)
What is the likely problem?
Solution
Step 1: Check scan() method requirements
scan() requires a timeout argument to specify scan duration.Step 2: Understand effect of missing timeout
Without timeout, scan may return immediately with no devices.Final Answer:
Missing scan timeout argument causes no devices found -> Option CQuick Check:
scan() needs timeout to find devices [OK]
- Omitting scan timeout argument
- Assuming dev.addr is invalid
- Thinking scan() uses 'start' keyword
Solution
Step 1: Understand filtering by service UUID
Service UUIDs appear in scan data, not in address or name.Step 2: Use list comprehension with getScanData()
getScanData() returns advertised data; check if UUID '1234' is present.Final Answer:
Use a list comprehension: [dev for dev in devices if '1234' in dev.getScanData()] -> Option AQuick Check:
Filter by UUID in scan data using list comprehension [OK]
- Checking UUID in device address
- Filtering by device name instead of UUID
- Using non-existent filter() method on devices
Ready to prove it?
Pass the test and this pattern moves unknown
