Advertisement
Guest User

Java JSch code

a guest
Feb 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package com.kiranthepro.epizy.com.raspberrypirobotcontroller;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.webkit.WebView;
  7. import android.webkit.WebViewClient;
  8.  
  9. import com.jcraft.jsch.JSch;
  10. import com.jcraft.jsch.Channel;
  11. import com.jcraft.jsch.ChannelExec;
  12. import com.jcraft.jsch.JSchException;
  13. import com.jcraft.jsch.Session;
  14.  
  15. public class RobotController extends AppCompatActivity {
  16.     private WebView streamWebView;
  17.     String host;
  18.     String user;
  19.     String password;
  20.  
  21.     public static Session session;
  22.     public static ChannelExec channel;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_robot_controller);
  28.  
  29.         host=getIntent().getExtras().getString("com.kiranthepro.epizy.com.raspberrypirobotcontroller.HOSTNAME");
  30.         user=getIntent().getExtras().getString("com.kiranthepro.epizy.com.raspberrypirobotcontroller.USERNAME");
  31.         password=getIntent().getExtras().getString("com.kiranthepro.epizy.com.raspberrypirobotcontroller.PASSWORD");
  32.  
  33.         initStreamConnection();
  34.  
  35.         new makeSSHConnection().execute();
  36.  
  37.     }
  38.  
  39.  
  40.  
  41.     private class makeSSHConnection extends AsyncTask<Void, Void, Void> {
  42.  
  43.         @Override
  44.         protected Void doInBackground(Void... voids) {
  45.  
  46.             try {
  47.                 initSSHConnection();
  48.                 System.out.println("IT WORKED!");
  49.             } catch (Exception e) {
  50.                 System.out.println(e);
  51.             }
  52.  
  53.             return null;
  54.         }
  55.     }
  56.  
  57.     public void initStreamConnection() {
  58.  
  59.         streamWebView = (WebView)findViewById(R.id.PiLiveStream);
  60.         streamWebView.setWebViewClient(new WebViewClient());
  61.         streamWebView.loadUrl(host);
  62.         streamWebView.getSettings().setBuiltInZoomControls(true);
  63.         streamWebView.getSettings().setDisplayZoomControls(false);
  64.     }
  65.  
  66.  
  67.     public void initSSHConnection() throws JSchException {
  68.  
  69.         JSch jsch = new JSch();
  70.         Session session = jsch.getSession(user, host, 22);
  71.         session.setPassword(password);
  72.         session.setTimeout(10000);
  73.         java.util.Properties config = new java.util.Properties();
  74.         config.put("StrictHostKeyChecking", "no");
  75.         session.setConfig(config);
  76.         session.connect();
  77.  
  78.         channel = (ChannelExec)session.openChannel("exec");
  79.         channel.setCommand("python3 PythonFiles/robot_controller_scripts/moveRobotSetupPins.py");
  80.         System.out.println("Successfully set up pins!");
  81.  
  82.         System.out.println(host + "\n" + user + "\n" + password);
  83.     }
  84.  
  85.     public void sendFwdCmd() {
  86.         channel.sendCommand("python3 PythonFiles/robot_controller_scripts/moveRobotScriptSSH.py F");
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement