title: TCP & UDP Over Wireless with Google Glass
body: <p>I have developed both TCP and UDP applications for Google Glass. Here is the implementation for UDP:</p>
  
  <pre><code>public class MainActivity extends Activity
  {
      private TextView mTextView;
      private someRunnable mRun;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity);
          mTextView = (TextView) findViewById(R.id.textView);
  
          Handler mHandler = new Handler()
          {
  
              @Override
              public void handleMessage(Message msg)
              {
                  Bundle bundle = msg.getData();
                  mTextView.setText(bundle.getString("message"));
                  super.handleMessage(msg);
              }
          };
  
          try
          {
              mRun = new someRunnable(mHandler);
              (new Thread(mRun)).start();
          }
          catch (IOException e)
          {
              Log.e("Error", "Unable to start Network activity");
              Log.e("Error", e.getLocalizedMessage());
          }
      }
  
      @Override
      protected void onDestroy()
      {
          mRun.stopNetworkActivity();
          super.onDestroy();
      }
  }
  </code></pre>
  
  <p>I have a server constantly sending out packets every .5 seconds containing numbers. The Glass application receives the data and displays it on a TextView. I know that UDP is not reliable, but even with TCP I would get random "pauses" that would last for about 4 seconds followed by a lot of packet resends - By pausing of course, I mean that Glass is just dropping packets forcing the server to resend. Or even worse, with UDP just "drop and move on". </p>
  
  <p>After doing further analysis, the application seems to pause <strong>ONLY</strong> when the following is shown in LogCat: </p>
  
  <pre><code>01-05 16:22:43.812: W/BluetoothAdapter(951): getBluetoothService() called with no BluetoothManagerCallback
  01-05 16:22:43.812: D/BluetoothSocket(951): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[122]}
  01-05 16:22:48.203: W/bt-sdp(951): SDP - Rcvd conn cnf with error: 0x4  CID 0x40
  01-05 16:22:48.203: E/btif_sock_rfcomm(951): find_rfc_slot_by_id unable to find RFCOMM slot id: 1356
  01-05 16:22:53.336: W/bt-sdp(951): SDP - Rcvd conn cnf with error: 0x4  CID 0x42
  01-05 16:22:53.336: E/btif_sock_rfcomm(951): find_rfc_slot_by_id unable to find RFCOMM slot id: 1357
  01-05 16:22:53.336: W/BluetoothMapClientService(951): connection failed
  01-05 16:22:53.336: W/BluetoothMapClientService(951): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:521)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:532)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:323)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at com.android.bluetooth.map.BluetoothMapClientService.connect(BluetoothMapClientService.java:262)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at com.android.bluetooth.map.BluetoothMapClientService$1.connect(BluetoothMapClientService.java:94)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at android.bluetooth.IBluetoothMapClient$Stub.onTransact(IBluetoothMapClient.java:60)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at android.os.Binder.execTransact(Binder.java:404)
  01-05 16:22:53.336: W/BluetoothMapClientService(951):   at dalvik.system.NativeStart.run(Native Method)
  </code></pre>
  
  <p>Once the IOException is thrown, everything goes back to normal and functions smoothly. </p>
  
  <p>If I connect Glass to my phone via bluetooth, the message above does not display and everything runs perfectly (no pausing).</p>
  
  <p>Why is the BluetoothSocket having such an impact on my application? Is there anything that I can do besides forcing the client to be connected via bluetooth before running the application?</p>