0

I have a modified bluetooth chat program, that displays coming data from other device on screen. That works nicely, when using arduino and sending data. Now I would like to add a feature that when on screen pops message: "EMPTYING", then it would detect that and run a asynctask, but I cannot get it to work.

Here is the part that gets data and displays it on the screen.

`public void run() { byte[] buffer = new byte[1024]; int bytes;

        String strRx = "";
        int c = 0;
        int k = 0;

        //BLuetoothi pakettide vastuvõtmine ja ekraanile panek
        while (true) {
            try {
                //Mingi data tuleb, seega ekraanile
                if (connectedInputStream.available() > 0) {
                    bytes = connectedInputStream.read(buffer);
                    final String strReceived = new String(buffer);

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // textStatus.append(strReceived);
                            textStatus.setText(strReceived);


                            String strReceivedCut=strReceived.substring(0,8); //Lõikab stringist esimese osa välja
                            boolean resultOfComparison=new String("EMPTYING").equals(strReceivedCut);
                            if(resultOfComparison==true) {
                                testkast.setText("It Happened");

                                //long timeNow= System.currentTimeMillis(); //Praegune hetk

                                //long startTime = System.currentTimeMillis();
                                //if(timeNow-startTime>1000*60*10) { //Should be 10 minutes
                                new UploadBySSH().execute(strReceived); //Saadame algse stringi SSH-ga servusse
                                //}

                            }
                        }
                    });



                    //Mingit datat ei tule, seega ootame Sleep
                } else SystemClock.sleep(100);

                //Keepalive osa, mis saadab ? mingi aja tagant
                //hetkel on ajaks 10*100=1000 millisekundit
                //Originaal programmi keepalive:
                //;E??????????;E??????????;E??????????;E??????????;E??????????;E??????????
                c++;

                if (c > 10) {
                    String kl = "?";
                    byte[] bytesToSend = kl.getBytes();
                    connectedOutputStream.write(bytesToSend);
                    c = 0;
                    k++;
                }


            } catch (IOException e) {
                e.printStackTrace();
                final String msgConnectionLost = "Ühendus nurjus: PANE PROGRAMM KINNI\n"
                        + e.getMessage();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        textStatus.setText(msgConnectionLost);
                    }
                });
            }
        }


    }`

Here is the part that would ssh into a server.

private class UploadBySSH extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... args) {

        try {
            JSch ssh = new JSch();
            Session session = ssh.getSession("root", "192.168.4.234", 22);
            // Remember that this is just for testing and we need a quick access, you can add an identity and known_hosts file to prevent
            // Man In the Middle attacks
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.setPassword("password");

            session.connect();
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            channel.setOutputStream(baos);

            channel.setCommand("/usr/bin/python /mnt/sda2/www/insert.py "+args[0]);
            channel.connect();
            channel.disconnect();

            Boolean success = true;
            session.disconnect();

        } catch (JSchException e) {
            System.out.println(e.getMessage().toString());
            e.printStackTrace();

        }
        return "Executed";
    }

}

Could anybody give some hints? Cannot that first part of code not find the ssh code? :S

Kaarel
  • 1
  • 2

0 Answers0