title: Linux Bluetooth programming in C & socket.h
body: <p>I'm a new stackoverflow user! I'm writing because I have some problems programming Bluetooth with C on my KUbuntu 12.04.</p>
  
  <p>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):
  <a href="http://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=6&amp;sqi=2&amp;ved=0CIYBEBYwBQ&amp;url=http://www.cs.uleth.ca/~benkoczi/3720/data/NXT_Bluetooth_handout-jeremy.pdf&amp;ei=STzTT9njJI2SswaVurWdDw&amp;usg=AFQjCNHIUOUffS_5qdMpONbLRoRrbUzOmQ&amp;sig2=jLAX57opI2qOdDKNJJ01hA" rel="nofollow">NXT_Bluetooth_Handout</a></p>
  
  <p>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 </p>
  
  <pre><code>// Socket, used for Bluetooth socket
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  // Bluetooth headers
  #include &lt;bluetooth/bluetooth.h&gt;
  #include &lt;bluetooth/rfcomm.h&gt;
  </code></pre>
  
  <p>Mainly I encountered two problems:
  1) If I simply try to compile the program I get:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>Where str2ba is a BlueZ function, which should work...ba2str is the other function which this library provides and it works without problem.</p>
  
  <p>2) If I modify the code in order to use ba2str instead of str2ba function I get an error related to the socket:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>The problem is that I have no socket header is sys/, in fact if I run:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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!</p>
  
  <p>Thank you very much!!</p>