AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Programming bluetooth using C++

Mon May 13, 2019 6:32 pm

http://people.csail.mit.edu/albert/blu ... 768482065
Edited - link did not show up
https://lifehacker.com/everything-you-n ... 1768482065

I am using the 1st link "scan" sample referenced above and it sort of works.
I ended up using the 2nd link to "activate" bluetooth on 3 BRPi

I am using ssh to communicate with RPi, have no access to GUI for now.

It is my understanding that "bluetoothctlt " is a "Linux package" designed to be used as standalone CLI application.
That is NOT my goal, I need a C++ code to interact with bluetooth without using "bluetoothctlt "

Unfortunately the code presented in the first reference link DOES NOT help me to initially activate the RPi bluetooth.
The ID "scan" works only after I do bluetoothctlt .
I can do that using "system calls" but prefer C++ .
Is that possible?

Thanks in advance


EDITED
I like to attempt to clarify the above post.
I need to have "headless operation" .
I am familiar with "bluez stack " of hci_x functions, what I am missing is how to initialize / start "bluetooth service" on RPi from running C++ program.
The enclosed C code will always fail because the RPI OS bluetooth service is not active , not untill CLI "bluetoothctlt" is used.
In essence - I either have to make "system" calls to bluetoothctl or find hci-_x function which does that.

Code: Select all

	// get the device ID
	if ((id = hci_get_route(NULL)) < 0) {
#ifdef DEBUG
		cout << " get the device ID  failed exit " << endl; //
#endif
		return -1;
	}



User avatar
topguy
Posts: 5624
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Programming bluetooth using C++

Tue May 14, 2019 12:17 pm

Why not make "starting the bluetooth service" a part of the system boot ?
Is there a "bluetooth" service you can start or enable with systemd ?

User avatar
Douglas6
Posts: 4713
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Programming bluetooth using C++

Tue May 14, 2019 1:33 pm

There's a ton of example C code in the BlueZ github (https://git.kernel.org/pub/scm/bluetoot ... .git/tree/) in the tools/ directory. I would start there. Most of it uses D-Bus interfaces to interact with BlueZ, so get familiar with that.

That Lifehacker article you referenced is quite inaccurate.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Programming bluetooth using C++

Tue May 14, 2019 3:09 pm

Thanks
that is what I was looking for - something which has comments in code.
I am still searching for how to start C++ to "interact" with RPi hardware.
I believe I need to learn how to distinguish between OS "build-in" bluetooth and using the bluez library.
I need to go back to RPi configure, I have not been there for months, to check if bluetooth needs to be configured there.
Thanks for your post, appreciate it.

deepo
Posts: 36
Joined: Sun Dec 30, 2018 8:36 pm

Re: Programming bluetooth using C++

Wed May 29, 2019 8:48 pm

I have also tried to get Bluetooth communication code working from C++ - with varied success.
My goal is to intercept Bluetooth Low Energy (BLE) Advertisement messages, similar to Bluetooth beacon signals.
I have managed that using the Bluez Bluetooth stack.

My goal was to receive temperature measurements from Minew BLE tags that transmit measurements as BLE Advertisement messages.

That seems to work fine for me.

My next step was then to communicate with the BLE tag to program it, but I have not been able to do that yet.

Many sources on the interwebs says to look at the Bluez DBus API, as that is supposedly the better way to use the Bluez stack, but I have not found a way through yet.

Let me know if you want some of my sample code.

/Mogens

deepo
Posts: 36
Joined: Sun Dec 30, 2018 8:36 pm

Re: Programming bluetooth using C++

Wed May 29, 2019 8:50 pm

Oh by the way.
I have never had to start the Bluetooth stack in any way, when working with Bluez on rPi 3B+.
My code (similar to your example) just connected and worked out of the box.

/Mogens

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Programming bluetooth using C++

Thu May 30, 2019 12:21 am

Thanks for the reply.
I am sure some will not appreciate this, but I am firm believer that "blueZ" is one of the worst NON documented stuff on the net.
It seems to be closed to an average programmer as far as finding out things.
Since it is now part of the Linux kernel even basic Linux documentation is very scarce.


I think there are several "levels" in accessing bluetooth technology...

Basic - low level HCI functions.
There is an ancient "scan" (C) wrapper which supposedly identify ALL nearby devices - its function "hci_inquiry" identify ONLY ONE such device.

Then you have "tools" - commands pretty much useless to seriously use in C++ code.

Yes, the "HCI" is pretty much obsolete , no big loss " and replaced with "D_Bus" .
I have not found SINGLE documentation in "blueZ" package ( source code ) about D_Bus usage.

