Page 1 of 1

Bluetooth C programming

Posted: Tue Jul 22, 2014 4:24 pm
by lihakimpale
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!

Re: Bluetooth C programming

Posted: Tue Jul 22, 2014 4:54 pm
by Douglas6
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

Re: Bluetooth C programming

Posted: Tue Jul 22, 2014 4:59 pm
by PeterO
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

Re: Bluetooth C programming

Posted: Wed Jul 23, 2014 8:58 am
by lihakimpale
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.

Re: Bluetooth C programming

Posted: Thu Jul 24, 2014 2:25 pm
by lihakimpale
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.

Re: Bluetooth C programming

Posted: Thu Jul 24, 2014 2:31 pm
by lihakimpale
I think it should be in /usr/local/lib directory but it isn't.

Re: Bluetooth C programming

Posted: Thu Jul 24, 2014 3:18 pm
by lihakimpale
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?

Re: Bluetooth C programming

Posted: Thu Jul 24, 2014 6:32 pm
by lihakimpale
Ok I figured it out by myself. Just needed libbluetooth.a and libbluetooth.so files and also add -lbluetooth flag for the linker.

Re: Bluetooth C programming

Posted: Thu Jul 24, 2014 6:36 pm
by PeterO
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

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 4:19 pm
by lihakimpale
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.

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 4:34 pm
by Douglas6
Make sure that the client device is both paired and trusted

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 5:17 pm
by lihakimpale
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

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 6:14 pm
by Douglas6
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.

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 6:20 pm
by lihakimpale
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,

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 6:32 pm
by Douglas6
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.

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 6:42 pm
by lihakimpale
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.

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 6:54 pm
by lihakimpale
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!

Re: Bluetooth C programming

Posted: Tue Jul 29, 2014 7:10 pm
by Douglas6
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'.