Skip to content

"Why am I unable to read the keyboard input?" #984

Open
@maxwangwyf

Description

@maxwangwyf

import aioble
import asyncio
import bluetooth

async def scan_bluetooth():
print("Starting BLE scan...")
async with aioble.scan(
duration_ms=10000, # 扫描 10 秒
interval_us=300000, # 间隔 300 毫秒
window_us=300000, # 窗口 300 毫秒
active=True # 主动请求额外数据
) as scanner:
async for result in scanner:
name = result.name() or "Unknown"
addr = result.device.addr_hex()
rssi = result.rssi
services = result.services()

        if "0d:f9:6d:6a:75:c9" in addr:
            print("找到此设备")
            print(result.services)
            return result.device
return None

async def main():
device = await scan_bluetooth()
if not device:
print("未找到设备")
return

try:
    print(f"正在连接 {device.addr_hex()}...")
    connection = await device.connect(timeout_ms=2000)  # 设置连接超时
    print("连接成功",connection)


except asyncio.TimeoutError:
    print("连接超时")
except Exception as e:
    print(f"连接失败: {e}")
async with connection:
    try:
        hid_service=0x1812
        key_input=0x2A4D
        mouse_input=0x2A33
        
        ser=bluetooth.UUID(hid_service)
        char=bluetooth.UUID(key_input)
        hid_service = await connection.service(ser)
        print(hid_service)
        hid_characteristic = await hid_service.characteristic(char)
        print(hid_characteristic)
    except asyncio.TimeoutError:
        print("Timeout discovering services/characteristics")
        return
    while True:
            # 尝试读取特征的值
        data = await hid_characteristic.read()
        print("Received key input data:", data)

运行主程序

asyncio.run(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions