我正在尝试编写我的第一个每个Android应用程序来连接到我的Raspberry Pi并使用gphoto2库控制我的DSLR。我目前有一个非常粗糙的应用程序,有两个按钮,一个“连接”按钮和一个“捕获”按钮。
package com.example.myfirstapp;
import java.util.Properties;
import com.jcraft.jsch.*;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the send button */
public void sshConnect(View view) throws Exception {
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
String host="123.456.7.89";
String user ="pi";
String pwd = "password";
int port = 22;
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("compression.s2c", "zlib,none");
config.put("compression.c2s", "zlib,none");
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, port);
session.setConfig(config);
session.setPassword(pwd);
session.connect();
Context context = getApplicationContext();
CharSequence text = "Connected to Pi";
int duration = android.widget.Toast.LENGTH_SHORT;
android.widget.Toast toast = android.widget.Toast.makeText(context, text, duration);
toast.show();
}
public static void sendCapture(Session session) throws Exception {
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("gphoto2 --capture-image");
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
}
}
我可以连接到我的覆盆子pi但是无法将jsch会话传递给“Capture”按钮而不使用以下方法从连接部分调用它:
sendCapture(session);
我希望代码使用“连接”按钮通过ssh进行连接,然后能够使用“捕获”按钮尽可能多地拍摄照片。欢迎任何问题或一般性建议。
由于
答案 0 :(得分:0)
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String ip,username,password;
ip="192.168.x.x";
username="guest";
password="guest";
//Write your code here
});