Skip to content

Commit c5c955e

Browse files
committedJul 11, 2024
add decoding device class in the bluetooth scanner
1 parent 904d289 commit c5c955e

File tree

1 file changed

+77
-17
lines changed

1 file changed

+77
-17
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,85 @@
1-
# Import bluetooth from the PyBluez module.
21
import bluetooth
32

3+
# Major and Minor Device Class definitions based on Bluetooth specifications
4+
MAJOR_CLASSES = {
5+
0: "Miscellaneous",
6+
1: "Computer",
7+
2: "Phone",
8+
3: "LAN/Network Access",
9+
4: "Audio/Video",
10+
5: "Peripheral",
11+
6: "Imaging",
12+
7: "Wearable",
13+
8: "Toy",
14+
9: "Health",
15+
10: "Uncategorized"
16+
}
17+
18+
MINOR_CLASSES = {
19+
# Computer Major Class
20+
(1, 0): "Uncategorized Computer", (1, 1): "Desktop Workstation",
21+
(1, 2): "Server-class Computer", (1, 3): "Laptop", (1, 4): "Handheld PC/PDA",
22+
(1, 5): "Palm-sized PC/PDA", (1, 6): "Wearable computer",
23+
# Phone Major Class
24+
(2, 0): "Uncategorized Phone", (2, 1): "Cellular", (2, 2): "Cordless",
25+
(2, 3): "Smartphone", (2, 4): "Wired modem or voice gateway",
26+
(2, 5): "Common ISDN Access",
27+
# LAN/Network Access Major Class
28+
(3, 0): "Fully available", (3, 1): "1% to 17% utilized",
29+
(3, 2): "17% to 33% utilized", (3, 3): "33% to 50% utilized",
30+
(3, 4): "50% to 67% utilized", (3, 5): "67% to 83% utilized",
31+
(3, 6): "83% to 99% utilized", (3, 7): "No service available",
32+
# Audio/Video Major Class
33+
(4, 0): "Uncategorized A/V", (4, 1): "Wearable Headset", (4, 2): "Hands-free Device",
34+
(4, 3): "Microphone", (4, 4): "Loudspeaker", (4, 5): "Headphones", (4, 6): "Portable Audio",
35+
(4, 7): "Car audio", (4, 8): "Set-top box", (4, 9): "HiFi Audio Device",
36+
(4, 10): "VCR", (4, 11): "Video Camera", (4, 12): "Camcorder",
37+
(4, 13): "Video Monitor", (4, 14): "Video Display and Loudspeaker",
38+
(4, 15): "Video Conferencing", (4, 16): "Gaming/Toy",
39+
# Peripheral Major Class
40+
(5, 0): "Not Keyboard/Not Pointing Device", (5, 1): "Keyboard",
41+
(5, 2): "Pointing device", (5, 3): "Combo Keyboard/Pointing device",
42+
# Imaging Major Class
43+
(6, 0): "Display", (6, 1): "Camera", (6, 2): "Scanner", (6, 3): "Printer",
44+
# Wearable Major Class
45+
(7, 0): "Wristwatch", (7, 1): "Pager", (7, 2): "Jacket",
46+
(7, 3): "Helmet", (7, 4): "Glasses",
47+
# Toy Major Class
48+
(8, 0): "Robot", (8, 1): "Vehicle",
49+
(8, 2): "Doll / Action figure",
50+
(8, 3): "Controller", (8, 4): "Game",
51+
# Health Major Class
52+
(9, 0): "Undefined", (9, 1): "Blood Pressure Monitor",
53+
(9, 2): "Thermometer", (9, 3): "Weighing Scale",
54+
(9, 4): "Glucose Meter", (9, 5): "Pulse Oximeter",
55+
(9, 6): "Heart/Pulse Rate Monitor", (9, 7): "Health Data Display",
56+
(9, 8): "Step Counter", (9, 9): "Body Composition Analyzer",
57+
(9, 10): "Peak Flow Monitor", (9, 11): "Medication Monitor",
58+
(9, 12): "Knee Prosthesis", (9, 13): "Ankle Prosthesis",
59+
# More specific definitions can be added if needed
60+
}
61+
62+
def parse_device_class(device_class):
63+
major = (device_class >> 8) & 0x1F # divide by 2**8 and mask with 0x1F (take the last 5 bits)
64+
minor = (device_class >> 2) & 0x3F # divide by 2**2 and mask with 0x3F (take the last 6 bits)
65+
major_class_name = MAJOR_CLASSES.get(major, "Unknown Major Class")
66+
minor_class_key = (major, minor)
67+
minor_class_name = MINOR_CLASSES.get(minor_class_key, "Unknown Minor Class")
68+
return major_class_name, minor_class_name
69+
70+
471
def scan_bluetooth_devices():
572
try:
6-
# Discover Bluetooth devices with names and classes.
7-
discovered_devices = bluetooth.discover_devices(lookup_names=True, lookup_class=True)
8-
9-
# Display information about the scanning process.
10-
print('[!] Scanning for active devices...')
11-
print(f"[!] Found {len(discovered_devices)} Devices\n")
12-
13-
# Iterate through discovered devices and print their details.
73+
discovered_devices = bluetooth.discover_devices(duration=8, lookup_names=True, lookup_class=True)
74+
print('[!] Scanning for Bluetooth devices...')
75+
print(f"[!] Found {len(discovered_devices)} Devices")
1476
for addr, name, device_class in discovered_devices:
15-
print(f'[+] Name: {name}')
16-
print(f'[+] Address: {addr}')
17-
print(f'[+] Device Class: {device_class}\n')
18-
77+
major_class, minor_class = parse_device_class(device_class)
78+
print(f"[+] Device Name: {name}")
79+
print(f" Address: {addr}")
80+
print(f" Device Class: {device_class} ({major_class}, {minor_class})")
1981
except Exception as e:
20-
# Handle and display any exceptions that occur during device discovery.
2182
print(f"[ERROR] An error occurred: {e}")
2283

23-
24-
# Call the Bluetooth device scanning function when the script is run
25-
scan_bluetooth_devices()
84+
if __name__ == "__main__":
85+
scan_bluetooth_devices()

0 commit comments

Comments
 (0)
Please sign in to comment.