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

标签 java android bluetooth msp430

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

最佳答案

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

关于java - 如何在android中分离通过蓝牙接收的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229362/

相关文章:

java - 如何处理 ImageView 拖放中的放置事件?

ios - 核心蓝牙 setNotifyValue 未将 CBCharacteristics 通知属性设置为 true

Android, BluetoothSocket - 我如何判断连接是否已经存在或者我是否必须调用连接?

java - Android fatal error : Async Task

java - 如何从命令行运行 json-schema-validator

c# - 使用内联对象方法调用与声明新变量

java - AKKA 路由器使用一致性哈希作为路由,但需要将消息传递给特定的参与者

Android:更改 ListView 内的 TextView 的文本?

java - Android 中来自 java.lang.management 的 ManagementFactory

ios - 使用 CoreBluetooth 在 iOS 上更改间隔时间读取信标 RSSI