如何在android中分离通过蓝牙接收的数据

时间:2013-03-05 16:41:50

标签: java android bluetooth msp430

我已经修改了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转换为字符串,然后使用tokenizer将其分离,然后将其转换回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

2 个答案:

答案 0 :(得分:1)

您要么有一条消息,其中包含多个按预定义顺序排列的值,要么您必须告诉接收方(app)接下来要发送的值,或多或少按照您的建议。

答案 1 :(得分:0)

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

因此,在将字符串与tokens.nextToken()分开之前,请检查字节字符串中有多少个标记:     tokens.countTokens