----------------------编辑--------------
A进展不大,但不是解决方案。如果我在要发送的命令之间插入以下代码,至少允许命令有时间在远程端处理(但这仍然不是正确的方法,正确的方法是等待在发送另一个命令之前用于响应“>”)...
android.os.SystemClock.sleep(150);
尽管在系统时钟休眠期间侦听器线程被阻塞,但来自调制解调器的输入直到代码序列被发送后才会附加到文本视图,这不太理想。不过,再次睡眠不是正确的方法,我需要找到更好的方法,所以在“>”结果从另一端的设备返回之前,我不会发送新命令。当我完成这段代码后,我将需要一种方法来处理输入,所以如果我考虑一下,睡眠真的没有进展。插入睡眠的示例:
sendData("enable");
android.os.SystemClock.sleep(150);
sendData("password");
android.os.SystemClock.sleep(150);
sendData("conf t");
android.os.SystemClock.sleep(150);
sendData("interface eth0");
android.os.SystemClock.sleep(150);
//etc...etc...etc...
----------------------下面的原帖------------------------- -
我正在使用来自 Matt Bell 博客的这段优雅编写的代码,位于此处: http ://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/
来源: http ://project-greengiant.googlecode.com/svn/trunk/Blog/Android%20Arduino%20Bluetooth
在不把代码切得太糟糕的情况下,我试图以一种优雅的方式将命令串行发送到连接的调制解调器,每次都等到收到完整的响应后再发送下一个命令。(我知道这不是 Android 的做事方式,我可以使用其他语言快速处理这个问题)。
到目前为止,这是我正在使用的内容(您会看到我还没有走得很远,事实上,这段代码运行得非常好,直到我需要等待第一个命令完成才能发送更多命令)。我尽可能多地省略了与这个问题无关的代码。提前致谢。
package Android.Arduino.Bluetooth;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class BluetoothTest extends Activity
{
TextView myLabel;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
int counter;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLabel = (TextView)findViewById(R.id.label);
try
{
findBT();
openBT();
sendData("enable");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
sendData("password");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
sendData("conf t");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
sendData("interface eth0");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
//etc...etc...etc...
}
catch (IOException ex) { }
}
}
void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
myLabel.append("Bluetooth Opened" + "\n");
}
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 62; //This is the ASCII code for a > character indicating all data received
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
myLabel.append(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void sendData(String msg0) throws IOException
{
msg0 += "\r";
mmOutputStream.write(msg0.getBytes());
myLabel.append("Data Sent" + "\n");
}
}