I am not sure what BLE technology offers as fas as wireless communication.
If the name "low energy " really means something above "low energy" I'll pass for now.

I am presently looking into using "system()" to access "bluetoothctl" TOOL.
I can get simple response - such as system("bluetoothctl help"):

I am experimenting with "popen" to get interactive response - so far not much luck.
But there are other Linux "commands" I’ll try later.

I may be interested in you code later. Right now I need to prove that using "bluetoothctl" is worth the effort.
Later.






.

User avatar
Douglas6
Posts: 4713
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Programming bluetooth using C++

Thu May 30, 2019 1:04 am

AnneRanch wrote:
Thu May 30, 2019 12:21 am
I have not found SINGLE documentation in "blueZ" package ( source code ) about D_Bus usage.
Did you look into the BlueZ github directory as I recommended? Based on your response, I'm guessing no. Again, I can't help if you ignore my advice.

If you are looking at using bluetoothctl from a C program, try 'expect'. Or not, your choice.

deepo
Posts: 36
Joined: Sun Dec 30, 2018 8:36 pm

Re: Programming bluetooth using C++

Thu May 30, 2019 7:33 am

AnneRanch, I understand your frustration. I'm there myself.

My latest Bluetooth "project" was installing SBFspot this week on a rPi Zero W to transfer data via Bluetooth from my solar cells on the roof to pvoutput.org.
https://github.com/SBFspot/SBFspot

I haven't looked into the code yet, but it works flawlessly.

I wrote about my latest attempt with BlueZ here:
viewtopic.php?f=33&t=239640&p=1463422#p1463422

Douglas6: I'll have to look into the BlueZ github directory, thanks.

/Mogens

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Programming bluetooth using C++

Thu May 30, 2019 2:29 pm

Mogens
Yes, I thought that the concept "open source" includes "open documentation".
In an essence this has been "just learning experience" and so far with very limited success.

What is really interesting NONE of these "resources" have simple - "this is how to connect bluetooth devices IN SOFTWARE ".
Perhaps these "source packages" could use some kind of "table of contents". Wishful thinking on my part.


I am really not thrilled to switch to yet another "concept" - I hate to call it "protocol" - D_Bus.
If "hci" published by same group did not do the job, what makes anybody think that "D_Bus" will be the holly grail?

( just read a note saying "bluez should not be used in C++ programming"!)

The whole idea of using "'bluetothctl" was - if it works in plain "command" mode - why not put it in C++.
The concept of starting another process is pretty clear, no magic, but to make "exec" work in interactive mode seems impossible.

PS
As far as RPI Zero goes - after finding out that its "processor" architecture is NOT currently part of tools I am using I had to scrap it for my project.
Long , boring story.

Mogens
Just an unhappy update.
This DBus seems to be "under development" and as far as my OS is concerned "version 1.0".
Already found "missing header " and checked "developers " site - 87 pending errors!
Funny - I cannot just test delete the reference to missing header - file is "read only".

I'll try to find current , hopefully working release not under OS "supervision" , and build it from source .

I am really not to thrilled the way Linux "supports" software development.
Everything is either obsolete or "under construction".
Not really sure what is the answer.
Of course I will get flamed for saying this.

deepo
Posts: 36
Joined: Sun Dec 30, 2018 8:36 pm

Re: Programming bluetooth using C++

Fri May 31, 2019 8:25 am

AnneRanch: You should be able to fix the missing file problem by editing the file that includes the missing file as robot:
sudo nano filename.ext

But your build might still be broken after that.

I think I updated BlueZ to 5.5 but I don’t have my notes here. I’ll respond when I get back home monday.
Are you trying out BlueZ?

/Mogens

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Programming bluetooth using C++

Fri May 31, 2019 12:28 pm

I have been using bluez 5.50 source compiled for Raspberry Pi 3 B . I did try to crosscompile it ( on X86) but kept running into issues.
I ended up building it on RPi and just copy the library to X86.
It looks as most of the "DBus" stuff" is unusable.
I am not sure I want to find out why or just start with fresh DBus source only.
But I need to find one with "configure" , I am not that comfortable to tackle autotools from scratch.

deepo
Posts: 36
Joined: Sun Dec 30, 2018 8:36 pm

Re: Programming bluetooth using C++

Thu Jun 13, 2019 7:27 pm

I stumbled upon TinyB from Intel:
TinyB exposes the BLE GATT API for C++, Java and other languages, using BlueZ over DBus.
https://github.com/intel-iot-devkit/tinyb

I was able to discover Bluetooth devices, listing their services etc.

But it does not seem to allow me to receive BLE advertisements, so I'm still on the hunt for something that can do that over DBus.

/Mogens

Return to “C/C++”