[英]How do I execute PyBluez samples using BlueZ5 library as normal user (without sudo)?
系统:Ubuntu 15.10和Bluez 5.38
具有BlueZ 4的PyBluez对此没有任何问题。 但是由于迁移到BlueZ 5并进行了必要的调整以运行蓝牙代码,因此如果没有sudo
,我将无法运行相同的程序。 使用sudo
一切都会按预期进行。 但是,如果我在没有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')
我知道,这是由于更新的蓝牙库中的安全性提高,这就是为什么它需要sudo
。 但是,是否有任何特殊的组(例如访问摄像机的video
)可以固有地访问蓝牙,这样我就可以使我的用户成为该组的成员,并且无需sudo
就可以访问蓝牙? 仅供参考,我的用户已经是dialout
组的成员,并且没有帮助。
请让我知道是否还有其他方法可以解决此问题。
您可以使用setcap
设置python可执行文件的权限。
要在Ubuntu上安装setcap
:
sudo apt-get install libcap2-bin
要弄清楚您的python可执行文件在哪里:
which python
这通常是一个链接。 您需要找出二进制文件的实际路径。 在我的机器上, /usr/bin/python
是/usr/bin/python2.7
的链接
为可执行文件分配适当的权限:
sudo setcap 'cap_net_raw,cap_net_admin+eip' <path-to-python-binary-here>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.