0

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.

Sidmeister
  • 856
  • 2
  • 14
  • 27

1 Answers1

2

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

Community
  • 1
  • 1
  • I did this. Works in 14.04 but fails in 15.10. With `sudo` it works fine. I need this to work in 15.10 without `sudo`! – Sidmeister Apr 19 '16 at 19:04
  • From your question, it looks like you're using python 3.4, so I assume your binary would be something like `/usr/bin/python3.4` - is that correct? Did you get an error while running `setcap` or did it just not fix the issue of having to run your python code with `sudo`? – Scott Yoder Apr 19 '16 at 19:33
  • Yes. I set permission for python3.4 and it was smooth. However, it didn't solve the permission issue. It gave out when `bluetooth._bluetooth.sdp_advertise_service` was being called inside `bluez.py` library – Sidmeister Apr 19 '16 at 20:06