我修改了BluetoothChat示例代码,以便连接到我已连接到TI MSP430开发板上的UART的通用蓝牙收发器。我建立了通信并可以发送和接收单个字符串并在TextView中显示值。下面是我用来发送压力,温度1和温度2的1-3位数值的C代码。它非常简单,我按照设计工作。如何分离正在通过蓝牙接收的数据android
for(int i = 0; i <= 2; i++) // send pressure value
{
UCA0TXBUF = pressureString[i];
while(!(IFG2 & UCA0TXIFG));
}
for(int i = 0; i <= 2; i++) // send temp1 value
{
UCA0TXBUF = tempOneString[i];
while(!(IFG2 & UCA0TXIFG));
}
for(int i = 0; i <= 2; i++) // send temp2 value
{
UCA0TXBUF = tempTwoString[i];
while(!(IFG2 & UCA0TXIFG));
}
现在我想数据的多条发送到机器人装置,并让它们根据为每个值的单独的TextView它们的数据类型显示。现在我正在测量两个温度传感器和一个压力传感器。我已将所有数据发送到Android设备,但没有任何问题,但所有值都只是在TextView中相互覆盖,以便只显示最后发送的字符串。
这是代码运行地连接到远程设备,而部分:
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
这是读取该消息并将其显示在所述的TextView的代码:
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mTextView.setText(readMessage); //added by AMJ in attempt to display variable in textview
break;
我可以似乎无法弄清楚如何编程android应用程序,以便能够区分字符串,以便当我收到Temp1字符串时它将转到Temp1TextView,并且Temp2字符串转到Temp2TextView等。我应该添加一个特殊的字符作为从MSP430发送的第一位,a nd参考这一点在Android确定它应该去哪里?只是一个想法。
任何帮助,非常感谢。
编辑:我想我可以尝试并将int转换为字符串,然后使用标记器分离它,然后将其转换回int。但是,当它通过蓝牙接收数据时,该应用程序现在正在崩溃。这是我用来转换它的代码。任何想法为什么它可能会崩溃?
bytes = mmInStream.read(buffer);
byteString = String.valueOf(bytes);
StringTokenizer tokens = new StringTokenizer(byteString, ":");
String first = tokens.nextToken(); // this will contain exhaust temp
String second = tokens.nextToken(); // this will contain damper position
separatebytes1 = Integer.valueOf(first);
separatebytes2 = Integer.valueOf(second);
// Read from the InputStream
// bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
// mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, separatebytes1, -1, buffer)
.sendToTarget();
这是从崩溃的logcat:
W/dalvikvm(24850): threadid=11: thread exiting with uncaught exception (group=0x
411ae300)
E/AndroidRuntime(24850): FATAL EXCEPTION: Thread-1286
E/AndroidRuntime(24850): java.util.NoSuchElementException
E/AndroidRuntime(24850): at java.util.StringTokenizer.nextToken(StringTok
enizer.java:208)
E/AndroidRuntime(24850): at com.example.android.BluetoothChat.BluetoothCh
atService$ConnectedThread.run(BluetoothChatService.java:411)
W/ActivityManager( 270): Force finishing activity com.example.android.Bluetoo
thChat/.BluetoothChat
欢迎SO。一般来说,在你的问题中包含代码片段是有帮助的,因为它有助于隔离问题,甚至可以让你的问题更容易理解。尝试通过使用代码块,重点说明重要笔记等来设置问题的格式。 – pestrella 2013-03-05 17:06:46
谢谢您的提示。我添加了我认为与问题相关的部分代码。我试图保持它的合理数量,而不是增加太多。 – 2013-03-05 17:54:04
@AlexTrebek请发布在崩溃时显示的异常。提示:我在这方面花费了无薪时间。;) – 2013-03-06 07:35:20