rfcomm bluetooth权限被拒绝错误覆盆子pi

时间:2016-01-04 20:57:23

标签: bluetooth raspberry-pi

我正在使用蓝牙加密狗尝试将信息从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, 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

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:14)

以root用户身份运行脚本有效,但it's not a good practice

根据this thread,您只需要调整/var/run/sdp文件的权限(使用--compat开关时创建的文件)。

所以,为了防止链接腐烂,我正在复制dlech的帖子并将其改编为Raspberry Pi:

  1. 确保您的pi用户位于bluetooth群组中:

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

    1.1。如果不是,请将pi添加到bluetooth组:

    $ sudo usermod -G bluetooth -a pi
    
  2. 更改/var/run/sdp文件的组:

    $ sudo chgrp bluetooth /var/run/sdp
    
  3. 要在重新启动后使更改保持不变:

    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
    
  4. 归功于最初“弄明白”的用户dlech

答案 1 :(得分:-1)

sudo完成了这项工作。

sudo python script.py
相关问题