← index #984Issue #734
Off-topic · high · value 0.871
QUERY · ISSUE

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

openby maxwangwyfopened 2025-03-12updated 2025-03-12

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())

CANDIDATE · ISSUE

Timeout when activating ble on RP2040 (Pi Pico W)

closedby griffinsteffy19opened 2023-09-24updated 2024-11-14

I am running a bluetooth service that currently just as a rx service with write and capture both set to true. I am getting in consent results when reloading the device after awhile without being powered. I continue to get the following error...
File "App/TX1/bleService.py", line 50, in __init__ File "aioble/server.py", line 328, in register_services File "aioble/core.py", line 38, in ensure_active OSError: [Errno 110] ETIMEDOUT

# Ble Service Module

# Module Includes

# Common Application Modules
from App.common.logModule import Log

# Core Libaries
import bluetooth
import uasyncio as asyncio
from micropython import const
import aioble
import random
import struct

# Ble Provision Service Logger
log = Log("bleS", 'TRACE')

# Module Defines
RX_TIMEOUT_MS = 5000

# Uart Service UUID
UART_SERVICE_UUID = bluetooth.UUID(UART_UUID)

# RX Characteristic UUID
UART_RX_CHARACTERISTIC_UUID = bluetooth.UUID(RX_UUID)

# How frequently to send advertising beacons.
_ADV_INTERVAL_MS = 250_000


class BLEService:
    def __init__(self, dataCb, advName="CTdvc"):
        self.name = advName
        self.dataCb = dataCb
        self.isStarted = False
        self.advTask = None
        self.rxCbTask = None

        # Register GATT server.
        self.uart_service = aioble.Service(UART_SERVICE_UUID)
        self.rx_characteristic = aioble.Characteristic(
            self.uart_service, UART_RX_CHARACTERISTIC_UUID, write=True, capture=True
        )
        log.debug('Registering Services...')
        aioble.register_services(self.uart_service)
        log.info('Services Registered')

    async def rxCb(self):
        while True:
            await self.rx_characteristic.written()
            try:
                data = self.rx_characteristic.read().decode('utf-8')
                print(f"written: {data}")
                if(None != self.dataCb):
                    await self.dataCb(data)
            except UnicodeError:
                log.error('Unicode Error during decode')
        self.rxCbTask = None

    async def advertisingTask(self):
        while True:
            async with await aioble.advertise(
                _ADV_INTERVAL_MS,
                name=self.name,
                services=[UART_SERVICE_UUID],

            ) as connection:
                print("Connection from", connection.device)

                await connection.disconnected()

        self.advTask = None

    def started(self):
        return self.isStarted

    async def start(self):
        log.trace('start')
        if(not self.isStarted):

            log.debug("staring BLE with name=%s" % self.name)
            self.rxCbTask = asyncio.create_task(self.rxCb())
            self.advTask = asyncio.create_task(self.advertisingTask())
            self.isStarted = True
        else:
            log.debug('already started')

        while True:
            asyncio.sleep(0.5)

    async def stop(self):
        log.trace('stop')
        self.isStarted = False

        if(None != self.advTask):
            self.advTask.cancel()
            self.advTask = None

        if(None != self.rxCbTask):
            self.rxCbTask.cancel()
            self.rxCbTask = None

Above this module I have initialize it, then call start within a running while loop task

6 comments
griffinsteffy19 · 2023-09-24

It looks like this might be coming form the underlying bluetooth library:

>>> import bluetooth
>>> ble = bluetooth.BLE()
>>> ble.active(True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 110] ETIMEDOUT

Here is the UF2 I am running: v1.20.0-198-g0eacdeb1c

I found a previous issue here which mentioned activating wifi first, but that did not seem to help. However that was for a ESP32...

>>> import network
>>> wlan = network.WLAN(network.STA_IF)
>>> wlan.active(True)
None
>>> ble.active(True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 110] ETIMEDOUT
MattyBoy4444 · 2023-10-25

Did you figure out the problem?

griffinsteffy19 · 2023-10-25

No, most consistent way to fix the issue is to reload the uf2 file.

I have seen this issue with the latest RPI_PICO_W-20231005-v1.21.0.uf2 file as well (I think that is the right link....regardless I have seen the issue on 1.21.0 with ble enabled)

jimmo · 2023-10-26

There was a recent fix https://github.com/micropython/micropython/pull/12647 that might explain this? Do you see the same issue on the preview builds?

griffinsteffy19 · 2023-10-26

I haven't ran any of the recent preview builds (after 1.21.0). I'll do some testing tomorrow with the latest preview and see what happens.

griffinsteffy19 · 2024-11-14

Leaving a final update 1 Year Later. It seems like that fix mentioned has solved it, but don't have any A/B tests to prove it. I'll close this issue for now.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied