title: android bluetooth chat app (device loses connection immediately)
body: <p>I have been going through several threads on here and didn't come across an answer to the issue I am running into.</p>
  
  <p>My setup:
  I have a Mac pc that I am using as a virtual serial port to communicate with my android Nexus S phone. Running the bluetooth chat app on the phone and using it as a client to talk to the virt comm I set up. </p>
  
  <p>Initially I tried the bluetooth chat app with 2 android phones to confirm it works, which it does. I can send texts back and forth.</p>
  
  <p>My Use case:
  I have a device that reads RFid tags and sends the data to an android phone to collect the info.</p>
  
  <p>I am using my PC to represent my device for now. </p>
  
  <p>++++++++++++++++++
  Ok to the problem,</p>
  
  <p>I try to connect to the pc from my phone and initially I get a "connecting...." status bar update and after 15secs or so I get a toast message saying "I am connected to the pc" but immediately after I get "device lost connection" toast. Then the status bar goes to "not connected"</p>
  
  <p>When I step through with the debugger, it seems to fail at the following portion of the bluetooth chat app. Specifically this line (<code>bytes = mmInStream.read(buffer);</code>)</p>
  
  <pre><code>    public void run() {
          Log.i(TAG, "BEGIN mConnectedThread");
          byte[] buffer = new byte[1024];
          int bytes;
  
          // Keep listening to the InputStream while connected
          while (true) {
              try {
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer);
  
                  // Send the obtained bytes to the UI Activity
                  mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "disconnected", e);
                  connectionLost();
                  break;
              }
          }
      }
  </code></pre>
  
  <p>When I look in logcat, the i/o exception is "<code>software caused connection abort</code>"
  for the read() on inputstream.</p>
  
  <p>Questions:
  Does this have to do with my virtual port not setup right? I have the terminal up and waiting to receive input on /dev/tty.Nexus....
  using the screen command @ 9600 baud</p>
  
  <p>Otherwise, I thought maybe the socket which the inputstream connects to is unavailable somehow. I printed that to log and it seems like it was not NULL. Every time I step through though it dies at the <strong>ConnectThread</strong> not in the <strong>ConnectedThread</strong>.</p>
  
  <p>The following portion of code: specifically this line (<code>mmSocket.connect();</code>)</p>
  
  <pre><code>        public void run() {
          Log.i(TAG, "BEGIN mConnectThread");
          setName("ConnectThread");
  
          // Always cancel discovery because it will slow down a connection
          mAdapter.cancelDiscovery();
  
          // Make a connection to the BluetoothSocket
          try {
              // This is a blocking call and will only return on a
              // successful connection or an exception
              mmSocket.connect();
          } catch (IOException e) {
              connectionFailed();
              // Close the socket
              try {
                  mmSocket.close();
              } catch (IOException e2) {
                  Log.e(TAG, "unable to close() socket during connection failure", e2);
              }
              // Start the service over to restart listening mode
              BluetoothChatService.this.start();
              return;
          }
  
          // Reset the ConnectThread because we're done
          synchronized (BluetoothChatService.this) {
              mConnectThread = null;
          }
  
          // Start the connected thread
          connected(mmSocket, mmDevice);
      }
  </code></pre>
  
  <p>I wonder if the socket variable is losing scope due to multi-threading and the socket is being passed around?</p>
  
  <p>Thanks</p>