Search code examples
c++linuxbluetoothsdp

C++ linux bluetooth sdp_connect only succeed with root privilege


I'm trying to run a bluetooth server program on Linux
In order for my client device to find the server, I need to register my service profile on sdp
My code is basically similar to code on this page

The problem is that I need to get root privilege in order to make sdp_connect succeed
When running as a normal user, sdp_connect always returns NULL

Because the other part of my code requires a normal user (not root) environment, I'm wondering if I can make sdp_connect and the following sdp_record_register succeed without root access

Update

Just found a possible solution here
I'm using Bluez5 and sdp_connect seems to be deprecated Bluez4 function, so I need to run the service in compatibility mode
Now it works, and it gives access of /var/run/sdp to all
Just not sure if it is safe to give such permanent access


Solution

  • As you rightly note, there have been 8 tools marked as deprecated as they are no longer maintained or have been replaced by other tools.

    Running with --compat would seem like a quick fix rather than a long term solution.

    The supported APIs are Bluetooth Management API which is focused towards system level functions.

    The application level is accessed through D-Bus and is spread over several documents in https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc

    Adapter, Device, GATT, and profile are some of the common ones depending what you are trying to do.

    Looking at the bluetoothctl source code can give some examples of how to use these APIs with C.

    There are more examples in the test directory but they are in Python:

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test