我已经修改了 BluetoothChat 示例代码以连接到我已连接到 TI MSP430 开发板上的 UART 的通用蓝牙收发器。我已经建立了通信,可以发送和接收单个字符串并在 TextView 中显示该值。下面是我用来发送压力、temp1 和 temp 2 的 1-3 位数值的 C 代码。它相当简单,而且我正在按设计工作。
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));
}
现在我想将多条数据发送到 android 设备,并根据它们的数据类型在每个值的单独 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 发送的第一位,并在 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