简体   繁体   English

如何使用BlueZ5库作为普通用户(没有sudo)执行PyBluez示例?

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

System: Ubuntu 15.10 with Bluez 5.38 系统:Ubuntu 15.10和Bluez 5.38

PyBluez with BlueZ 4 didn't have any problem with this. 具有BlueZ 4的PyBluez对此没有任何问题。 But since migrating to BlueZ 5 and making necessary adjustments to run the bluetooth code, I cannot run the same programs without sudo . 但是由于迁移到BlueZ 5并进行了必要的调整以运行蓝牙代码,因此如果没有sudo ,我将无法运行相同的程序。 Using sudo everything works as expected. 使用sudo一切都会按预期进行。 However, if I run without sudo, the following error is thrown: 但是,如果我在没有sudo的情况下运行,则会引发以下错误:

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 . 我知道,这是由于更新的蓝牙库中的安全性提高,这就是为什么它需要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 ? 但是,是否有任何特殊的组(例如访问摄像机的video )可以固有地访问蓝牙,这样我就可以使我的用户成为该组的成员,并且无需sudo就可以访问蓝牙? FYI, my user is already a member of dialout group and it doesn't help. 仅供参考,我的用户已经是dialout组的成员,并且没有帮助。

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. 您可以使用setcap设置python可执行文件的权限。

To install setcap on Ubuntu: 要在Ubuntu上安装setcap

sudo apt-get install libcap2-bin

To figure out where your python executable is: 要弄清楚您的python可执行文件在哪里:

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 在我的机器上, /usr/bin/python/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 相关问题: https : //unix.stackexchange.com/a/182559

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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