Open wahadatjan opened 3 months ago
Can you please share a minimal reproducible test case?
Likely, you are doing exactly what the error says and trying to start a scanner while another scanner is still running.
@dlech it is a django project which i am trying to run on raspberry pi where i am trying to call a django view for scanning the ble devices but it crashes
async def scan_devices(request):
async def scan_callback(device, advertising_data):
# Process advertising data here
print("Device:", device.address)
print("Advertisement Data:", advertising_data)
# You can extract specific data based on keys or parsing formats
devices = []
async with BleakScanner(scan_callback) as scanner:
await scanner.start() # Start scanning for devices
# Wait for a short duration (adjust as needed)
await asyncio.sleep(5)
await scanner.stop() # Stop scanning after the wait time
devices = scanner.discovered_devices # Get discovered devices
formatted_devices = [{
'uuid': str(device.address),
'name': device.name if device.name else 'Unknown',
'adv_data': "advertising_data" # Include advertising data in the response
} for device in devices]
return JsonResponse({'devices': formatted_devices})
If two requests can come in within the 5 second scan time, this could cause the problem. You could protect against concurrent scanning by using an asyncio.Lock
(maybe a good idea to return a busy error if the lock is held because of the long wait time?).
When using BlueZ, running btmon
in a separate terminal may help reveal what is happening. It can show a more verbose meaning to the error message as multiple things can produce org.bluez.Error.InProgress
.
@dlech connecting to multiple device is working fine but when it comes to get data from characteristics from different connected devices it gives data from a single device only .
when it comes to get data from characteristics from different connected devices it gives data from a single device only .
Can you please start a new issue with a minimal reproducible test case and Bluetooth packet capture logs and Bleak debug logs showing the issue?
We're having the same problem on bleak-0.21.1 and 0.22.2. It can work a few times before we get the error. The process exits before the next run starts so there shouldn't be any stray scanners laying around.
btmon:
@ MGMT Command: Start Service Discovery (0x003a) plen 4
Address type: 0x06
LE Public
LE Random
RSSI: invalid (0x7f)
UUIDs: 0
< HCI Command: LE Set Random Address (0x08|0x0005) plen 6
Address: 2A:F1:57:42:D3:68 (Non-Resolvable)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7
Type: Active (0x01)
Interval: 11.250 msec (0x0012)
Window: 11.250 msec (0x0012)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
@ MGMT Event: Command Complete (0x0001) plen 4
Start Service Discovery (0x003a) plen 1
Status: Authentication Failed (0x05)
Address type: 0x06
LE Public
LE Random
Updating the firmware using rpi-update
seems to have solved it on our end. I haven't been able to trigger it anymore.
Description
when i tried to scan the ble devices it gives me this error while running code on Raspberry pi