我正在使用蓝牙加密狗尝试从ubuntu 15.04发送信息到raspberry pi b +运行最新的debian jessie图像.我只是关注http://people.csail.mit.edu/albert/bluez-intro/教程.我得到了简单的RFCOMM和L2CAP协议.我在运行SDP协议时遇到问题.服务器代码是 -
from bluetooth import * server_sock = BluetoothSocket(RFCOMM) server_sock.bind(("", PORT_ANY)) server_sock.listen(1) advertise_service(server_sock, "SampleServer",service_classes=[SERIAL_PORT_CLASS], profiles=[SERIAL_PORT_PROFILE]) client_sock, client_info = server_sock.accept() print "connection from: ", client_info client_sock.send("PyBluez server says Hello!") data = client_sock.recv(1024) print "received: ", data client_sock.close() server_sock.close()
我得到的错误是 -
Traceback (most recent call last): File "rfcomm-server.py", line 7, inadvertise_service(server_sock, "SampleServer",service_classes=[SERIAL_PORT_CLASS], profiles=[SERIAL_PORT_PROFILE]) File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 176, in advertise_service raise BluetoothError (str (e)) bluetooth.btcommon.BluetoothError: (13, 'Permission denied')
以下是我采取的一些步骤 -
Add the user 'pi' to lp group run piscan on hciconfig hci0 Add --compat option to bluetoothd in bluetooth.service
任何帮助,将不胜感激.谢谢!
以root用户身份运行脚本是有效的,但这不是一个好习惯.
根据此线程,您只需要调整/var/run/sdp
文件的权限(使用--compat
交换机时创建).
因此,为了防止链接腐烂,我正在复制dlech的帖子并将其改编为Raspberry Pi:
确保您的pi
用户在bluetooth
群组中:
$ cat /etc/group | grep bluetooth bluetooth:x:113:pi
1.1.如果不是,请添加pi
到bluetooth
组:
$ sudo usermod -G bluetooth -a pi
更改/var/run/sdp
文件组:
$ sudo chgrp bluetooth /var/run/sdp
要在重新启动后使更改持久化:
3.1./etc/systemd/system/var-run-sdp.path
使用以下内容创建文件:
[Unit] Descrption=Monitor /var/run/sdp [Install] WantedBy=bluetooth.service [Path] PathExists=/var/run/sdp Unit=var-run-sdp.service
3.2.另一个文件,/etc/systemd/system/var-run-sdp.service
:
[Unit] Description=Set permission of /var/run/sdp [Install] RequiredBy=var-run-sdp.path [Service] Type=simple ExecStart=/bin/chgrp bluetooth /var/run/sdp
3.3.最后,开始吧:
sudo systemctl daemon-reload sudo systemctl enable var-run-sdp.path sudo systemctl enable var-run-sdp.service sudo systemctl start var-run-sdp.path
归功于最初"弄明白"的用户dlech.