title: Periodically send data to server
body: <p>I'm working on an app that receives data from Arduino through Bluetooth and sends that data to server. I've got this part working - once a user presses a button, I initiate BT connection, receive data and send it to server via Async Task:</p>
  
  <ol>
  <li><p>Thread for receiving data:</p>
  
  <pre><code>private class ConnectedThread extends Thread {
  private final InputStream mmInStream;
  private final OutputStream mmOutStream;
  
  public ConnectedThread(BluetoothSocket socket) {
  
      InputStream tmpIn = null;
      OutputStream tmpOut = null;
  
      try {
          tmpIn = socket.getInputStream();
          tmpOut = socket.getOutputStream();
      } catch (IOException e) { }
  
      mmInStream = tmpIn;
      mmOutStream = tmpOut;
  }
  
  public void run() {
      Log.i("test", "Connected thread RUN");
      byte[] buffer = new byte[256];
      int bytes;
  
      // Keep listening to the InputStream until an exception occurs
      while (true) {
          try {
              Log.i("test", "Trying....");
              bytes = mmInStream.read(buffer);
              handler.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();
          } catch (IOException e) {
              break;
          }
      }
  }
  
  public void write(String message) {
      Log.d(TAG, "...Data to send: " + message + "...");
      byte[] msgBuffer = message.getBytes();
      try {
          mmOutStream.write(msgBuffer);
      } catch (IOException e) {
          Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
      }
  }
  </code></pre>
  
  <p>}</p></li>
  <li><p>ConnectedThread handler (receives data, starts AsyncTask):</p>
  
  <pre><code>private final Handler handler = new Handler() {
      public void handleMessage(android.os.Message msg) {
          switch (msg.what) {
              case RECIEVE_MESSAGE:
                  byte[] readBuf = (byte[]) msg.obj;
                  //String readMessage = new String(readBuf, 0, msg.arg1);
                  String strIncom = new String(readBuf, 0, msg.arg1);
                  sb.append(strIncom);
                  int endOfLineIndex = sb.indexOf("\r\n");
                  if (endOfLineIndex &gt; 0) {
                      String sbprint = sb.substring(0, endOfLineIndex);
                      sb.delete(0, sb.length());
                      Log.i("test", "sbprint: " + sbprint);
                      sendDataToServer(sbprint);
                      txtData.setText("Data from Arduino: " + sbprint);
                  }
                  break;
          }
      };
  </code></pre>
  
  <p>};</p></li>
  <li><p>sendDataToServer method:</p>
  
  <pre><code>private void sendDataToServer(String dataToSend){
      if(dataToSend != null &amp;&amp; !dataToSend.equals("")) {
          sendDataAsyncTask = new SendDataAsyncTask(MyActivity.this, dataToSend);
          sendDataAsyncTask.setSendDataListener(MyActivity.this);
          sendDataAsyncTask.execute();
      }
  }
  </code></pre></li>
  </ol>
  
  <p>This, more or less works. However, now I need to repeat the process every two minutes. I've got it working with TimerTask, but of course the task stops running when device goes to sleep.</p>
  
  <p>I'm guessing I need a Service and / or an AlarmManager to wake the device every two minutes and run the above code? Any tips for this? </p>