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 'le on' #190

Closed
raykamp opened this issue May 17, 2017 · 6 comments
Closed

Failed to execute mgmt cmd 'le on' #190

raykamp opened this issue May 17, 2017 · 6 comments

Comments

@raykamp
Copy link

raykamp commented May 17, 2017

Hey Ian, I'm getting the following error when running the "blescan" utility. It's a similar issue to #94, but I am using sudo.

$ sudo /usr/local/bin/blescan
Scanning for devices...
Traceback (most recent call last):
  File "/usr/local/bin/blescan", line 9, in <module>
    load_entry_point('bluepy==1.1.0', 'console_scripts', 'blescan')()
  File "/usr/local/lib/python2.7/dist-packages/bluepy/blescan.py", line 122, in main
    devices = scanner.scan(arg.timeout)
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 674, in scan
    self.start()
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 612, in start
    self._mgmtCmd("le on")
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 276, in _mgmtCmd
    "Failed to execute mgmt cmd '%s'" % (cmd))
bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on'

I'm running Ubuntu 16.04.2 on an x86-64 machine.
Is there something I'm doing wrong here?

I did try using this method to give "capabilities" to the bluepy-helper program. It didn't work.
https://unix.stackexchange.com/questions/96106/bluetooth-le-scan-as-non-root

@raykamp
Copy link
Author

raykamp commented May 17, 2017

If I run bluepy-helper directly and with sudo, I am able to execute 'le on' and 'scan'

$ sudo ./bluepy-helper 1
le on
rsp=$mgmt code=$success
scan
rsp=$mgmt code=$success
rsp=$stat state=$scan dst='(null) mtu=h0 sec='low
rsp=$scan addr=b43221686C114 type=h2 rssi=h3B flag=h0 d=b02010613FF4C000C0E08D97FFED6EEE0B8CB242ECAE0AB
rsp=$scan addr=b54E18ED171AF type=h2 rssi=h3B flag=h0 d=b02010613FF4C000C0E00965954BF988565E47E8EBAD0D3
rsp=$scan addr=bACBC329E198E type=h1 rssi=h3B flag=h0 d=b02010607FF4C0010020B00
rsp=$scan addr=bA47733FC1DED type=h1 rssi=h43 flag=h0 d=b0201060303A0FE1016A0FE03FA8FCA960EF6612020203EFF06096F43617374

Digging in a little deeper.. it looks like the error message I received was because bluepy-helper didn't have the interface number specified. If I added the interface argument in blescan and everything is groovy.

$ sudo /usr/local/bin/blescan -i=1
Scanning for devices...
    Device (new): ac:bc:32:9e:19:8e (public), -59 dBm 
	Flags: <06>
	Manufacturer: <4c0010020b00>
    Device (new): 67:69:76:85:59:87 (random), -95 dBm 
	Flags: <1a>
	Manufacturer: <4c0010020b00>
    Device (new): 78:4f:43:6f:e0:19 (public), -59 dBm 
	Flags: <06>
	Manufacturer: <4c0010020b00>

Is this expected behavior? If a user only has one interface, should bluepy detect that automatically, without explicitly passing the interface number?

$ hcitool dev
Devices:
	hci1	5C:F3:70:60:03:7E

@raykamp raykamp changed the title Permissions Error Failed to execute mgmt cmd 'le on' May 18, 2017
@raykamp raykamp closed this as completed May 18, 2017
@henridwyer
Copy link

henridwyer commented Jun 27, 2017

@raykamp I actually got this to work without needing sudo, using the link you provided on a Fedora server:

sudo setcap 'cap_net_raw,cap_net_admin+eip' PATH/TO/LIB/python3.5/site-packages/bluepy/bluepy-helper

Make sure it's the right version of the executable.

Now I can run blescan without sudo (and use it in my python code without running as a superuser)

@djnotus
Copy link

djnotus commented Nov 23, 2017

The issue is that this line in blte.py in the Scanner Class has the interface hard coded to 1. Run hciconfig and see what interfaces you have, and change the 0 to the number of the interface you are trying to use.

