1

I'm trying to send commands from my android phone to Raspberry Pi via WiFi. This is what I have done: I've setup RPi as a wifi access point, and now I'm able to connect to it with android phone.

Now I have written a simple app in Android Studio to send a command using SSH. But unfortunately it's not working. The following are my codes:

MainActivity.java

package example_company.controller1;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
    }

    public void onClick11 (View v) {
        new AsyncTask<Integer, Void, Void>(){
            @Override
            protected Void doInBackground(Integer... params) {
                try {
                    executeSSHcommand();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(1);
    }

    public void executeSSHcommand(){
        String user = "pi";
        String password = "raspberry";
        String host = "192.168.0.1";
        int port=22;
        try{

            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setTimeout(10000);
            session.connect();
            ChannelExec channel = (ChannelExec)session.openChannel("exec");
            channel.setCommand("omxd n");
            channel.connect();
            channel.disconnect();

        }
        catch(JSchException e){

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="example_company.controller1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="NEXT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="onCLick11" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example_company.controller1">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

So, what's wrong !? What am I missing !?

Your help and support are highly appreciated.

EDIT 1

I added try{Thread.sleep(1000);}catch(Exception ee){} between channel.connect(); and channel.disconnect(); as suggested in the similar question. But no help. Still the same problem. When I press the button, nothing happens.

EDIT 2

The following are the messages of gradle build:

Information:Gradle tasks [:app:assembleDebug]
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.Channel$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ChannelSftp$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ChannelSftp$2) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ChannelSftp$3) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ConfigRepository$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ConfigRepository$2) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.JSch$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.Util$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Information:BUILD SUCCESSFUL
Information:Total time: 1 mins 8.318 secs
Information:64 errors
Information:0 warnings
Information:See complete output in console
Machavity
  • 30,841
  • 27
  • 92
  • 100
Omid1989
  • 419
  • 3
  • 8
  • 17
  • 1
    `unfortunately it's not working.` That is not good info. Tell more. What should happen? What happens instead? How far does your code run? Why no log statements used? – greenapps Nov 02 '17 at 13:09
  • `catch(JSchException e){ }`. Dont leave that catch blok empty as now you will never know is there is a catch. Put the normal code in it an log and toast the exception. Does your app crash? – greenapps Nov 02 '17 at 13:10
  • `channel.connect(); channel.disconnect(); ` ? That quick? Dont you wanna wait for a response? – greenapps Nov 02 '17 at 13:12
  • @greenapps Thanks for your comments. When the button is pressed, it should send the command `omxd n` via SSH to RPi with specified IP, username, password and port. But it doesn't send. Also `logcat` doesn't show anything. – Omid1989 Nov 02 '17 at 13:25
  • @greenapps I'm using a similar code to this: https://stackoverflow.com/questions/41050989/basic-ssh-connection-via-jsch-on-android/43147951 – Omid1989 Nov 02 '17 at 13:26
  • @greenapps there is another problem with `logcat` : When I connect my phone to RPi with wifi, the `logcat` becomes offline and doesn't show anything. – Omid1989 Nov 02 '17 at 13:28
  • @MartinPrikryl I added an edit to declare that the similar question's answer didn't help me. – Omid1989 Nov 02 '17 at 13:50
  • @greenapps As written in an EDIT in the above question, adding `try{Thread.sleep(1000);}catch(Exception ee){}` between connect and disconnect. But no help. – Omid1989 Nov 02 '17 at 13:52
  • But what do you expect to happen? Your code does not do anything visually. It just runs the command and nothing else. – Martin Prikryl Nov 02 '17 at 13:53
  • @MartinPrikryl I will see the effect of the command in RPi's screen. But I see nothing. When I use ready apps like JuiceSSH to do the same, I clearly see the effect. But this way, there is nothing. – Omid1989 Nov 02 '17 at 13:55
  • Well, so do read the command error output and check exit code to see if anything went wrong. – Martin Prikryl Nov 02 '17 at 14:00
  • @greenapps I added ***EDIT 2*** which are messages of gradle build. Does it help anything ? – Omid1989 Nov 02 '17 at 14:00
  • @MartinPrikryl I added ***EDIT 2*** which are messages of gradle build. Does it help anything ? – Omid1989 Nov 02 '17 at 14:01
  • I do not think it explains anything. Moreover, you should post [mcve]. I.e. a simple Java console application. And not complicate your question with all the (most probably) unrelated Android GUI stuff. – Martin Prikryl Nov 02 '17 at 14:02
  • `I added try{Thread.sleep(1000);}catch(Exception ee){} between channel.connect(); and channel.disconnect(); as suggested in the similar question.` Nonsense. Martin Prikryl gave you a link what you have to do instead. – greenapps Nov 02 '17 at 14:16
  • `When I press the button, nothing happens.` So your async task is not started? Then you could say so. And why is that catch block still empty? Please remove the gradle stuff. And why did you not add log statements? It's irrelevant where your Android device is connected to with wifi. Should not have anything to do with logcat. – greenapps Nov 02 '17 at 14:17
  • @greenapps Ok. Done. I solved the problem. I will update and add answer. – Omid1989 Nov 02 '17 at 14:21

1 Answers1

3

I solved this issue by changing two things:

  1. Adding try{Thread.sleep(1000);}catch(Exception ee){} between connect and disconnect lines. Instead of 1000, I used higher values, like 10000.
  2. The process in RPi which was listening to android, was dead! Irealized this with JuiceSSH. So, after solving the problem of this process in RPi, it became active and alive.

After these two changes, the android app worked perfectly.

Thanks @greenapps and @MartinPrikryl for their helps.

Omid1989
  • 419
  • 3
  • 8
  • 17