[英]Android & Bluetooth & Arduino
我試圖在從傳輸的arduino類型的設備接收到的Android手機(目標4.3)上顯示傳感器數據。 傳輸通過藍牙進行。 我能夠連接到arduino類型的設備,甚至可以共享數據,但是由於某些原因,我遇到了同步問題。
現在設置arduino的方式,成功連接后,它等待從我的手機接收一個字節(無符號字節值255),當它收到此字節時,它會通過發送一個包含三個信息的數據包(3個字節)來做出響應不同的傳感器,即
packet:
byte 1: temperature data
byte 2: cadence data
byte 3: speed data
我要做的就是重復顯示此數據(實時更新),直到用戶終止android手機上的連接。
這是我的代碼,我覺得自己在邏輯中的某個地方犯了一個小錯誤。
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// Do Something;
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getActivity(),"CONNECTED",Toast.LENGTH_SHORT).show();
/*
* Could send test string here
*/
/*
* String connect_string = "test";
* connectedThread.write(connect_string.getBytes());
*/
connectedThread.start();
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
int tempInt = byteToInt(readBuf[0]);
int cadenceInt = byteToInt(readBuf[1]);
int speedInt = byteToInt(readBuf[2]);
EditText temperatureData = (EditText)getActivity().findViewById(R.id.temperatureData);
temperatureData.setText(Integer.toString(tempInt));
EditText cadenceData = (EditText)getActivity().findViewById(R.id.cadence);
cadenceData.setText(Integer.toString(cadenceInt));
EditText speedData = (EditText)getActivity().findViewById(R.id.speed_data);
speedData.setText(Integer.toString(speedInt));
}
}
};
public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
/*
* Use a temporary object that is later assigned to mmSocket,
* because mmSocket is final
*/
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
Toast.makeText(getActivity(), "Connecting to device failed!", Toast.LENGTH_LONG).show();
}
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte[3];
byte maxByte = (byte) 1;
mmOutStream.write(255);
bytes = mmInStream.read(buffer,0,buffer.length);
// Send the obtained bytes to the message handler
mHandler.obtainMessage(MESSAGE_READ,buffer).sendToTarget();
}
catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
}
public static int byteToInt(byte b){
int value;
value = b & 0xFF;
return value;
}
正在顯示我收到的數據,但通常最終會出錯,這主要是因為字節數組序列已關閉,導致顯示了錯誤的值。 我一直試圖弄清楚這一點,任何輸入都會有所幫助。
檢查是否可以將消息發送給處理程序克隆的數組。 它應該類似於“ buffer.clone”或“ buffer.clone()”,而不是簡單的“ buffer”。 如果是這樣,則意味着將未克隆的緩沖區復制為對處理程序的引用。 在處理程序執行其工作時,連接的線程可能會重新定義該數組並為其分配不同的值。 要對此進行測試,您還可以定義緩沖區ad Byte []而不是byte []。 這樣,我在應用程序中解決了類似的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.