-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth-scan.py
More file actions
51 lines (43 loc) · 1.76 KB
/
bluetooth-scan.py
File metadata and controls
51 lines (43 loc) · 1.76 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
# Bluetooth LE scanner
# Prints the name and address of every nearby Bluetooth LE device
import time
import asyncio
from winrt.windows.devices import radios
from bleak import BleakScanner, BleakClient
TARGET_NAME = "Your_device" # Replace with your target device name
TARGET_ADRESS = None # Will be set when the target device is found
NB_RETRIES = 5
TRIES = 1
async def bluetooth_power(turn_on):
all_radios = await radios.Radio.get_radios_async()
for this_radio in all_radios:
if this_radio.kind == radios.RadioKind.BLUETOOTH:
if turn_on:
result = await this_radio.set_state_async(radios.RadioState.ON)
else:
result = await this_radio.set_state_async(radios.RadioState.OFF)
async def main():
global TARGET_ADRESS
devices = await BleakScanner.discover()
for device in devices:
print(device)
if device.name == TARGET_NAME:
TARGET_ADRESS = device.address
if TARGET_ADRESS is None:
print(f"Target device {TARGET_NAME} not found. Retrying in 5 seconds... (Try {TRIES}/{NB_RETRIES})")
return False
else:
print(f"Found target device: {TARGET_NAME} at {TARGET_ADRESS}")
async with BleakClient(TARGET_ADRESS) as client:
# we’ll do the read/write operations here
print(f"Succesfully connected to {TARGET_NAME} in {TRIES} tries.")
print(client.is_connected)
return True
if __name__ == '__main__':
for i in range(0, NB_RETRIES):
asyncio.run(bluetooth_power(True)) # Ensure Bluetooth is on
print("Scanning for Bluetooth LE devices...")
stop = asyncio.run(main())
if stop:
break # Exit loop if connected
TRIES += 1