10

I was trying out bluetooth programming in python. It was working fine till yesterday. This morning, there was a power outage and for some reason, the bluetooth module got disabled and it could not be turned on. So, I did a sudo hciconfig hci0 reset and then turned it on. From that point onwards, the simplest of the programs are failing to execute. Take this one for example. It gets stuck at advertise_service in bluetooth module and throws the following error (FYI: virtualenv was not a problem here. The systemwide python also does the same thing).

Traceback (most recent call last):
  File "bt.py", line 17, in <module>
    advertise_service( server_sock, "SampleServer", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ])
  File "/home/machinename/.virtualenvs/py27/local/lib/python2.7/site-packages/bluetooth/bluez.py", line 242, in advertise_service
    raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')

Sometimes I got a different error when I compiled and reinstalled Bluez driver:

Traceback (most recent call last):
  File "build/bdist.linux-x86_64/egg/bluetooth/bluez.py", line 268, in advertise_service
  bluetooth.btcommon.BluetoothError: error no advertisable device.

But all of these worked like a charm before in that machine; in fact all of the program works just fine with my other ubuntu (14.04LTS) machine as I write this. I inspected the source code, and traced to a _bluetooth.so file - which is a compiled code, hence I couldn't figure out what to do anymore.

Any pointer will be highly appreciated.

Sidmeister
  • 856
  • 2
  • 14
  • 27
  • I traced root of the second error to [here](https://github.com/karulis/pybluez/blob/647198b994ce3458384b8680e68e368d85fbfd0e/bluez/btmodule.c#L2666) – Sidmeister Oct 13 '15 at 20:48
  • Do the standard `hci` command line tools still work? Like `hcitool scan`? – kaylum Oct 14 '15 at 21:53
  • Yes, the standard `hci` commands were working alright. Only the functions, which were being called from the [`_bluetooth`](https://github.com/karulis/pybluez/blob/master/bluetooth/bluez.py#L6) module, were throwing errors. If my code didn't refer to that module, everything was working fine. – Sidmeister Oct 15 '15 at 06:11

3 Answers3

23

This error is due to incompatibility issues with BlueZ 5 and SDP with bluetoothd

Fix for 15.10 and BlueZ 5

Make sure, running sdptool browse local gives following error:

Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory

As it turns out, the culprit is bluetoothd, the Bluetooth daemon. Using SDP with bluetoothd requires deprecated features for some silly reason, so to fix this, the daemon must be started in compatibility mode with bluetoothd -C (or bluetooth --compat).

Find location of bluetooth.service by:

systemctl status bluetooth.service

Then edit bluetooth.service and look for

ExecStart=/usr/libexec/bluetooth/bluetoothd

Append --compat at the end of this line, save, and then run

service bluetooth start

If all goes well, you should be able to successfully run

sudo sdptool browse local

Finally, reset the adapter:

sudo hciconfig -a hci0 reset

Things should work fine now

Old answer

Just to let people know, I believe the latest BlueZ build was somehow broken in my system. I downloaded, compiled and installed the 5.35 version, and nothing was working. I dialed down to 5.34, still same. I also noticed that the bluetooth adapter was going down automatically 3-4 minutes after enabling it using,

sudo hciconfig hci0 up # hci0 is the bt adapter

I used one usb bluetooth dongle to test. It did not go down automatically like the inbuilt adapter, but the problems persisted. Then I used apt-get to reinstall bluez,

apt-get install --reinstall bluez

and all of a sudden everything came back to normal.

Sidmeister
  • 856
  • 2
  • 14
  • 27
  • Ah, that explains it. pybluez has not been ported to bluez5. There have been major API changes between bluez4 and bluez5. I think you'll find that your apt-get installs bluez4. – kaylum Oct 15 '15 at 10:06
  • 1
    This link http://raspberrypi.stackexchange.com/questions/41776/failed-to-connect-to-sdp-server-on-ffffff000000-no-such-file-or-directory worked for me – Evans Murithi Dec 30 '16 at 07:31
1

To fix:

bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')

You need to:

  1. sudo nano /lib/systemd/system/bluetooth.service
  2. Change from: ExecStart=/usr/lib/bluetooth/bluetoothd
  3. To: ExecStart=/usr/lib/bluetooth/bluetoothd --compat
  4. sudo systemctl daemon-reload
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

Again, as sidmeister mentioned, Make sure, running sdptool browse local gives following error:

Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory

But,for those who are using initd system manager, its hard to find a solution if you want to execute sdp_rfcomm_server/client model and the terminal will keep on showing same error again and again. So for init.d follow these steps:

  1. Stop bluetooth first

    $ /etc/init.d/bluetooth stop

  2. Status check

    $ /etc/init.d/bluetooth status

  3. Run bluetooth in compatibility mode(don't forget ampersand,otherwise prompt won't turn up )

    $ /usr/libexec/bluetooth/bluetoothd --compat&

  4. start bluetooth again

    $ /etc/init.d/bluetooth start

  5. again try sdpbrowse

    $ sdptool browse local

Things should work for you now.

Luzan Baral
  • 3,678
  • 5
  • 37
  • 68