title: Holding android bluetooth connection through multiple activities
body: <p>I am building an Android app that communicates with an Arduino board via bluetooth, I have the bluetooth code in a class of it's own called BlueComms. To connect to the device I use the following methord: </p>
  
  <pre><code>public boolean connectDevice() {
      CheckBt();
      BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
      Log.d(TAG, "Connecting to ... " + device);
      mBluetoothAdapter.cancelDiscovery();
      try {
          btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
          btSocket.connect();
          outStream = btSocket.getOutputStream();
          Log.d(TAG, "Connection made.");
          return true;
  
      } catch (IOException e) {
          try {
              btSocket.close();
          } catch (IOException e2) {
              Log.d(TAG, "Unable to end the connection");
              return false;
          }
          Log.d(TAG, "Socket creation failed");
      }
      return false;
  
  }
      private void CheckBt() {
      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  
      if (!mBluetoothAdapter.isEnabled()) {
          System.out.println("Bt dsbld");
      }
  
      if (mBluetoothAdapter == null) {
          System.out.println("Bt null");
      }
  }
  </code></pre>
  
  <p>This connects fine but as soon as I leave the activity I connected through it drops the connection, showing this through LogCat, </p>
  
  <pre><code> D/dalvikvm(21623): GC_CONCURRENT freed 103K, 10% free 2776K/3056K, paused 5ms+2ms, total 35ms
  </code></pre>
  
  <p>I can no longer connect to the device, but if I call killBt() it throws a fatal error and if I try to send data I get a 'Socket creation failed' error. My send message code is as follows: </p>
  
  <pre><code>public void sendData(String data, int recvAct) {
      try {
          outStream = btSocket.getOutputStream();
      } catch (IOException e) {
          Log.d(TAG, "Bug BEFORE Sending stuff", e);
      }
  
      String message = data;
      byte[] msgBuffer = message.getBytes();
  
      try {
          outStream.write(msgBuffer);
      } catch (IOException e) {
          Log.d(TAG, "Bug while sending stuff", e);
      }
  }
  </code></pre>
  
  <p>How should I go about preventing the connection from being paused by the activity I connect with when I switch a different activity, I am switching activities with this code: </p>
  
  <pre><code>Intent myIntent = new Intent(v.getContext(), Timelapse.class);
      startActivityForResult(myIntent, 0);
  </code></pre>
  
  <p>Many Thanks, 
  Rozz</p>