IanHarvey / bluepy

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

bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on' #218

Open edouardkleinhans opened 6 years ago

edouardkleinhans commented 6 years ago

Hello,

I'm trying to run you sample code (http://ianharvey.github.io/bluepy-doc/scanner.html#sample-code) on my orangepi with a bluetoot v4 dongle, but it doesn"t work.

root@orangepipcplus:~# lsusb Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 007 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

root@orangepipcplus:~# hcitool dev Devices: hci0 00:1A:7D:DA:71:13

root@orangepipcplus:~# python test.py Traceback (most recent call last): File "test.py", line 14, in devices = scanner.scan(10.0) File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 679, in scan self.start(passive=passive) File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 617, 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'

Any idea of what's wrong ?

PrzemoF commented 6 years ago

Did you try with sudo?

edouardkleinhans commented 6 years ago

all the command are executed as root.

goldfingyr commented 6 years ago

I think I have the same problem just with the TI Sensortag (running as root - debugging on). Running on a Raspberry Pi with a BLE USB; hcitool is able to scan:

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

Got: '' Got: '' Got: '' Got: '' Got: '' Got: '' Sent: le on

Traceback (most recent call last): File "sensortagcollector.py", line 317, in devices = scanner.scan(timeout=30.0) 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 271, in _mgmtCmd self._writeCmd(cmd + '\n') File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 267, in _writeCmd self._helper.stdin.write(cmd) IOError: [Errno 32] Broken pipe

rmesnard commented 6 years ago

Same issue for me. On a Orange PI Zero , i've try 2 different USB adapter ( CSR 4.0 and Orico ) Both working perfectly with hcitools.

Scanning for devices... Traceback (most recent call last): File "/usr/local/bin/blescan", line 9, in <module> load_entry_point('bluepy==1.1.2', '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/bluepy/btle.py", line 679, in scan self.start(passive=passive) File "/usr/local/lib/python2.7/dist-packages/bluepy/bluepy/btle.py", line 617, in start self._mgmtCmd("le on") File "/usr/local/lib/python2.7/dist-packages/bluepy/bluepy/btle.py", line 276, in _mgmtCmd "Failed to execute mgmt cmd '%s'" % (cmd)) bluepy.bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on'

I try to rebuild helper with the same results.

Ishma59 commented 6 years ago

same problem on my kali linux 2017 I try running : $sudo bleah t0 BTLE Exception: Failed to execute mgmt cmd 'le on'

Ishma59 commented 6 years ago

any help thanks in advance

songyawen commented 6 years ago

I have the same question. (2017kali\bleah ) Thanks for any help!!!!!!

SHA89 commented 6 years ago

What output does bluepy-helper give when you run it directly? /usr/local/lib/python2.7/dist-packages/bluepy/bluepy-helper 0 help le on

rosshadden commented 6 years ago

I had the same error, but sudo makes it work for me. Do we know which group or permissions it requires? I would like to make it run with my user.

SHA89 commented 6 years ago

sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper This gives everyone permission to execute bluepy-helper.

hasamba commented 6 years ago

i'm getting: MQTT connection returned result: Connection Accepted. Traceback (most recent call last): File "miscalegw.py", line 99, in <module> main() File "miscalegw.py", line 96, in main devices = scanner.scan(5) File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 679, in scan self.start(passive=passive) File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 617, in start self._mgmtCmd("le on") File "/usr/local/lib/python3.5/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 tried all the answer i could find, tried running as root, with sudo, i checked the interface and it's 0 def init(self, iface=0):

anyone can help?

SHA89 commented 6 years ago

have you tried running bluepy-helper directly and tried to execute "le on" manually?

hasamba commented 6 years ago

im getting: "bluepy-helper: command not found" eventough i can see the file and it is executeable

SHA89 commented 6 years ago

I guess you are not so familiar with linux command line. to run the executable, you must execute it the following way: ./bluepy-helper 0 If you forget the "./", the system will assume you try to start a program that is available in your PATH. With the "./" you give the direct address of your executable. The "0" defines the interface you want to use. If you only have one bluetooth interface, 0 will be correct, if you have multiple, you can use 1, ...

If the program complains about permissions, try it again with super user rights: sudo ./bluepy-helper 0

hasamba commented 6 years ago

thanks for your explanation, i learned something new :) im running now /usr/local/lib/python3.5/dist-packages/bluepy $ ./bluepy-helper 0 help le on and im not getting any response, just the curser blinking, is that ok?

same with sudo

SHA89 commented 6 years ago

