Community
cancel
Showing results for 
Search instead for 
Did you mean: 
idata
Community Manager
1,633 Views

BlueZ C Code

I'm trying to figure out how to handle pairing a device and setting up communication using BlueZ in a C application not a python script. I can't really find anything on this. BlueZ has examples of something, but literally no documentation on the API at all, or how it's support to work. I've tried to work backwards with the simple-agent script that allows automatic pairing with no pin code, but I can't figure it out and every forum I've come across that supposedly is about this topic points to code that doesn't even come close to how it's done... Does anybody have ANY idea on how to do this?

8 Replies
idata
Community Manager
44 Views

Hello jblackston,

 

 

I have never tried to interface with Bluetooth using C, however as you mention, it is indeed possible. For example, I was able to find several Bluetooth devices with the sample code of simplescan found in https://people.csail.mit.edu/albert/bluez-intro/c404.html.

 

I'd suggest you to read that document and the rest of the book, it should be of much help for what you're trying to achieve.

 

 

Peter.
idata
Community Manager
44 Views

I've looked at that code and read that book before I posted my question. The the book is incomplete, because it doesn't show you how to take an inquiry and turn it into the ability to authenticate with out requiring a PIN and trusting the device.

When you run their code or any code on the website and then try and pair the Edison to a PC it will immediately fail authentication... And I would assume if you got past that you still need to bypass the pin request. So no where is any of that found. None of the rest of the code in that book matters since you can not successfully pair with a device...........

When you look at the book and then look at the monster amount of sample code provided to you via the BlueZ Util file from their website it seems like you have to make a the same agent you do in python 'simple-agent.py', but in actually C code so that when an object sets up the connection to pair the correct agent functions fire off... Plus you need to setup the UUID for what services are supported like 'SPP' where as the example given in the intel edison example, doesn't show that the connected BT Edison supports the SPP service, because it was never setup...

The BlueZ examples seem to have the code, but it not documented at all even though BlueZ points you to the code base for documentation...

idata
Community Manager
44 Views

I understand, in that case, you could use as a workaround the method of pairing mentioned in https://communities.intel.com/message/292975# 292975 https://communities.intel.com/message/292975# 292975.

 

 

Let me know if that helps you, I'll be glad to help you if otherwise.

 

Peter.
idata
Community Manager
44 Views

Were you able to try the pairing mode mentioned above? Did it work?

 

 

Peter.
idata
Community Manager
44 Views

That pairing code worked from the get go two years ago, but that's not what my question is about. My question is how does one do that with the C library code for BlueZ and handle all BT stuff with the C library.

idata
Community Manager
44 Views

Why don't you try to pair the device with a shell script and then handle the rest of the actions in C? For example I paired my phone with this script:

 

 

# !/bin/sh

 

rfkill unblock bluetooth

 

sleep 1

 

hciconfig hci0 sspmode 1

 

hciconfig hci0 sspmode

 

hciconfig hci0 piscan

 

sdptool add SP

 

hcitool scan

 

# I found the phone's MAC manually and entered it

 

hcitool cc 50:XX:27:XX:6B:XX

 

rfcomm connect /dev/rfcomm0 50:XX:27:XX:6B:XX 4 &

 

hcitool auth 50:XX:27:XX:6B:XX

 

 

As you can see I found the MAC address of the phone and entered it manually, however you can modify the script to automatically find the Bluetooth devices and let you choose manually whichever you want.

 

After this I entered my phone's Bluetooth configuration and I simply tapped the Edison icon and it was successfully paired.

 

For example you can handle a command's output in a similar way to this: http://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output/. This approach might require some work but could prove easier.

 

Do you think that an approach like this might work for you? You can add a line in the end of the shell script that starts the C script that does the rest of the work.

 

 

-Peter.
idata
Community Manager
44 Views

So I'm assuming you don't know how to implement pairing and agent code via BlueZ C.

idata
Community Manager
44 Views

As I mentioned in my first post, I've never tried to interface with Bluetooth using C before. So, If you would like a more detailed answer I suggest you to contact BlueZ (http://www.bluez.org/contact/ http://www.bluez.org/contact/), so they can help you with this question about their API.

 

 

-Peter.