lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Bluetooth C programming

Tue Jul 22, 2014 4:24 pm

I'm trying to do some Bluetooth programming with two Raspberrys but I'm not sure what packets/libraries or such things I need to install. I have usb bluetotooth dongles and ran this command: sudo apt-get install bluetooth bluez-utils blueman.

I thought I would be fine with this but when I try to compile simplescan.c from here http://people.csail.mit.edu/rudolph/Tea ... BTBook.pdf, I get fatal error: bluetooth/bluetooth.h: no such file or directory, compilation terminated.

I would be very thankful if someone could help me!

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

Re: Bluetooth C programming

Tue Jul 22, 2014 4:54 pm

I don't 'do' C, but I can google an error message with the best of 'em: http://stackoverflow.com/questions/2343 ... d-in-linux

They suggest

Code: Select all

sudo apt-get install libbluetooth-dev

User avatar
PeterO
Posts: 4294
Joined: Sun Jul 22, 2012 4:14 pm

Re: Bluetooth C programming

Tue Jul 22, 2014 4:59 pm

lihakimpale wrote: sudo apt-get install bluetooth bluez-utils blueman.

I thought I would be fine with this but when I try to compile simplescan.c from here http://people.csail.mit.edu/rudolph/Tea ... BTBook.pdf, I get fatal error: bluetooth/bluetooth.h: no such file or directory, compilation terminated.
There is probably a "dev" library to install as well. That will include the "c" header files. I can't check on a pi at the moment (writing new image to micro sd-card ;) ) , but on this MINT box it is
"libbluetooth-dev - Development files for using the BlueZ Linux Bluetooth library"

so I would expect "sudo apt-get install libbluetooth-dev" to work.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Wed Jul 23, 2014 8:58 am

Ok thank you very much.

I am compiling the code using arm-linux-gnueabihf cross compiler from my Ubuntu laptop with eclipse. I think I need the .a file for the linker but I can't find it anywhere.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Thu Jul 24, 2014 2:25 pm

I have done some research and found out that .so file is for same function and there should be somewhere libbluetooth.so.2 file but I can't find it. I have searched all the lib directories but can't find it. I'm assuming I should find those files from lib directory.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Thu Jul 24, 2014 2:31 pm

I think it should be in /usr/local/lib directory but it isn't.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Thu Jul 24, 2014 3:18 pm

I found the files. They were in /usr/lib/arm-linux-gnueabi directory. There is libbluetooth.a, libbluetooth.so, libbluetooth.so.3 and libbluetooth.so.3.12.0 files. Do I need to add all those files to my Eclipse linker or is that .a file just necessary?

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Thu Jul 24, 2014 6:32 pm

Ok I figured it out by myself. Just needed libbluetooth.a and libbluetooth.so files and also add -lbluetooth flag for the linker.

User avatar
PeterO
Posts: 4294
Joined: Sun Jul 22, 2012 4:14 pm

Re: Bluetooth C programming

Thu Jul 24, 2014 6:36 pm

lihakimpale wrote:Ok I figured it out by myself. Just needed libbluetooth.a and libbluetooth.so files and also add -lbluetooth flag for the linker.
Some background info for you....

.a versions are static libraries.
.so versions are dynamic link libraries.

If you link against a static library the required code from the library will end up in your executable.
If you link against a dynamic library the required code from the library will be added to your program by the linker/loader at run time.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Tue Jul 29, 2014 4:19 pm

Do you know what is the problem when I try to get connection between L2CAP_client.c and L2CAP_server.c it gives error: "Connection refused". I have hard-coded the bluetooth address so it should be right.

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

Re: Bluetooth C programming

Tue Jul 29, 2014 4:34 pm

Make sure that the client device is both paired and trusted

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Tue Jul 29, 2014 5:17 pm

Douglas6 wrote:Make sure that the client device is both paired and trusted
Of course it ain't working:
  • [email protected] ~ $ sudo bluez-simple-agent hci0 00:15:83:32:7A:3F
    RequestPinCode (/org/bluez/1880/hci0/dev_00_15_83_32_7A_3F)
    Enter PIN Code: 0000
    Creating device failed: org.bluez.Error.AuthenticationRejected: Authentication Rejected
  • [email protected] ~ $ sudo bluez-test-device trusted 00:15:83:32:7A:3F
    Traceback (most recent call last):
    File "/usr/bin/bluez-test-device", line 158, in <module>
    path = adapter.FindDevice(args[1])
    File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
    return self._proxy_method(*args, **keywords)
    File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
    File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
    dbus.exceptions.DBusException: org.bluez.Error.DoesNotExist: Does Not Exist

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