if you type 'help' you will see all possibilities. As i can see in your error message, your problem already occurs when trying to execute the command le on (to put it in bluetooth low energy mode) so i suggest you try that command.

hasamba commented 6 years ago

for some reason "help" also doesnt give me any result...just blinking

SHA89 commented 6 years ago

i'm using it in python 2.7 but i'll set up a 3.5 to test

SHA89 commented 6 years ago

(venv) ~/python_sandbox/test_bluepy3/venv/lib/python3.5/site-packages/bluepy $sudo ./bluepy-helper 0 help

help Show this help

stat Show current status

quit Exit interactive mode

conn [address [address type [interface]]] Connect to a remote device

disc Disconnect from a remote device

svcs [UUID] Primary Service Discovery

incl [start hnd [end hnd]] Find Included Services

char [start hnd [end hnd [UUID]]] Characteristics Discovery

desc [start hnd] [end hnd] Characteristics Descriptor Discovery

rd Characteristics Value/Descriptor Read by handle

rdu [start hnd] [end hnd] Characteristics Value/Descriptor Read by UUID

wrr Characteristic Value Write (Write Request)

wr Characteristic Value Write (No response)

secu [low | medium | high] Set security level. Default: low

mtu Exchange MTU for GATT/ATT

le [on | off] Control LE feature on the controller

pairable [on | off] Control PAIRABLE feature on the controller

pair Start pairing with the device

unpair Start unpairing with the device

scan Start scan

scanend Force scan end

pasv Start passive scan

pasvend Force passive scan end

rsp=$stat state=$disc mtu=h0 sec='low

It seems to work on my computer. I also have a blinking cursor, but if i type help, i do get this help message. And have you tried other commands? Do you see "help" appearing on your blinking cursor as you type? don't forget to press enter.

SHA89 commented 6 years ago

Just to be sure, you have to start the program with sudo ./bluepy-helper 0 and after it's started, you need to type 'help' or 'le on'

flatsiedatsie commented 6 years ago

Same error, but when doing multiple calls in quick succession. Python 3.4 on a Pi Zero W with Dietpi Stretch.

2018-02-20 17:23:54.673 Error: (Mi Flower Mate) ----> Line 55 in /home/pi/domoticz/plugins/mi_flower_mate/miflora/backends/bluepy.py, function connect 2018-02-20 17:23:54.673 Error: (Mi Flower Mate) ----> Line 353 in /usr/local/lib/python3.4/dist-packages/bluepy/btle.py, function init 2018-02-20 17:23:54.674 Error: (Mi Flower Mate) ----> Line 402 in /usr/local/lib/python3.4/dist-packages/bluepy/btle.py, function _connect

