title: How to implement Bluetooth LE with Bluez in Linux
body: <p>I'm working on setting up two Linux systems for a BLE demo. Obviously one system will be the peripheral and one will be the central devices. I have several questions surrounding both of these configurations. </p>
  
  <p><strong>Environment</strong></p>
  
  <ul>
  <li>2x Ubuntu 14.04 systems</li>
  <li>2x Pluggable USB-BT4LE dongles (<a href="http://plugable.com/products/usb-bt4le" rel="nofollow noreferrer">http://plugable.com/products/usb-bt4le</a>)</li>
  </ul>
  
  <p><strong>Peripheral Device Setup</strong></p>
  
  <p>The first order of business is getting the peripheral system setup and advertising with a GATT server configured. At this time, it does not seem possible to configure a GATT server from the command line. So, while it is a simple task bringing a USB dongle up and advertising it, this does not allow the creation of custom services and characteristics. The only example of a GATT server I could find was the gatt-example.c file in the Bluez package. So I downloaded and built the latest bluez-5.23 source. (<a href="http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html" rel="nofollow noreferrer">http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html</a>). Additionally configured using the --enable-maintainer-mode flag to force build the gatt-example.c plugin into bluetoothd. And I validated post-build from the <code>~/bluez-5.23/plugins</code> directory that there was a <code>bluetoothd-gat-example.o</code> file. Which tells me that the gatt-example was at least successfully built.</p>
  
  <p>I then modified the configuration file to enable LE and the attribute server. </p>
  
  <pre><code>$ sudo vi /etc/bluetooth/main.conf
  EnableLE = true           // Enable Low Energy support. Default is false.
  AttributeServer = true    // Enable the GATT attribute server. Default is false.
  </code></pre>
  
  <p>Then simply reboot or restart the bluetooth daemon...</p>
  
  <p><strong>Central Device Setup</strong></p>
  
  <p>As the central device does not need any special plugins built like the peripheral, I just installed bluez using <code>apt-get</code>. This appears to have installed v4.101 according to <code>bluetoothd -v</code>. </p>
  
  <p><strong>Session Setup</strong></p>
  
  <p>The connection process then should be fairly simple. I setup the peripheral to advertise and then connect with the central device:</p>
  
  <p>Peripheral:</p>
  
  <pre><code>$ sudo hciconfig hci0 up        // Make sure the interface is up
  $ sudo hciconfig hci0 leadv     // Set the interface to advertise
  </code></pre>
  
  <p>Central:</p>
  
  <pre><code>$ sudo hcitool -i hci0 lescan   // Scan for nearby devices advertising
  LE Scan ...
  00:02:72:C9:5E:0F (unknown)     // Not sure why two of the same MAC are found?
  00:02:72:C9:5E:0F (unknown)     // but I know this is my device...
  
  $ sudo gatttool -i hci0 -b 00:02:72:C9:5E:0F -m 48 --interactive     // Connect interactively
  [   ][00:02:72:C9:5E:0F][LE]&gt; connect
  [CON][00:02:72:C9:5E:0F][LE]&gt; primary
  attr handle: 0x0001, end grp handle: 0x0008 uuid: 00001800-0000-1000-8000-00805f9b34fb
  attr handle: 0x0010, end grp handle: 0x0010 uuid: 00001801-0000-1000-8000-00805f9b34fb
  [CON][00:02:72:C9:5E:0F][LE]&gt; characteristics 
  handle: 0x0004, char properties: 0x02, char value handle: 0x0006, uuid: 00002a00-0000-1000-8000-00805f9b34fb
  handle: 0x0007, char properties: 0x02, char value handle: 0x0008, uuid: 00002a01-0000-1000-8000-00805f9b34fb
  </code></pre>
  
  <p>And we see not one of the services or characteristics from the gatt-example are available. </p>
  
  <p><strong>Questions</strong></p>
  
  <p>--Peripheral Device</p>
  
  <ol>
  <li>How would I go about creating my own custom GATT server? Can it be a stand-alone C application or does it need to be built into bluetoothd as a plugin like the gatt-example? The answer to this question (<a href="https://stackoverflow.com/questions/19549555/creating-a-gatt-server">Creating a Gatt Server?</a>) implies you do the following: "start by initializing the GATT library and additional modules" and then "register your GATT database". But there isn't a single example of how to implement those generic statements and the link provided is simply a URL to the Bluetooth website.</li>
  <li>The GATT specifications (<a href="https://developer.bluetooth.org/gatt/Pages/default.aspx" rel="nofollow noreferrer">https://developer.bluetooth.org/gatt/Pages/default.aspx</a>) provide numerous "adopted" services and characteristics that are downloadable in XML format. But there is no instructions for how to use them?!</li>
  <li>How do a validate my GATT server is running?</li>
  </ol>
  
  <p>--Central Device</p>
  
  <ol>
  <li>Why is my central device not seeing the services and characteristics from the GATT server running on the peripheral? </li>
  </ol>
  
  <p>I can provide any additional information necessary. Thanks.</p>