Re: Bluetooth C programming

Tue Jul 29, 2014 6:14 pm

Ok, you'll need to get it paired before you can trust it, and before you can connect to it.

What type of Bluetooth device is it? Does it perhaps have a different pin code than "0000"? The only way I can reproduce your "Authentication Rejected" error when pairing my phone is to enter an incorrect pin code ( or pressing 'cancel') when the phone asks me for it. If it's not a phone, then try pin code "1234", the 'other' default.
Last edited by Douglas6 on Tue Jul 29, 2014 6:20 pm, edited 1 time in total.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Tue Jul 29, 2014 6:20 pm

Douglas6 wrote:Ok, you'll need to get it paired before you can trust it, and before you can connect to it.

What type of Bluetooth device is it? Does it perhaps have a different pin code than "0000"? The only way I can reproduce your "Authentication Rejected" error when pairing my phone is to enter an incorrect pin code when the phone asks me for it. If it's not a phone, then try pin code "1234", the 'other' default.
These both are B-speech bluetooth USB adapters. I tried with PIN 1234 but it gives me the same error, I tried to pair with my phone and it paired without problems,

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

Re: Bluetooth C programming

Tue Jul 29, 2014 6:32 pm

lihakimpale wrote:These both are B-speech bluetooth USB adapters
Ok, what are they plugged into? I assume one is a Pi. Is the other also a Pi? If so, try running

Code: Select all

sudo bluez-simple-agent
on Pi 'A'. Then on Pi 'B' run your

Code: Select all

sudo bluez-simple-agent hci0 00:15:83:32:7A:3F
and enter a pin code.

Back on Pi 'A', it should be prompting you for a pin code. Enter the same one. enter 'yes' to the next prompt (if it responds with 'Cancel', try it again. Should work on the first or second try.)

Then use the command you used before for trusting, from Pi 'B'.

If the other device is not a Pi, but a PC, the procedure should be similar. You need agents running on both computers to handle pairing. In this case it will be easier to initiate pairing from the PC.
Last edited by Douglas6 on Tue Jul 29, 2014 6:45 pm, edited 1 time in total.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Tue Jul 29, 2014 6:42 pm

I also tried this which should solve the problem: "Patch the /usr/bin/bluez-simple-agent script. Replace "KeyboardDisplay" with "DisplayYesNo"."

It didn't solve for me.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: Bluetooth C programming

Tue Jul 29, 2014 6:54 pm

Douglas6 wrote:
lihakimpale wrote:These both are B-speech bluetooth USB adapters
Ok, what are they plugged into? I assume one is a Pi. Is the other also a Pi? If so, try running

Code: Select all

sudo bluez-simple-agent
on Pi 'A'. Then on Pi 'B' run your

Code: Select all

sudo bluez-simple-agent hci0 00:15:83:32:7A:3F
and enter a pin code.

Back on Pi 'A', it should be prompting you for a pin code. Enter the same one. enter 'yes' to the next prompt (if it responds with 'Cancel', try it again. Should work on the first or second try.)

Then use the command you used before for trusting, from Pi 'B'.

If the other device is not a Pi, but a PC, the procedure should be similar. You need agents running on both computers to handle pairing. In this case it will be easier to initiate pairing from the PC.
Yes I have two Pis and got it working now. Thank you very much for your help!

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

Re: Bluetooth C programming

Tue Jul 29, 2014 7:10 pm

lihakimpale wrote:Yes I have two Pis and got it working now. Thank you very much for your help!
Great, glad to hear it.
lihakimpale wrote:I also tried this which should solve the problem: "Patch the /usr/bin/bluez-simple-agent script. Replace "KeyboardDisplay" with "DisplayYesNo"."
Nope. As you've seen, you still need agents running, if only to reply 'Yes'. If using a serial bluetooth module, for example, it will have firmware running an agent. By the way, there's no need to patch bluez-simple-agent; it will take a command-line argument, e.g. '-c DisplayYesNo'.

Return to “C/C++”