title: How to reuse a SSH (Jsch) session created in an AsyncTask in another class
body: <p>I'm a Android &amp; Java newbie and here is my situation:</p>
  
  <p>I'm trying to create an app which connects to a Beaglebone Black using a ssh connection and then controls some peripherals connected to the BBB by issuing commands coming from an Android device.
  I'm opening (successfully) an ssh session in an AsyncTask while the user sees an splash screen, if the connection was successful the user will get a confirmation and then will be able to send predefined commands by clicking some available buttons.</p>
  
  <p>What I want to do next is left the session opened and then create a new channel (exec or shell ) each time I wish to issue a command and wait for the response from the BBB, but I don´t know how to reuse such ssh session outside the AsynkTask.</p>
  
  <p>is that even possible?  </p>
  
  <p>I'm using Android Studio 0.8.2 and Jsch 0.1.51, my code is as follows:</p>
  
  <p>public class SplashScreen extends ActionBarActivity {</p>
  
  <pre><code>public static final int segundos =10;
  public static final int milisegundos =segundos*1000;
  public static  final int delay=2;
  private ProgressBar pbprogreso;
  @Override
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_splash_screen);
      pbprogreso= (ProgressBar)findViewById(R.id.pbprogreso);
      pbprogreso.setMax(maximo_progreso());
      empezaranimacion();
  }
  
  public void empezaranimacion()
  {
      sshConnect task = new sshConnect();
      task.execute(new String[] {"http:"});
  
      new CountDownTimer(milisegundos,1000)
  
      {
          @Override
      public void onTick(long milisUntilFinished){
              pbprogreso.setProgress(establecer_progreso(milisUntilFinished));
          }
         @Override
      public void onFinish(){
             finish();
         }
      }.start();
  
  }
  public  int establecer_progreso (long miliseconds)
  {
      return (int)((milisegundos-miliseconds)/1000);
  }
  
  public int maximo_progreso () {
      return segundos-delay;
  }
  
  public class sshConnect extends AsyncTask &lt;String, Void, String&gt;{
      ByteArrayOutputStream Baos=new ByteArrayOutputStream();
      ByteArrayInputStream Bais = new ByteArrayInputStream(new byte[1000]);
      @Override
      protected String doInBackground(String... data) {
  
          String host = "xxxxxxx";
          String user = "root";
          String pwd = "";
          int port = 22;
          JSch jsch = new JSch();
          try {
              Session session = jsch.getSession(user, host, port);
              session.setPassword(pwd);
              session.setConfig("StrictHostKeyChecking", "no");
              session.connect();
  
              ChannelExec channel = (ChannelExec)session.openChannel("exec");
              channel.setOutputStream(Baos);
              channel.setInputStream(Bais);
              //Run Command
              channel.setCommand("python ~/BBB_test/testconnect.py");
              channel.connect();
              try{Thread.sleep(3500);}catch (Exception ee){}
              channel.disconnect();
              //session.disconnect();
          }catch (Exception e){
              System.out.println(e.getMessage());
          }
          return Baos.toString();
      }
      @Override
      protected void onPostExecute(String result) {
          if (result.equals("Connected to BBB Baby!!\n")) {
              Intent nuevofrom = new Intent(SplashScreen.this, Principal.class);
              startActivity(nuevofrom);
              finish();
          } else {
              Intent newfrom = new Intent(SplashScreen.this, ConnecError.class);
              startActivity(newfrom);
              finish();
  
          }
      }
  }
  </code></pre>
  
  <p>//Here is where I want to reuse the opened session and create a new channel</p>
  
  <pre><code>public class sendCommand extends AsyncTask &lt;String, Void, String &gt;{
      ByteArrayOutputStream Baosc=new ByteArrayOutputStream();
      ByteArrayInputStream Baisc = new ByteArrayInputStream(new byte[1000])
  
      protected String doInBackground (String... command){
          try {
              ChannelExec channel2 = (ChannelExec)session.openChannel("exec");
              channel2.setOutputStream(Baosc);
              channel2.setInputStream(Baisc);
              //Run Command
              channel2.setCommand("python ~/BBB_test/testgpio.py");
              channel2.connect();
              try{Thread.sleep(3500);}catch (Exception ee){}
              channel2.disconnect();
  
  
          }catch (Exception e) {
              System.out.println(e.getMessage());
          }
          return  Baosc.toString();
      }
      protected void onPostExecute(Long result) {
          TextView txt = (TextView) findViewById(R.id.infotext);
          txt.setText(result);
  
      }
  
  }
  </code></pre>
  
  <p>If something else is needed  let me know! (it is the first time I ask something in a forum)</p>
  
  <p>Thanks a lot for your time and support!</p>