Linux Bluetooth programming in C & socket.h

mafalda picture mafalda · Jun 9, 2012 · Viewed 10.1k times · Source

I'm a new stackoverflow user! I'm writing because I have some problems programming Bluetooth with C on my KUbuntu 12.04.

I'm trying to connect a device (LEGO Mindstorm Brick) to my notebook through a program that I've found in this pdf (Initiate connection): NXT_Bluetooth_Handout

I installed the following packages: - bluez-hcidump, communication debugging tool - bluez, Linux Bluetooth stack and associated tools - libBluetooth3, the BlueZ library - libBluetooth-dev, the development files for linking to the BlueZ library

// Socket, used for Bluetooth socket
#include <sys/socket.h>
#include <sys/types.h>
// Bluetooth headers
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

Mainly I encountered two problems: 1) If I simply try to compile the program I get:

$ gcc -lm -lbluetooth nxt_bt_connect.c -o nxt_bt_connect
/tmp/ccSLdkpn.o: In function `init_bluetooth':
nxt_bt_connect.c:(.text+0x60): undefined reference to `str2ba'
collect2: ld returned 1 exit status

Where str2ba is a BlueZ function, which should work...ba2str is the other function which this library provides and it works without problem.

2) If I modify the code in order to use ba2str instead of str2ba function I get an error related to the socket:

$ gcc -lm -lbluetooth 1.c -o nxt_bt_connect2
1.c: In function ‘main’:
1.c:101:23: error: called object ‘socket’ is not a function

The problem is that I have no socket header is sys/, in fact if I run:

find /usr/include/ -name socket.h
 /usr/include/gtkmm-2.4/gtkmm/socket.h
 /usr/include/linux/socket.h
 /usr/include/asm-generic/socket.h
 /usr/include/giomm-2.4/giomm/socket.h
 /usr/include/x86_64-linux-gnu/sys/socket.h
 /usr/include/x86_64-linux-gnu/asm/socket.h
 /usr/include/x86_64-linux-gnu/bits/socket.h

Is there someone who knows how to solve those problems? I hope to have described all situation in a sufficiently clear way...I apologize for my bad English!

Thank you very much!!

Answer

user529758 picture user529758 · Jun 9, 2012

For the second question: you have to post the code you've written. Else we can't figure out what's bad.

For the first question: you need to put the library linker flags as the last parameters when invoking GCC:

gcc nxt_bt_connect.c -o nxt_bt_connect -lm -lbluetooth