我有一辆arduino汽车,我希望通过蓝牙从我的Android手机控制。我已经为此创建了一个应用程序,但是当我尝试通过我的OutputStream.write函数发送多个连续消息时,它们将作为单个连续消息发送。例如:如果我调用connectedThread.write(" 200-100");然后在我的串行监视器上connectThread.write(" 300-150")显示为单个消息:" 200-100300-150"。我在其他StackOverflow帖子(OutputStream.write() never ends,Android outputStream.write send multiple messages)中看到了同样的问题,但他们没有一个适合我的答案,我尝试使用connectedThread.flush()并且它没有& #39; t帮助。这是我的代码(连接部分在另一个Activity中,它工作正常):
package com.clickau.cosmin.bluetootharduinocarcontroller;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Controls extends AppCompatActivity {
private SeekBar speed;
private SeekBar direction;
private BluetoothSocket btSocket = DataHolder.getData();
private ConnectedThread connectedThread;
private int speedValue;
private int directionValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controls);
speed = (SeekBar) findViewById(R.id.speed);
direction = (SeekBar) findViewById(R.id.direction);
speedValue = speed.getProgress();
directionValue = direction.getProgress();
speed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser)
{
speedValue = speed.getProgress()/5;
speedValue*=5;
directionValue = direction.getProgress()/5;
directionValue*=5;
/*StringBuilder str = new StringBuilder();
if (speedValue == 255)
str.append("STOP");
else
{
if (speedValue > 255)
{
str.append(speedValue - 255);
str.append('F');
}
else if (speedValue < 255)
{
str.append(255-speedValue);
str.append('S');
}
if (directionValue > 100)
{
str.append('D');
str.append(directionValue-100);
}
else if (directionValue < 100)
{
str.append('S');
str.append(100 - directionValue);
}
}
str.append('\n');
connectedThread.write(str.toString());*/
connectedThread.write(speedValue+ "+" + directionValue);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
speed.setProgress(255);
speedValue = speed.getProgress();
}
});
direction.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser)
{
speedValue = speed.getProgress()/5;
speedValue*=5;
directionValue = direction.getProgress()/5;
directionValue*=5;
/*StringBuilder str = new StringBuilder();
if (speedValue == 255)
str.append("STOP");
else
{
if (speedValue > 255)
{
str.append(speedValue - 255);
str.append('F');
}
else if (speedValue < 255)
{
str.append(255-speedValue);
str.append('S');
}
if (directionValue > 100)
{
str.append('D');
str.append(directionValue-100);
}
else if (directionValue < 100)
{
str.append('S');
str.append(100 - directionValue);
}
}
str.append('\n');
connectedThread.write(str.toString());*/
connectedThread.write(speedValue+ "+" + directionValue);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
direction.setProgress(100);
directionValue = direction.getProgress();
}
});
connectedThread = new ConnectedThread(btSocket);
connectedThread.start();
}
private class ConnectedThread extends Thread
{
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket)
{
InputStream tmpIn = null;
OutputStream tmpOut = null;
try{
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
}catch(IOException e) {
Log.e("ERROR", "Connected Thread");
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
/*public void run()
{
byte[] buffer = new byte[256];
int bytes;
while (true)
{
try{
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
bluetoothIn.obtainMessage(handlerState, bytes, -1 ,readMessage).sendToTarget();
} catch (IOException e)
{
break;
}
}
}*/
public void write(String input)
{
byte[] msgBuffer = input.getBytes();
try{
mmOutStream.write(msgBuffer);
mmOutStream.flush();
}catch(IOException e)
{
Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
我认为这可能是对蓝牙的优化,但它真的很烦人,因为我需要在移动控件时发送这些消息,而不是10秒后发送。有谁知道如何解决这个问题?