title: How to program a Bluetooth LE device using C on Linux x86?
body: <p>I have a bluetooth device which I can control using gatttool on linux. I want to develop my own c program that can send commands to it. </p>
  
  <p>I have done bluetooth programming in the past and it is relatively straightforward, similar to network programming but this time, it is a bluetooth low energy device and following the principles <a href="http://people.csail.mit.edu/albert/bluez-intro/c404.html" rel="noreferrer">here</a> results in a host is down message when I can clearly connect/disconnect from it using gatttool.</p>
  
  <p>How do I create this program? I know I should be using the bluez library but I am not sure where to start with Low energy devices.</p>
  
  <pre><code>int main(int argc, char **argv)
  {
     struct sockaddr_rc addr = { 0 };
     int s, status;
     char dest[18] = "B4:99:4C:5C:EE:49";
     char buf[2048];
     pthread_t rthread;
  
     setbuf(stdout, NULL); 
     // allocate a socket
     s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
     // set the connection parameters (who to connect to)
     addr.rc_family = AF_BLUETOOTH;
     addr.rc_channel = (uint8_t) 1;
     str2ba( dest, &amp;addr.rc_bdaddr );
     // connect to server
     status = connect(s, (struct sockaddr *)&amp;addr, sizeof(addr));
  
     if( status &lt; 0 ){
        perror("Error connecting to host\n");
        exit(1);
     }
  
     while(fgets(buf, sizeof(buf), stdin) != NULL){
        status = send(s, buf, sizeof(buf), 0);
        if(status &lt; 0){
           printf("Error sending.\n");
       exit(1);
        }
     }
  
     close(s);
  
     return;
  </code></pre>