OutputStream.write() 永远不会结束 [英] OutputStream.write() never ends

查看:250
本文介绍了OutputStream.write() 永远不会结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一辆带有蓝牙接口的汽车,我使用我的 android 应用程序连接到它.机器人被编程,当他得到一个数字 1-5 时,他就会采取行动.1 - 向前行驶2 - 向后行驶3 - 左转4 - 右转5 - 停止我的应用程序中有 5 个按钮.他们的活动是这样的

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

其中socket是持有BluetoothSocket并建立连接并具有写功能的类:

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

而且!当我连接并按下例如发送 2 的按钮时,机器人开始向后移动,但我没有收到来自线路的消息

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

所以看起来 write(byte[] buffer) 函数永远不会结束它的过程.按一个按钮后,当我尝试按另一个按钮时,它不起作用.就像 OutputStream.write() 仍然试图写一些东西.不知道为什么会这样,有什么解决办法吗?

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?

推荐答案

在像这样调用 write() 之后尝试使用 flush() 函数

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

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

这篇关于OutputStream.write() 永远不会结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