title: How to Listen from bluetooth socket in android background service?
body: <p>So I'm try to make service in Android app, this is my first App on android (&amp; on Java too)), so i have a great number of problems. But the main problem is "architecture" my background service.</p>
  
  <p>I need to set a connection via Bluetooth to device (My custom device). I have my own protocol (still in progress), and so I need in my Background service for the following things:</p>
  
  <ol>
  <li>Sent something</li>
  <li>Always listen to InputStream! (all the time)</li>
  </ol>
  
  <p>How i can create this to things in my service? 
  I need to create two thread's in my service?</p>
  
  <p>I have tried a lot of "ideas" as to how to? and I have very huge "dirty" code... and every time I run it, it crashes.</p>
  
  <pre><code>public class MyService extends Service {
  
  public BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  public BluetoothSocket socket;
  public String readMessage;
  public int BTC_STATE;
  ExecutorService es;
  
  
  public void onCreate() {
      super.onCreate();
      es = Executors.newFixedThreadPool(2);
  }
  
  
  }
  
   public void onDestroy() {
      super.onDestroy();
  
  }
  // ЗАПУСК СЕРВИСА
  public int onStartCommand(Intent intent, int flags, int startId) {
      int time = intent.getIntExtra(DashboardActivity.PARAM_TIME, 1);
      int task = intent.getIntExtra(DashboardActivity.PARAM_TASK, 0);
      String strMAC = intent.getStringExtra(DashboardActivity.PARAM_MAC);
      String strMESS = intent.getStringExtra(DashboardActivity.PARAM_MESSAGE);
      MyRun mr = new MyRun(startId, time, task,strMAC,strMESS);
      es.execute(mr);
  
      return super.onStartCommand(intent, flags, startId);
  }
  
  public IBinder onBind(Intent arg0) {
      return null;
  }
  
  class MyRun implements Runnable {
  
      int time;
      int startId;
      int task;
      int target;
      String strMAC;
      String strMESS;
  
      public MyRun(int startId, int time, int task,String strMAC,String strMESS) {
          this.time = time;
          this.startId = startId;
          this.task = task;
          this.target = 1;
          this.strMAC = strMAC;
          this.strMESS = strMESS;
  
      }
  
      public void run() {
          Intent intent = new Intent(DashboardActivity.BROADCAST_ACTION);
  //here, depending on task id i sent reqwest. But where i need to create listning method?
  
  
  }
  </code></pre>
  
  <p>In run() i have different tasks... But where i need to create listning method for my BTE socket InPutStream ?</p>