title: Bluetooth Examples for Android
body: <p>Does anybody know of any example available that illustrates Bluetooth development on Android.</p>
  
  <p>I have read the tutorial <a href="http://developer.android.com/guide/topics/wireless/bluetooth.html" rel="noreferrer">here</a> and I understand everything on that page. </p>
  
  <p>However when it comes to implementing the Bluetooth code, into an application it is necessary to view the Bluetooth Chat example to understand how it all works. </p>
  
  <p>Bluetooth Chat example <a href="http://developer.android.com/resources/samples/BluetoothChat/index.html" rel="noreferrer">here</a></p>
  
  <p>This example is good, but it is also hard to comprehend because each device is initially set up to be a server.</p>
  
  <p><em>Who is the server and do both devices send out server sockets until one device scans?</em></p>
  
  <p><em>Once a device makes itself discoverable does it become the server?</em></p>
  
  <p><em>When does the OnResume activity start, because once that starts and the mChatService has been initialized in SetupChat, the device will start an Accept thread.</em></p>
  
  <p>Some code examples are given below, and link to the full Bluetooth chat is available above.</p>
  
  <pre><code>@Override
  public synchronized void onResume() {
      super.onResume();
      if(D) Log.e(TAG, "+ ON RESUME +");
  
      // Performing this check in onResume() covers the case in which BT was
      // not enabled during onStart(), so we were paused to enable it...
      // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
      if (mChatService != null) {
          // Only if the state is STATE_NONE, do we know that we haven't started already
          if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
            // Start the Bluetooth chat services
            mChatService.start();
          }
      }
  }
  
  private void setupChat() {
  
      // Initialize the BluetoothChatService to perform bluetooth connections
      mChatService = new BluetoothChatService(this, mHandler);
  
      // Initialize the buffer for outgoing messages
      mOutStringBuffer = new StringBuffer("");
  }
  
  
  /**
   * Start the chat service. Specifically start AcceptThread to begin a
   * session in listening (server) mode. Called by the Activity onResume() */
  public synchronized void start() {
      if (D) Log.d(TAG, "start");
  
      // Cancel any thread attempting to make a connection
      if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
  
      // Cancel any thread currently running a connection
      if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
  
      setState(STATE_LISTEN);
  
      // Start the thread to listen on a BluetoothServerSocket
      if (mSecureAcceptThread == null) {
          mSecureAcceptThread = new AcceptThread(true);
          mSecureAcceptThread.start();
      }
      if (mInsecureAcceptThread == null) {
          mInsecureAcceptThread = new AcceptThread(false);
          mInsecureAcceptThread.start();
      }
  }
  </code></pre>
  
  <p>What I am asking for is for any examples of Bluetooth, that are easier to understand and examples that clearly segregate the server side and the client side of Bluetooth.
  <em>I have Google'd this, and I have read all details available on the developer.android.com website.</em></p>