-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
65 lines (56 loc) · 2.11 KB
/
scanner.py
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
import asyncio
from bleak import BleakScanner, BleakClient
import serial.tools.list_ports
from classifier import classify_device
from utils import load_config
config = load_config('config.yaml')
discovered_devices = []
def detection_callback(device, advertisement_data):
discovered_devices.append((device, advertisement_data))
async def discover_bluetooth_devices(scan_time=10):
global discovered_devices
discovered_devices = []
scanner = BleakScanner(detection_callback, filters={"DuplicateData": False})
await scanner.start()
await asyncio.sleep(scan_time)
await scanner.stop()
ports = serial.tools.list_ports.comports()
for port in ports:
discovered_devices.append((port.description, port.device))
return discovered_devices
async def get_device_info(device):
try:
async with BleakClient(device) as client:
info = {
"name": device.name,
"address": device.address,
"rssi": device.rssi,
}
services = await client.get_services()
for service in services:
info[service.uuid] = []
for char in service.characteristics:
try:
value = await client.read_gatt_char(char.uuid)
info[service.uuid].append({char.uuid: value})
except Exception as e:
info[service.uuid].append({char.uuid: str(e)})
return info
except Exception as e:
return {"error": str(e)}
async def connect_to_device(mac):
device = next((d for d, _ in discovered_devices if getattr(d, 'address', None) == mac), None)
if not device:
return {"error": "Dispositivo no encontrado"}
try:
client = BleakClient(device)
await client.connect()
return {"client": client, "status": "connected"}
except Exception as e:
return {"error": str(e)}
async def disconnect_from_device(client):
try:
await client.disconnect()
return {"status": "disconnected"}
except Exception as e:
return {"error": str(e)}