title: Bluetooth programming in C
body: <p>I managed to successfully connect two android phones using apps i made and I tried to use what I learned there to connect an android phone to my raspberry pi( which uses Raspbian Jessie ). I tried to implement the server side on the raspberry and use the client version of my android app. But i noticed something was missing in the code this gentleman wrote: <a href="http://people.csail.mit.edu/albert/bluez-intro/x502.html" rel="nofollow">http://people.csail.mit.edu/albert/bluez-intro/x502.html</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;bluetooth/bluetooth.h&gt;
  #include &lt;bluetooth/rfcomm.h&gt;
  
  int main(int argc, char **argv)
  {
      struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
      char buf[1024] = { 0 };
      int s, client, bytes_read;
      socklen_t opt = sizeof(rem_addr);
  
      // allocate socket
      s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  
      // bind socket to port 1 of the first available 
      // local bluetooth adapter
      loc_addr.rc_family = AF_BLUETOOTH;
      loc_addr.rc_bdaddr = *BDADDR_ANY;
      loc_addr.rc_channel = (uint8_t) 1;
      bind(s, (struct sockaddr *)&amp;loc_addr, sizeof(loc_addr));
  
      // put socket into listening mode
      listen(s, 1);
  
      // accept one connection
      client = accept(s, (struct sockaddr *)&amp;rem_addr, &amp;opt);
  
      ba2str( &amp;rem_addr.rc_bdaddr, buf );
      fprintf(stderr, "accepted connection from %s\n", buf);
      memset(buf, 0, sizeof(buf));
  
      // read data from the client
      bytes_read = read(client, buf, sizeof(buf));
      if( bytes_read &gt; 0 ) {
          printf("received [%s]\n", buf);
      }
  
      // close connection
      close(client);
      close(s);
      return 0;
  }
  </code></pre>
  
  <p>In android I had to hard code a UUID into both the server and the client side but here I see no reference to anything that might be a UUID. Why is that? </p>
  
  <p>This is the android code:
      public class MainActivity extends AppCompatActivity {</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      if (!mBluetoothAdapter.isEnabled()) {
          System.exit(1);
      }
  
      Set&lt;BluetoothDevice&gt; pairedDevices = mBluetoothAdapter.getBondedDevices();
      Object[] pairedDev = pairedDevices.toArray();
      ArrayAdapter&lt;String&gt; mArrayAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, 0);
      /*if (pairedDevices.size() &gt; 0) {
          for (BluetoothDevice device : pairedDevices) {
              mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
          }
      }
      ListView listView = (ListView) findViewById(R.id.PairedDevicesListView);
      listView.setAdapter(mArrayAdapter);*/
      ConnectThread connectThread = new ConnectThread((BluetoothDevice) pairedDev[1]);
      connectThread.run();
      connectThread.cancel();
  }
  
  public void buttonOnClick(View v)
  {
      Button button = (Button) v;
  
  }
  
  @Override
  protected void onResume() {
      super.onResume();
  }
  
  @Override
  protected void onStart() {
      super.onStart();
  }
  
  @Override
  protected void onPause() {
      super.onPause();
  }
  
  @Override
  protected void onStop() {
      super.onStop();
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
  }
  
  @Override
  protected void onRestart() {
      super.onRestart();
  }
  
  
  private class ConnectThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final BluetoothDevice mmDevice;
  
      public ConnectThread(BluetoothDevice device) {
          BluetoothSocket tmp = null;
          mmDevice = device;
  
          try {
              tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("F4C20AC0-3DF8-11E6-BDF4-0800200C9A66"));
          } catch (IOException e) { }
          mmSocket = tmp;
      }
  
      public void run() {
          BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          mBluetoothAdapter.cancelDiscovery();
  
          try {
              mmSocket.connect();
          } catch (IOException connectException) {
              try {
                  mmSocket.close();
                  System.exit(1);
              } catch (IOException closeException) { System.exit(1); }
              return;
          }
  
          // Do work to manage the connection (in a separate thread)
          // manageConnectedSocket(mmSocket);
          byte[] bytes = new byte[3];
          bytes[0] = 1;
          bytes[1] = 3;
          bytes[2] = 2;
          ConnectedThread connectedThread = new ConnectedThread(mmSocket);
          connectedThread.write(bytes);
      }
  
      /** Will cancel an in-progress connection, and close the socket */
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) { }
      }
  }
  
  private class ConnectedThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final InputStream mmInStream;
      private final OutputStream mmOutStream;
  
      public ConnectedThread(BluetoothSocket socket) {
          mmSocket = socket;
          InputStream tmpIn = null;
          OutputStream tmpOut = null;
  
          // Get the input and output streams, using temp objects because
          // member streams are final
          try {
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream();
          } catch (IOException e) { }
  
          mmInStream = tmpIn;
          mmOutStream = tmpOut;
      }
  
      public void run() {
          byte[] buffer = new byte[1024];  // buffer store for the stream
          int bytes; // bytes returned from read()
  
          // Keep listening to the InputStream until an exception occurs
          while (true) {
              try {
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer);
                  // Send the obtained bytes to the UI activity
                  // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
              } catch (IOException e) {
                  break;
              }
          }
      }
  
      /* Call this from the main activity to send data to the remote device */
      public void write(byte[] bytes) {
          try {
              mmOutStream.write(bytes);
          } catch (IOException e) { }
      }
  
      /* Call this from the main activity to shutdown the connection */
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) { }
      }
  }
  }
  </code></pre>