IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.58k stars 490 forks source link

Failed to execute mgmt cmd 'scanend' #150

Open Owlie opened 7 years ago

Owlie commented 7 years ago

Raspberry pi 3 Model B

Using the example code from http://ianharvey.github.io/bluepy-doc/scanner.html#sample-code

from bluepy.btle import Scanner, DefaultDelegate

class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print "Discovered device", dev.addr
        elif isNewData:
            print "Received new data from", dev.addr

scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)

for dev in devices:
    print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
    for (adtype, desc, value) in dev.getScanData():
        print "  %s = %s" % (desc, value)

I get this error.

Running  /usr/local/lib/python2.7/dist-packages/bluepy/bluepy-helper
Sent:  le on

Got: 'rsp=$mgmt code=$success\n'
Sent:  scan

Got: 'rsp=$mgmt code=$busy\n'
Sent:  scanend

Got: 'rsp=$mgmt code=$protoerr\n'
Stopping  /usr/local/lib/python2.7/dist-packages/bluepy/bluepy-helper
Traceback (most recent call last):
  File "main.py", line 18, in <module>
    devices = scanner.scan(10.0)
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 631, in scan
    self.start()
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 576, in start
    self._mgmtCmd("scanend")
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 240, in _mgmtCmd
    "Failed to execute mgmt cmd '%s'" % (cmd))
bluepy.btle.BTLEException: Failed to execute mgmt cmd 'scanend'
ulfwin commented 7 years ago

I get the same error running "blescan" in the console. I'm running as root

markrages commented 7 years ago

This happens on one of my Bluetooth dongles ("0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)") but not the other one ("0a5c:21e8 Broadcom Corp. BCM20702A0 Bluetooth 4.0").

The error happens when a random address is set, then scanning is shut down after:

> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x00
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x00
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Random Address (0x08|0x0005) ncmd 1
    status 0x00
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x0c
    Error: Command Disallowed

There appears to be a race condition between subsequent invocations of "bluepy-helper".

This patch "fixes" it: (By "fixing" I mean it makes it not happen on my particular computer. I didn't dig down through the layers of abstraction to solve it properly.)

diff --git a/bluepy/btle.py b/bluepy/btle.py
index ec950e1..65e6a78 100755
--- a/bluepy/btle.py
+++ b/bluepy/btle.py
@@ -218,6 +218,7 @@ class BluepyHelper:
             self._helper.stdin.write("quit\n")
             self._helper.stdin.flush()
             self._helper.wait()
+            time.sleep(0.1)
             self._helper = None
         if self._stderr is not None:
             self._stderr.close()
brianpeiris commented 7 years ago

I ran into the error on my raspberry pi but it was because I forgot to enable the bluetooth device in the first place :/

EinSoldiatGott commented 7 years ago

@markrages Your delay also works for a frequent BTL Exception Device Disconnected when scanning Frequently.

I understand that it's a workaround but it helped to eliminate the frequent exceptions.

Thanks

okelet commented 6 years ago

Same here, but the patch doesn't work for me... :( Ubuntu 16.04 x64, bluepy version 1.1.2, Python 3.5.

okelet commented 6 years ago

If this helps... This error has occurred when Bluetooth was disabled on my laptop (I didn't realize it). If I enable BT, I don't get that error...

vidvisionify commented 5 years ago

Also running into this on Ubuntu Server 18.04, patch doesn't help.

lokeshh commented 5 years ago

Try sudo hciconfig hci0 down && sudo hciconfig hci0 up.

j616 commented 4 years ago

Also finding this issue. It seems to happen particularly often under docker. Worth noting that restarting the interface with hciconfig isn't practical for long running logging applications etc.

johngo7470 commented 4 years ago

Yes, I have this issue, too. Raspian 4.19, Pi3, onboard bt. While hci down/up can clear it, like j616 commented, I need it for a persistent logging application (monitors motion sensors). I also have quite a bit of other software running on this Pi, so I'm testing on a Pi4 with a base Raspian install.

johngo7470 commented 4 years ago

My Pi4 also had issues - the process hung after running for a little while. I ended up switching to a USB-based BT devices (BLED112) with bled112_scanner.py.

rakeshscetic commented 4 years ago

I have same issue and it run for while and give me Error:

Traceback (most recent call last): File "TempHumMonitor.py", line 46, in devices = scanner.scan(10.0)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 852, in scan self.start(passive=passive) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 797, in start self._mgmtCmd(self._cmd()+"end") File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 312, in _mgmtCmd raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp) bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 11, error: Rejected)

lukaszbos commented 4 years ago

Changing that line worked in my case

devices = scanner.scan(10.0)

to

devices = scanner.scan(10.0, passive=True)

bbostock commented 4 years ago

I'm getting the same problem on Raspberry Pi with Raspbian, I treed adding "passive=true", but it no longer finds the devices. Any other ideas?

dima72 commented 3 years ago

Try sudo hciconfig hci0 down && sudo hciconfig hci0 up.

worked for me on rpiZeroW to solve

bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 13, error: Invalid Parameters)

Moulde commented 3 years ago

Try sudo hciconfig hci0 down && sudo hciconfig hci0 up.

worked for me on rpiZeroW to solve

bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 13, error: Invalid Parameters)

This seems to have fixed my issue with rejected as well. bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 11, error: Rejected)

I detect the error in my code and run the above commands in my python script like this. resp = subprocess.call("hciconfig hci0 down && hciconfig hci0 up", shell=True)

PythonLinWencai commented 2 years ago

大神们,请给个详细方案

chrismyers81 commented 2 years ago

Try sudo hciconfig hci0 down && sudo hciconfig hci0 up.

This seems to have, at least temporarily, worked around the issue for me.

Trying to read an airthings waveplus via a raspberry pi 3b+ every 15 minutes, posting the results to emoncms, which will sometimes work for weeks before failing, and sometimes fail after a few hours/days.

mklsbali commented 1 year ago

For me

sudo apt update sudo apt upgrade

solved the issue. (Ubuntu 22.04.2 LTS)

WanderingApps commented 1 month ago

I had similar error on my Raspberry Pi Zero W: Failed to execute mgmt cmd 'scanend'. This did not occur if I manually ran my Python script at the command line; only when I ran via cron. The fix for me was to add a delay to the start of my script to prevent it from running "too early" in the Pi boot process. Either adding sleep command to start of the Python script or to cron seemed to do the trick and eliminate the scanend Rejected error: @reboot sleep 60 && my_script.py