Got it! You want to track Bluetooth connection status by analyzing a .hci log file (a Bluetooth Host Controller Interface capture).
How to Track Connection Status from a .hci Log File
The .hci log file records the raw HCI packets exchanged between the Host and Controller. By parsing these packets and focusing on connection-related HCI events, you can deduce when a Bluetooth device gets connected or disconnected.
Step 1: Understand Key HCI Events Representing Connection States
| Event Name | Event Code (Hex) | Description |
|---|
| Connection Complete | 0x03 | Connection has been established |
| Disconnection Complete | 0x05 | Connection has been terminated |
| LE Meta Event | 0x3E | Contains subevents for BLE connection-related events |
Step 2: Important Events and Subevents for Classic BR/EDR and BLE
Step 3: How to Parse the .hci File to Track Connection State
You can use packet analysis tools like Wireshark or implement a parser that reads the .hci log file format and interprets the data.
Using Wireshark (Recommended for Initial Analysis):
-
Open the .hci file in Wireshark.
-
Apply display filters to catch connection or disconnection events:
- For BR/EDR Connection Complete:
btcommon.evt.code == 0x03
- For BR/EDR Disconnection Complete:
btcommon.evt.code == 0x05
- For BLE LE Meta Events (filter LE subevents):
btcommon.evt.code == 0x3e
- Then check the Subevent code for connection or disconnection:
- Connection Complete:
btcommon.le.subevent == 0x01
- Disconnection Complete:
btcommon.le.subevent == 0x05
-
The timestamps and BD_ADDRs in these packets will let you know exactly when devices connected or disconnected.
Step 4: Example of the HCI Packet Structure (Simplified)
| Field | Description |
|---|
| Packet Type | Command, Event, ACL, SCO, ISO |
| Event Code | Identifies event type |
| Parameter Length | Length of the event parameters |
| Parameters | Event-specific data |
Step 5: Programmatic Parsing Example — What to Look For in Raw HCI Data
- When reading
.hci files (typically binary), parse packets by reading the packet header, event type, etc.
- For each Event Packet (
Packet Type = 0x04) with Event Code:
- 0x03: Connection Complete → means a device is now connected.
- 0x05: Disconnection Complete → device disconnected.
- 0x3E: LE Meta Event → check the subevent.
Sample pseudo-code for tracking connection states from a .hci file:
def parse_hci_log(filename):
with open(filename, 'rb') as f:
while True:
packet_type = f.read(1)
if not packet_type:
break
if packet_type == b'\x04': # Event packet
event_code = f.read(1)
param_len = ord(f.read(1))
params = f.read(param_len)
if event_code == b'\x03': # Connection Complete Event
# Parse BD_ADDR from params, update connection state dictionary
print("Device Connected:", parse_bdaddr(params))
elif event_code == b'\x05': # Disconnection Complete
print("Device Disconnected")
elif event_code == b'\x3E': # LE Meta Event
subevent_code = params[0]
if subevent_code == 0x01: # LE Connection Complete
print("BLE Device Connected:", parse_bdaddr(params[7:13]))
elif subevent_code == 0x05: # LE Disconnection Complete
print("BLE Device Disconnected")
else:
# Skip or parse other packet types as needed
pass
parse_bdaddr() would extract the Bluetooth device address from the appropriate bytes.
Summary
- Open the
.hci file in Wireshark for easy visual analysis.
- Filter by Connection Complete and Disconnection Complete events.
- Alternatively, parse the
.hci binary to extract those events and their parameters.
- Use the event timestamps and BD_ADDR to track when each device connects or disconnects.
If you want, I can help with a script to parse .hci files to extract connection status or explain the .hci file format in more detail. Just ask!