(working on a plugin that uses this library: https://github.com/flatsiedatsie/Mi_Flower_mate_plugin)

shumingli708 commented 6 years ago

I typed 'le on' on bluepy-helper, and I got 'rsp=$mgmt code=$protoerr'.

hasamba commented 6 years ago

now im able to run and getting same message: rsp=$mgmt code=$protoerr

SHA89 commented 6 years ago

i also get the protoerr if i'm not running as superuser (sudo ./bluepy-helper 0).

  1. So first you need to check if you have the problem as sudo-user sudo ./bluepy-helper 0 le on
  2. If it works with sudo, and you want to be able to run it as a normal user, you need to set capabilities on the executable. try: sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper (make sure u are in the directory with the bluepy-helper executable) I don't know exactly what this does, but it allows me to run bluepy-helper without root privileges (sudo)
  3. Now you can run it as a normal user with ./bluepy-helper 0

If you have problems, please post the commands you used and the output. FIY i'm running it on ubuntu 16.04 and tested it with python 3.5

hasamba commented 6 years ago

i ran it before with sudo.

sudo ./bluepy-helper 0 le on rsp=$mgmt code=$protoerr

im on mint (ubuntu 16.04), python 3.5.2

SHA89 commented 6 years ago

In that case i don't have an explanation, my solution was the capabilities.. Maybe have a try with gatttool to see if you can set up a connection, and hciconfig to check if your bluetooth interfaces are up and running

hasamba commented 6 years ago

thanks again for all your help

flatsiedatsie commented 6 years ago

Here the issue still persists if there are multiple calls in a row:

https://www.domoticz.com/forum/viewtopic.php?f=65&t=22281

on Domoticz plugins are run an root, so that's not an issue.

jamestouri commented 6 years ago

Hi, was anyone able to solve this problem? I have been stuck on it for a while and I still get the same error bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on'

mohithkalyan commented 6 years ago

Failed to execute mgmt cmd 'le on'.

PrzemoF commented 6 years ago

I'm still testing, but this works [EDITED]:

  1. Start from one terminal: sudo /usr/local/lib/python3.6/site-packages/bluepy/bluepy-helper 0 or wherever you have bluepy-helper
  2. Type "le on" and you should get this: rsp=$mgmt code=$success
  3. Do whatever you need to do from another terminal.
warpdriv commented 5 years ago

Ran @PrzemoF 's post and got rsp=$mgmtcode=$mgmterrestat=h11emsg='Invalid Index

on 16.04, py 3.7

Help us Obiwon Kanobi! You're our only hope!

roshradha commented 5 years ago

I'm still testing, but this works [EDITED]:

  1. Start from one terminal: sudo /usr/local/lib/python3.6/site-packages/bluepy/bluepy-helper 0 or wherever you have bluepy-helper
  2. Type "le on" and you should get this: rsp=$mgmt code=$success
  3. Do whatever you need to do from another terminal.

I tried the same and I got: rsp=$mgmtcode=$mgmterrestat=hBemsg='Rejected

What should I do next?

Tasm-Devil commented 5 years ago

Hi. The Error still exists. With sudo it works:

me@mypc:~$ sudo ./.local/lib/python3.6/site-packages/bluepy/bluepy-helper 0

\# bluepy-helper.c version 1.3.0 built at 20:40:35 on May  3 2019
le on
rsp=$mgmtcode=$success

But without sudo it wont:

me@mypc:~$ sudo setcap 'cap_net_raw,cap_net_admin+eip' ./.local/lib/python3.6/site-packages/bluepy/bluepy-helper

me@mypc:~$ ./.local/lib/python3.6/site-packages/bluepy/bluepy-helper 0

\# bluepy-helper.c version 1.3.0 built at 20:40:35 on May  3 2019
le on
rsp=$mgmtcode=$mgmterrestat=h14emsg='Permission Denied

Are there any plans to fix it. I want to use bt-mqtt-gateway to get a connection to an nrf52840 running circuitpython.

doronator commented 5 years ago

Note that when running python scripts with sudo, your normal user and the super-user ivoked by sudo, may have a different default python location.

When using sudo with running a python script you may need to specify the full system path to the version of python you inteend to run

dima72 commented 3 years ago

I'm running on Rasbian PC. sudo ./bluepy-helper 0

and getting

le on
rsp=$mgmtcode=$mgmterrestat=h11emsg='Invalid Index

please suggest what might be wrong?

fphammerle commented 3 years ago

Re: https://github.com/IanHarvey/bluepy/issues/218#issuecomment-350680433

sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper This gives everyone permission to execute bluepy-helper.

cap_net_admin,cap_net_raw+ep without i ("inheritable") is sufficient:

$ sudo setcap cap_net_raw,cap_net_admin+ep bluepy-helper
$ getcap bluepy-helper
bluepy-helper = cap_net_admin,cap_net_raw+ep
$ bluepy-helper
# bluepy-helper.c version 1.3.0 built at 06:27:58 on Jul 16 2019
le on
rsp=$mgmtcode=$success
$ uname -r
5.10.17-v7l+
EricRongkai commented 3 years ago

I'm running on Rasbian PC. sudo ./bluepy-helper 0

and getting

le on
rsp=$mgmtcode=$mgmterrestat=h11emsg='Invalid Index

please suggest what might be wrong?

Firstly, make sure that your BLE device is existing, using following command

$ hciconfig

If the device is existing, it will like this

hci0: Type: Primary Bus: USB BD Address: 00:1A:7D:DA:71:11 ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING RX bytes:883983 acl:0 sco:0 events:23598 errors:0 TX bytes:6053 acl:0 sco:0 commands:160 errors:0

Your error is because that the BLE device is not activated. You can use this command to activate it.

$ sudo hciconfig [hci0] up

santobedi commented 2 years ago

Hello @EricRongkai,

$ hciconfig gave me

Command 'hciconfig' not found, but can be installed with:

sudo snap install bluez  # version 5.48-3, or
sudo apt  install bluez  # version 5.53-0ubuntu3.3

I installedsudo apt install bluez # version 5.53-0ubuntu3.3 and ran $ sudo hciconfig [hci0] up, however, I get Can't get device info: No such device. Same thing happened with sudo snap install bluez # version 5.48-3.

Now, when I run hciconfig, nothing happens. I still get rsp=$mgmtcode=$mgmterrestat=h11emsg='Invalid Index after le on command.