简体   繁体   English

通过Android应用将字符串发送到Java Server

[英]Send string over Android app to java Server

I'm using this code. 我正在使用此代码。 Server and android app. 服务器和Android应用。

https://github.com/luugiathuy/Remote-Bluetooth-Android https://github.com/luugiathuy/Remote-Bluetooth-Android

I can send int commands but I want to send strings for more information 我可以发送int命令,但我想发送字符串以获取更多信息

I create the issue in the repository but I want all the help posible 我在存储库中创建问题,但是我希望所有帮助

In the server I have this 在服务器上我有这个

@Override
    public void run() {
        try {

            // prepare to receive data
            InputStream inputStream = mConnection.openInputStream();

            System.out.println("waiting for input");

            while (true) {
                int command = inputStream.read();

                StringWriter writer = new StringWriter();
                IOUtils.copy(inputStream, writer, Charsets.UTF_8);
                String theString = writer.toString();
                System.out.println(theString);

                if (command == EXIT_CMD)
                {   
                    System.out.println("finish process");
                    break;
                }

                processCommand(command);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

with this response... 有了这个回应...

BlueCove version 2.1.1-SNAPSHOT on winsock
04c6093b00001000800000805f9b34fb
waiting for connection...
waiting for connection...
waiting for input
23456789?

finish process

BUT in Android I send "123456789" with this code 但是在Android中,我用此代码发送“ 123456789”

public void write(String out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED) return;
            r = mConnectedThread;
        }
        // Perform the write unsynchronized
        r.write(out.getBytes());
    }

Few edits... 很少编辑...

I comment this line 我评论这条线

int command = inputStream.read();

and I get the "full" string this this code 我得到这个代码的“完整”字符串

BufferedReader bReader=new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = bReader.readLine()) != null) 
{
     System.out.println(line);
}

123456789 ÿ 123456789ÿ

I can eliminate the last char but is not the best... 我可以消除最后一个字符,但不是最好的字符。

In my android app I have this 在我的Android应用中,我有这个

mCommandService.write(editText.getText().toString().trim());
editText.getText().clear();
mCommandService.stop();

If I remove the last line, the ÿ disapear. 如果删除最后一行,则the消失。 So I guess that is the stop command. 所以我想这是停止命令。 Can I remove that or is native? 我可以删除它还是本地的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM