----------------------編輯------------------------ -
有一點進展,但不是解決方案。如果我在要發送的命令之間插入以下代碼,至少允許命令有時間在遠程端進行處理(但這仍然不是正確的方法,正確的方法是等待在發送另一個命令之前響應「>」)...藍牙Android SPP,發送串口命令到設備?
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...
---------------------- original post below ----------- ---------------
我與馬特·貝爾的博客這個文筆優美的代碼,設在這裏的工作:設在這裏 http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/
來源: http://project-greengiant.googlecode.com/svn/trunk/Blog/Android%20Arduino%20Bluetooth
不切斷c我試圖以一種優雅地將命令串行地發送到所連接的調制解調器的方式,每次等待直到在發送下一個命令之前接收到完整的響應爲止。 (我知道這不是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");
}
}
好吧,我在兩個命令之間加入了一個暫停,所以至少這些命令有時間在遠端執行。這仍然不是正確的方法,因爲我不應該直接從遠程設備發送命令或解釋/處理輸入,直到我收到「完成處理」字符「>」。 – user1324818 2012-04-12 16:36:07
好的。我闖入了這條路,並使其工作。這可能不對,但我會在有機會時發佈代碼。它的工作原理,但我需要添加更多的錯誤處理! – user1324818 2012-04-13 01:46:56
如果你寫了一些進展或最終解決方案,我會非常感激。在此先感謝..乾杯 – Ewoks 2012-07-06 12:00:38