-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbtscanner.py
More file actions
162 lines (135 loc) · 6.09 KB
/
btscanner.py
File metadata and controls
162 lines (135 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import asyncio
import sys
from bleak import BleakScanner
from datetime import datetime
async def scan_bluetooth(duration=10, show_details=False):
"""
Scans for Bluetooth Low Energy (BLE) devices.
Args:
duration (int): Scan duration in seconds
show_details (bool): Show detailed information (RSSI, manufacturer data, etc.)
Returns:
list or dict: List of BLEDevice objects or dict with advertisement data
"""
print(f"Scanning for BLE devices for {duration} seconds...")
print("Please wait...\n")
if show_details:
# Returns dict: {address: (BLEDevice, AdvertisementData)}
devices = await BleakScanner.discover(timeout=duration, return_adv=True)
else:
# Returns list of BLEDevice objects
devices = await BleakScanner.discover(timeout=duration, return_adv=False)
return devices
async def main():
"""Main function to run the Bluetooth scanner."""
# Parse command line arguments
duration = 10 # Default scan duration
show_details = False
args = sys.argv[1:]
for arg in args:
if arg in ['-d', '--details']:
show_details = True
elif arg.startswith('-t'):
try:
duration = int(arg.split('=')[1])
except:
print("Usage: -t=<seconds> for scan duration")
sys.exit(1)
elif arg in ['-h', '--help']:
print("Usage: btscanner.py [options]")
print("\nOptions:")
print(" -d, --details Show detailed information (RSSI, manufacturer, services)")
print(" -t=<seconds> Scan duration in seconds (default: 10)")
print("\nExamples:")
print(" btscanner.py")
print(" btscanner.py -d")
print(" btscanner.py -t=20 -d")
sys.exit(0)
print("="*70)
print("Bluetooth Device Scanner")
print("="*70)
print(f"Scan Duration: {duration} seconds")
print(f"Detailed Info: {'Enabled' if show_details else 'Disabled'}")
print("="*70 + "\n")
try:
all_devices = []
# Scan for BLE devices
ble_devices = await scan_bluetooth(duration, show_details)
# Process BLE devices
if show_details:
# ble_devices is a dict: {address: (BLEDevice, AdvertisementData)}
for address, (ble_device, adv_data) in ble_devices.items():
device_info = {
'address': ble_device.address,
'name': ble_device.name or 'Unknown',
'rssi': adv_data.rssi,
'manufacturer': adv_data.manufacturer_data,
'services': list(adv_data.service_uuids)
}
all_devices.append(device_info)
else:
# ble_devices is a list of BLEDevice objects
for device in ble_devices:
device_info = {
'address': device.address,
'name': device.name or 'Unknown',
'rssi': None,
'manufacturer': None,
'services': []
}
all_devices.append(device_info)
if not all_devices:
print("\nNo Bluetooth devices found.")
print("\nTroubleshooting:")
print("- Make sure Bluetooth is enabled on your computer")
print("- Ensure nearby Bluetooth devices are discoverable")
print("- Try increasing scan duration with -t=<seconds>")
return
# Sort by name, then address
all_devices.sort(key=lambda x: (x['name'], x['address']))
print(f"\n{'='*70}")
print(f"Scan complete! Found {len(all_devices)} Bluetooth device(s)")
print("="*70)
# Display results
print("\nDiscovered Bluetooth Devices:\n")
for i, dev in enumerate(all_devices, 1):
print(f"{i}. {dev['name']}")
print(f" Address: {dev['address']}")
if show_details:
if dev['rssi'] is not None:
print(f" Signal Strength (RSSI): {dev['rssi']} dBm")
if dev['manufacturer']:
print(f" Manufacturer Data: {dev['manufacturer']}")
if dev['services']:
print(f" Services: {', '.join(dev['services'][:3])}" +
(f" (+{len(dev['services'])-3} more)" if len(dev['services']) > 3 else ""))
print()
# Save to file
output_file = "active_bt.txt"
with open(output_file, "w", encoding="utf-8") as f:
f.write(f"Bluetooth Scan Results - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Scan Duration: {duration} seconds\n")
f.write(f"Detailed Info: {'Enabled' if show_details else 'Disabled'}\n")
f.write("="*70 + "\n\n")
for i, dev in enumerate(all_devices, 1):
f.write(f"{i}. {dev['name']}\n")
f.write(f" Address: {dev['address']}\n")
if show_details:
if dev['rssi'] is not None:
f.write(f" Signal Strength (RSSI): {dev['rssi']} dBm\n")
if dev['manufacturer']:
f.write(f" Manufacturer Data: {dev['manufacturer']}\n")
if dev['services']:
f.write(f" Services: {', '.join(dev['services'])}\n")
f.write("\n")
print(f"Results saved to {output_file}")
except Exception as e:
print(f"\nError during Bluetooth scan: {e}")
print("\nPossible issues:")
print("- Bluetooth adapter not found or not enabled")
print("- Insufficient permissions (try running as administrator)")
print("- Bluetooth drivers not properly installed")
sys.exit(1)
if __name__ == "__main__":
# Run the async main function
asyncio.run(main())