3

我正在尝试编写我的第一个每个 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();

    }
}

我可以连接到我的树莓派,但如果不从连接部分调用它,我无法将 jsch 会话传递给“捕获”按钮:

sendCapture(session);

我希望代码使用“连接”按钮通过 ssh 连接,然后能够使用“捕获”按钮尽可能多地拍照。欢迎任何问题或一般建议。

谢谢

4

1 回答 1

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 

 });
于 2012-08-31T09:58:26.627 回答