10

I'm using a bluetooth dongle to try and send information from ubuntu 15.04 to raspberry pi b+ running the latest debian jessie image. I'm just following the http://people.csail.mit.edu/albert/bluez-intro/ tutorial. I got the simple RFCOMM and L2CAP protocols working. I'm having trouble running the SDP protocol. The server code is -

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()

The error I'm getting is -

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')

Here are some steps I have taken-

Add the user 'pi' to lp group
run piscan on hciconfig hci0
Add --compat option to bluetoothd in bluetooth.service

Any help would be appreciated. Thanks!

3
  • Have you tried running as root? Jan 4, 2016 at 21:00
  • 2
    I should hit myself on the head. Sometimes the solution is that simple! Jan 4, 2016 at 21:08
  • Running as root will not work if the pip install is local to user. The simplest way to solve this is sudo chmod o+rw /var/run/sdp. And from here, improve it accordingly. No huzz fuzz. This is the direct solution.
    – daparic
    Oct 14, 2021 at 13:51

3 Answers 3

19

Running your script as root kinda works, but it's not a good practice.

According to this thread, you just need to adjust permissions to the /var/run/sdp file (which is created when using the --compat switch).

So, to prevent link rot I'm reproducing dlech's post and adapting it to 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
    

Credit goes to user dlech who "figured it out" originally.

2
  • 6
    You need to modify the service file to include: ExecStartPost=/bin/chmod 662 /var/run/sdp. This worked for me
    – Jonno_FTW
    Jul 21, 2017 at 2:09
  • Do i add this as 3rd line to the [Service]? Mar 4, 2021 at 13:44
2

This solution worked for me:

sudo chmod o+rw /var/run/sdp

Thanks @eigenfield

-3

And sudo does the job.

sudo python script.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.