You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
2 months ago
|
from bleak import BleakScanner, BleakClient
|
||
|
import asyncio
|
||
|
|
||
|
async def get_services(mac_address: str):
|
||
|
async with BleakClient(mac_address) as client:
|
||
|
svcs = client.services
|
||
|
return svcs
|
||
|
|
||
|
async def find_and_get_services(device_name: str):
|
||
|
# 发现所有设备
|
||
|
devices = await BleakScanner.discover()
|
||
|
|
||
|
# 遍历设备列表,查找目标设备
|
||
|
for device in devices:
|
||
|
if device.name == device_name:
|
||
|
print(f"Found device: {device.name}, MAC Address: {device.address}")
|
||
|
|
||
|
# 获取服务
|
||
|
services = await get_services(device.address)
|
||
|
if services:
|
||
|
last_service = services.services[40]
|
||
|
return {
|
||
|
"device_name": device.name,
|
||
|
"mac_address": device.address,
|
||
|
"last_service_uuid": last_service.uuid,
|
||
|
}
|
||
|
else:
|
||
|
print("No services found for the device.")
|
||
|
return None
|
||
|
|
||
|
# 如果没有找到设备
|
||
|
print(f"Device with name '{device_name}' not found.")
|
||
|
return None
|
||
|
|
||
|
# 设备名称
|
||
|
device_name = "scent08d1f90efa9a"
|
||
|
|
||
|
# 调用函数并获取返回值
|
||
|
result = asyncio.run(find_and_get_services(device_name))
|
||
|
print(result)
|