我的是Android & Java的新手,這裏是我的情況:如何重複使用SSH(Jsch)會議
我試圖創建它連接到一個Beaglebone的應用黑色使用ssh連接,然後通過發出來自Android設備的命令來控制連接到BBB的一些外圍設備。 當用戶看到啓動畫面時,我在AsyncTask中打開(成功)ssh會話,如果連接成功,用戶將得到確認,然後可以通過單擊某些可用按鈕發送預定義的命令。
接下來我想做的事情是打開會話,然後每次我想發出一個命令並等待來自BBB的響應時創建一個新通道(exec或shell),但我不知道如何在AsynkTask之外重用這樣的ssh會話。
是甚至可能的?
我使用過Android Studio 0.8.2和Jsch 0.1.51,我的代碼如下:
公共類閃屏擴展ActionBarActivity {
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 <String, Void, String>{
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();
}
}
}
//這裏就是我想重複使用已打開的會話並創建新頻道
public class sendCommand extends AsyncTask <String, Void, String >{
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);
}
}
如果需要其他內容,請告訴我! (這是我第一次在論壇上提問)
非常感謝您的時間和支持!
謝謝@Damienknight,我在想,太多,螺母我怎麼能「呼」我對「doInBackgroud」方法打開的會話? – Laus23 2014-09-30 07:46:01
在doInBackground外部創建會話,並在調用doInBackground時傳遞會話。從會話對象中,您可以檢索頻道,輸入/輸出流等。 – Damienknight 2014-09-30 17:39:51
我想我知道您的意思,我要去嘗試一下, – Laus23 2014-10-06 07:41:01