简体   繁体   中英

How do I execute PyBluez samples using BlueZ5 library as normal user (without sudo)?

System: Ubuntu 15.10 with Bluez 5.38

PyBluez with BlueZ 4 didn't have any problem with this. But since migrating to BlueZ 5 and making necessary adjustments to run the bluetooth code, I cannot run the same programs without sudo . Using sudo everything works as expected. However, if I run without sudo, the following error is thrown:

Traceback (most recent call last):
  File "/home/sidmeister/mymodule/py34/lib/python3.4/site-packages/PyBluez-0.22-py3.4-linux-x86_64.egg/bluetooth/bluez.py", line 240, in advertise_service
    protocols)
_bluetooth.error: (13, 'Permission denied')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/sidmeister/mymodule/mymodule/blue/receiver.py", line xxx, in some_function
    profiles=[bluetooth.SERIAL_PORT_PROFILE]
  File "/home/sidmeister/mymodule/py34/lib/python3.4/site-packages/PyBluez-0.22-py3.4-linux-x86_64.egg/bluetooth/bluez.py", line 242, in advertise_service
    raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (13, 'Permission denied')

I understand that this stems from increased security in newer bluetooth library which is why it needs sudo . But, is there any special group (like video in case of accessing camera) that has inherent access to bluetooth so that I can make my user a member of that group and it will be able to access bluetooth without sudo ? FYI, my user is already a member of dialout group and it doesn't help.

Please let me know if there is any other way I can tackle this problem.

You can use setcap to set permissions on the python executable.

To install setcap on Ubuntu:

sudo apt-get install libcap2-bin

To figure out where your python executable is:

which python

This will often be a link. You'll need to figure out the actual path of the binary. On my machine, /usr/bin/python is a link to /usr/bin/python2.7

To assign the appropriate permissions to the executable:

sudo setcap 'cap_net_raw,cap_net_admin+eip' <path-to-python-binary-here>

Related question: https://unix.stackexchange.com/a/182559

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM