OutputStream.write() never ends

人走茶凉 提交于 2019-12-13 08:02:08

问题


I have a car with a bluetooth interface that I connect to using my android app. Robot is programmed so that when he gets a digit 1-5, he makes an action. 1 - drive forward 2 - drive backward 3 - turn left 4 - turn right 5 - stop I have 5 buttons in my app. Their events' look like this

public void button1(View view){
socket.write("1");
}

where socket is the class that holds BluetoothSocket and makes a connection and has write function:

public void write(String signal)
{
try
{
OutputStream.write(signal.getBytes());
Log.d("#Signal", " connected");
} catch (IOException e)
{
}
}

AND! When I connect, and press for example button that sends 2, robot starts moving backward but I don't get message from line

Log.d("#Signal", " connected");

So it looks like write(byte[] buffer) function never ends it's procedure. After pressing one button, when I try to press another one, it doesn't work. Like OutputStream.write() still tries to write something. I don't know why this happens, any solutions?


回答1:


Try using flush() function after you call write() like this

OutputStream.write(signal.getBytes());OutputStream.flush();


来源:https://stackoverflow.com/questions/35086989/outputstream-write-never-ends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!