2013-03-05 122 views
1

我修改了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 
+0

欢迎SO。一般来说,在你的问题中包含代码片段是有帮助的,因为它有助于隔离问题,甚至可以让你的问题更容易理解。尝试通过使用代码块,重点说明重要笔记等来设置问题的格式。 – pestrella 2013-03-05 17:06:46

+0

谢谢您的提示。我添加了我认为与问题相关的部分代码。我试图保持它的合理数量,而不是增加太多。 – 2013-03-05 17:54:04

+0

@AlexTrebek请发布在崩溃时显示的异常。提示:我在这方面花费了无薪时间。;) – 2013-03-06 07:35:20

回答

1

你要么有一个消息在预先定义的顺序多个值,或者你必须告诉接收器(应用程序),它价值正在发送,或多或少如您所建议的那样。

+0

感谢您的快速回复。理想情况下,MSP430不会连续发送相同的代码。目前温度数据每30秒发送一次,但只有当温度数据发生变化时才会发送压力数据。我可以更改C代码以同时刷新所有内容,然后我可以预先定义顺序,但如果我可以在数据本身之前发送某种标识符以便应用程序知道它将非常有用在哪里放置它。 – 2013-03-05 17:56:15

+0

正如你所建议的,我试图发送一个单独的消息,以“:”分隔,然后使用标记器将其分割为冒号。但现在我崩溃了。我发布了上面的代码。任何想法可能导致崩溃?谢谢。 – 2013-03-05 22:07:45

0

崩溃(与您编辑的问题有关)可能是因为byteString不包含“:”。如果是这种情况,您的 String second = tokens.nextToken() 将完全抛出您发布的致命异常。

所以,你tokens.nextToken()分隔字符串之前,检查有多少令牌是与字节字符串: tokens.countTokens

+0

感谢您基本上重复我已经在评论中写过的内容。 ;) – 2013-03-11 10:08:26

+0

哦,我完全忘了看是否有评论,我只看了一个答案,下面的评论就是答案,不是问题,下次会多加注意...... – julthedroid 2013-03-11 10:18:00

相关问题