0

我有一个修改过的蓝牙聊天程序,可以在屏幕上显示来自其他设备的数据。这在使用 arduino 和发送数据时效果很好。现在我想添加一个功能,当屏幕上弹出消息:“EMPTYING”时,它会检测到并运行异步任务,但我无法让它工作。

这是获取数据并将其显示在屏幕上的部分。

`public void run() { byte[] buffer = new byte[1024]; 整数字节;

        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);
                    }
                });
            }
        }


    }`

这是将 ssh 连接到服务器的部分。

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";
    }

}

有人可以给出一些提示吗?代码的第一部分不能找到 ssh 代码吗?:S

4

0 回答 0