Untitled

mail@pastecode.io avatar
unknown
python
7 days ago
1.4 kB
1
Indexable
Never
import asyncio
from bleak import BleakClient, BleakScanner

async def scan_devices():
    print("Scanning for Bluetooth devices...")
    devices = await BleakScanner.discover()
    for device in devices:
        print(f"Found device: {device.name} - {device.address}")

async def discover_services(device_address):
    async with BleakClient(device_address) as client:
        services = await client.get_services()
        for service in services:
            print(f"Service: {service.uuid}")
            for characteristic in service.characteristics:
                print(f"  Characteristic: {characteristic.uuid} (Properties: {characteristic.properties})")

async def query_vlf_bt(device_address, write_uuid, read_uuid):
    async with BleakClient(device_address) as client:
        command = "P01"
        await client.write_gatt_char(write_uuid, command.encode())
        
        response = await client.read_gatt_char(read_uuid)
        data = response.decode("utf-8").strip()
        print(f"Response from {command}: {data}")

async def main():
    await scan_devices()
    device_address = input("Enter the device address to connect to: ")
    await discover_services(device_address)
    write_uuid = input("Enter the write characteristic UUID: ")
    read_uuid = input("Enter the read characteristic UUID: ")
    await query_vlf_bt(device_address, write_uuid, read_uuid)

if __name__ == "__main__":
    asyncio.run(main())
Leave a Comment