繁体   English   中英

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

[英]How to separate data being received over bluetooth in android

我修改了BluetoothChat示例代码以连接到通用蓝牙收发器,该收发器已连接到TI MSP430开发板上的UART。 我已经建立了通信,可以发送和接收单个字符串,并在TextView中显示该值。 下面是我用来发送1-3位压力,temp1和temp 2值的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();

这是崩溃的日志:

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

您可能只收到一条带有预定义顺序的多个值的消息,或者必须告诉接收方(应用程序)下一个要发送的值,或多或少按照您的建议。

发生崩溃(关于已编辑的问题)的原因很可能是因为byteString不包含“:”。 在这种情况下,您的String second = tokens.nextToken()将完全引发您发布的致命异常。

因此,在使用tokens.nextToken()分隔字符串之前,请使用以下tokens.nextToken()检查字节串中有多少令牌: tokens.countTokens

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM