蓝牙rfcomm权限被拒绝错误树莓派

10

我正在使用蓝牙适配器尝试将信息从Ubuntu 15.04发送到运行最新的Debian Jessie镜像的Raspberry Pi B+。我只是按照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, in <module>
    advertise_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身份运行吗? - Patton Pierce
2
我应该打自己的头。有时候解决方案就是这么简单! - Ankit Sharma
如果pip安装是用户本地的,则以root身份运行将无法正常工作。解决这个问题的最简单方法是sudo chmod o+rw /var/run/sdp。从这里开始,根据需要进行改进。没有任何麻烦。这是直接的解决方案。 - daparic
3个回答

19

以root用户身份运行脚本有一些问题,但是不是个好习惯

根据这篇帖子,你只需要将权限调整为/var/run/sdp文件(使用--compat开关时创建)。

因此,为了防止链接失效,我会复制dlech的帖子并对其进行Raspberry Pi适配:

  1. make sure your pi user is in the bluetooth group:

    $ cat /etc/group | grep bluetooth
    bluetooth:x:113:pi
    

    1.1. If it's not, add pi to bluetooth group:

    $ sudo usermod -G bluetooth -a pi
    
  2. Change group of the /var/run/sdp file:

    $ sudo chgrp bluetooth /var/run/sdp
    
  3. To make the change persistent after reboot:

    3.1. Create file /etc/systemd/system/var-run-sdp.path with the following content:

    [Unit]
    Descrption=Monitor /var/run/sdp
    
    [Install]
    WantedBy=bluetooth.service
    
    [Path]
    PathExists=/var/run/sdp
    Unit=var-run-sdp.service
    

    3.2. And another file, /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. Finally, start it all up:

    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最初的“发现”。


6
您需要修改服务文件并包含以下内容:ExecStartPost=/bin/chmod 662 /var/run/sdp。这对我有用。 - Jonno_FTW
我应该将这一行添加到[Service]的第三行吗? - Murtaza Haji

2
这个解决方案对我有用:
sudo chmod o+rw /var/run/sdp

感谢 @eigenfield

-3

sudo 则可以胜任此工作。

sudo python script.py

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接