Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to execute mgmt cmd 'scanend' #150

Open
Owlie opened this issue Aug 28, 2016 · 20 comments
Open

Failed to execute mgmt cmd 'scanend' #150

Owlie opened this issue Aug 28, 2016 · 20 comments

Comments

@Owlie
Copy link

Owlie commented Aug 28, 2016

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
Copy link

ulfwin commented Oct 9, 2016

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

@markrages
Copy link
Contributor

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
Copy link

brianpeiris commented Jan 15, 2017

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
Copy link

@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
Copy link

okelet commented Sep 13, 2017

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

@okelet
Copy link

okelet commented Sep 13, 2017

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
Copy link

vidvisionify commented Jul 31, 2018

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

@lokeshh
Copy link

lokeshh commented Jan 17, 2019

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

@j616
Copy link

j616 commented Aug 26, 2019

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
Copy link

johngo7470 commented Nov 12, 2019

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
Copy link

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
Copy link

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
Copy link

lukaszbos commented Dec 7, 2019

Changing that line worked in my case

devices = scanner.scan(10.0)

to

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

@bbostock
Copy link

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
Copy link

dima72 commented Sep 11, 2020

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
Copy link

Moulde commented Jan 19, 2021

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
Copy link

大神们,请给个详细方案

@chrismyers81
Copy link

chrismyers81 commented Jun 5, 2022

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
Copy link

mklsbali commented May 10, 2023

For me

sudo apt update
sudo apt upgrade

solved the issue.
(Ubuntu 22.04.2 LTS)

plenty-brent added a commit to PlentyAg/bluepy that referenced this issue May 25, 2023
@WanderingApps
Copy link

WanderingApps commented May 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests