I try to make an application that runs a script on my raspberrypi to turn on a LED. This should be managed with an android app. I used jsch to create a ssh tunnel to execute the script. But if I press the button nothing happens and logcat doesnt do anything.
I tried to check if its even possible to create a ssh connection to my pi from the emulator and downloaded JuiceSSH and everthing went fine...
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button btUnlock = findViewById(R.id.btUnlock);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
btUnlock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
{
new ScriptTask().execute();
}
}
});
}
}
class ShellExecuter {
private static String username="pi";
private static String hostname="XXX";
private static String password="XXX";
private static int port=22;
public void executeScript(String scriptname){
try{
JSch jsch= new JSch();
Session session= jsch.getSession(username, hostname, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("XXXX");
session.connect();
ChannelExec channelExec=(ChannelExec)session.openChannel("exec");
InputStream in= channelExec.getInputStream();
channelExec.setCommand("sh "+scriptname);
channelExec.connect();
channelExec.disconnect();
session.disconnect();
}
catch (Exception e){
System.err.println("Error: " + e);
}
}
}
class ScriptTask extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
ShellExecuter shellExecuter=new ShellExecuter();
shellExecuter.executeScript("on.sh");
return null;
}
}
Unfortunately I dont have any error message. It just happens nothing...