Skip to content
Advertisement

How to connect BLE devices using Linux bluetooth C library

  1. Description of the problem

I am trying to connect my Bluetooth devices with BLE to a Linux system using the Bluetooth C libraries (I am programming using C++), so here is the code I am currently using:

JavaScript

NOTE: You need to set a specific MAC in connect function parameter. I am also compiling with g++ using the following command:

JavaScript

Here are more information about my hci0 device using hciconfig -a command:

JavaScript

I am also using Ubuntu 18.04 with:

bluetoothctl v5.64

dbus v1.12.2-1ubuntu1

bluez v5.48-0ubuntu3.

And the result I am getting when I run the application is the following:

JavaScript
  1. Solution attempts

I first tried to document myself about how the bluetooth C libraries for Linux works (itis difficult to find any documentation and less for BLE), I need to connect using BLE since my device does not allow communication using Bluetooth classic and I cannot find the reason for the problem I am having. I also have tried to restart both the bluetooth service (using sudo service bluetooth restart) and the hci0 device (using sudo hciconfig hci0 reset) several times but none worked, I also tried restarting the PC and neither, I even tried the steps mentioned here that are quite similar to my case since previously the “Connection timeout” error has also happened to me but it didn’t work either, it just returned the following code in case it was useful: Executing this command: hcitool cmd 0x08 0x000EI received the following output:

JavaScript

What can I do about this problem?

EDIT: I was researching and found that making dbus calls to the bluetooth service using C/C++ could work for me but it is quite difficult to find good documentation or concrete connection examples despite having reviewed lots of github projects as most of the code is too convoluted, included in many files and/or consecutive calls for what I am trying to find/understand

Advertisement

Answer

This is what the old hci_xxx bluetooth C functions are doing at the lowest level. They probably don’t work now because bluez/dbus is getting in the way. The following code works on a Raspberry Pi because it disables bluez first, and could be the basis of a C program – but it would be much easier to use one of the github libraries mentioned in the comments.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement