2

I am in the middle of a project, where I want to use the integrated bluetooth device on a Raspberry Pi 3 running Jessie and an Android phone to communicate with each other. It is important to notice that the RPi will be in a location, where it is not possible to connect a screen or keyboard to it. So I wont be able to pair the phone and RPi beforehand.

I am writing a program in c++ which will under normal circumstances use a 3G USB dongle to communicate with a server. The phone can then extract the data from the server. If the 3G connection goes down, I want to use bluetooth as a failsafe. Therefore I need to be able to do the listening for bluetooth devices and take care of the bluetooth communication from within my c++ program. Is this possible? If yes, I would appreciate an example or a hint to where I can find any information regarding this.

I have found a lot of guides, where you need to pair the phone and RPi beforehand. But this is not what i want.

I don't care about security.

Thanks in advance :)

  • "I am writing a program in c++..." -> Presuming you are using a GNU/Linux OS such as Raspbian I believe the relevant library here is bluez. However, for the most part you'll probably be much better off researching that on Stack Overflow. – goldilocks Oct 3 '16 at 14:00
  • 1
    I found a solution using bluez as you mentioned - thx! – Mattie Oct 4 '16 at 9:17
2

Alright, I found a solution to my own problem. I post it here in case of somebody running into the same problem.

I am using bluez and installed libbluetooth-dev to get the necessary libraries for the C++ code.

I used the code for the rfcomm-server.c from the example https://people.csail.mit.edu/albert/bluez-intro/x502.html.

After copying the code, I made some small alterations in the code. I added the

str2ba( address, &loc_addr.rc_bdaddr);

instead of the line

loc_addr.rc_bdaddr = *BDADDR_ANY;

where address is the MAC address of the bluetooth device on the RPi in a char-array, like this:

char address[18] = "XX:XX:XX:XX:XX:XX";

After this I changed the file dbus-org.bluez.service found in the directory /etc/systemd/system/.

I changed the line:

ExecStart=/usr/lib/bluetooth/bluetoothd

to

ExecStart=/usr/lib/bluetooth/bluetoothd -C

and added this line under it:

ExecStartPost=/usr/bin/sdptool add sp

I saved the file and rebooted the RPi.

Now i could run the code and it would wait for a connection from the android app. I used the app "Bluetooth Terminal". When i connected the phone to the RPi the code outputs the MacAddress of the phone and waits for the app on the phone to write something. You can now send anything you want from the app and the RPi will then output it on the screen and terminate the program.

This means that you now can connect from any phone to the RPi.

This may be a bit confusing, but let me know if I need to clarify anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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