"Why am I unable to read the keyboard input?"
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())
Timeout when activating ble on RP2040 (Pi Pico W)
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
It looks like this might be coming form the underlying
bluetoothlibrary: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...
Did you figure out the problem?
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)
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?
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.
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.