Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

java - OutputStream.write() never ends

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...