def init(self, iface=0):

@Cesarec88
Copy link

Hello, I've implemented the solution proposed,
still...

Traceback (most recent call last):
File "main.py", line 48, in
run()
File "main.py", line 27, in run
scan_dev = scan_mng.filter_device_by_addr(scan_mng.scan_devices(), mac_addressto_filter)
File "/home/pi/master/scan.py", line 48, in scan_devices
return self.__scanner.scan(scan_time)
File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 679, in scan
self.start(passive=passive)
File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 617, in start
self._mgmtCmd("le on")
File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 276, in _mgmtCmd
"Failed to execute mgmt cmd '%s'" % (cmd))
bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on'

what can I do?

@henry-stann
Copy link

henry-stann commented Jun 2, 2018

Hey Cesarec88,

Does Anything Scan?

Can you quickly confirm that you are finding bluetooth devices at all? One of the below should work:

pi@raspberrypi:~ $ sudo /usr/local/bin/blescan -i=0
pi@raspberrypi:~ $ sudo /usr/local/bin/blescan -i=1
pi@raspberrypi:~ $ sudo /usr/local/bin/blescan -i=2

If none of them do, you can try:

$cd ~
$cd /usr/local/lib/python3.5/dist-packages/bluepy
$ sudo ./bluepy-helper 1
le on
rsp=$mgmt code=$success
scan
rsp=$mgmt code=$success
(ctrl-C to escape, and try sudo ./bluepy-helper 0 and sudo ./bluepy-helper 2)

If none of those work, you can try:
pi@raspberrypi:~ $ sudo /usr/local/lib/python3.5/dist-packages/bluepy/bluepy-helper 0
le on
rsp=$mgmt code=$success
scan
rsp=$mgmt code=$success
(ctrl-C to escape, and try 0 and 2)

My number is actually different with the two different ways to scan. I'm just trying to scan in as many different ways as possible to make sure your linux can read anything.

Can I confirm, also, that you implemented djnotus' comment by using:
pi@raspberrypi:~ $ sudo nano /usr/local/lib/python3.5/dist-packages/bluepy/btle.py
Use Ctrl+_ (ctrl+shift+'minus-sign') to go to line 605:
class Scanner(BluepyHelper):
def init(self,iface=0):
And changed that 0 to a 1, if that is your trouble? It was not mine.

Are You Running in Sudo?

The file 'main.py' in your traceback tells me you are running a python script. Can I confirm that you are indeed running python from terminal using sudo? It is an often-overlooked problem in general. To make sure nothing in your script is throwing an error, can you go back to basics with me?

I use Ian Harvey's code, http://ianharvey.github.io/bluepy-doc/scanner.html . My test file is as follows:

pi@raspberrypi:~ $ cat Desktop/sample.py
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 run it by:
pi@raspberrypi:~ $ sudo python Desktop/sample.py

Nothing Seems to Work!

If you are still getting the error, could you modify btle.py?
pi@raspberrypi:~ $ sudo nano /usr/local/lib/python3.5/dist-packages/bluepy/btle.py
Change line 14 from:
Debugging = False
to Debugging = True

Then post your (now hopefully much more detailed) traceback to this forum, please.

Sorry if the incredibly detailed walk-thru seems like it is babying you. Often, I get tangled in linux by omitting very small details.

@fleutot
Copy link

fleutot commented Aug 10, 2021

@henridwyer:

@raykamp I actually got this to work without needing sudo, using the link you provided on a Fedora server:

sudo setcap 'cap_net_raw,cap_net_admin+eip' PATH/TO/LIB/python3.5/site-packages/bluepy/bluepy-helper

Make sure it's the right version of the executable.

Great! I want to keep bluepy in my projects local virtualenv. I added capabilities to that file anyway and it works.

But I wonder about delivering to others. I can't see any other way than asking users to run an install script that creates the venv, pip installs the requirements.txt, then run the command above and prompt for superuser password. Do you?

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

6 participants