【问题标题】:bluetooth receive data and display it in textview蓝牙接收数据并在textview中显示
【发布时间】:2013-06-24 06:50:53
【问题描述】:

我正在做一个 android 应用程序,它可以从蓝牙接收数据并将其显示为波形。现在的问题是我想在文本视图中显示数据以确认数据是否正确,但我显示的内容无法识别(如@&zA ...)。任何人都可以帮助将数据转换为 8 位值?谢谢你 !

相关编码如下:

  Handler handler = new Handler() {
            @Override 
            public void handleMessage(Message msg) {
                if (msg.what==READ) { 
                    String str = (String)msg.obj;
                    textView1.setText(str); 
                }
                super.handleMessage(msg); 
                }
        };

    private class ConnectedThread extends Thread {
        private final InputStream mmInStream;
        public ConnectedThread(BluetoothSocket socket) {
            InputStream tmpIn = null;
            try {
                tmpIn = socket.getInputStream(); 
            }catch (IOException e) { }
            mmInStream = tmpIn; 
        }

        public void run() {  
            byte[] buffer = new byte[5];
            int bytes; // bytes returned from read() 

            // Keep listening to the InputStream until an exception occurs 
            while (true) {
                try {
                    // Read from the InputStream 
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity 
                    String str = new String(buffer); 
                    temp = byteToInt(buffer); //Convert byte to int
                    handler.obtainMessage(READ, bytes, -1, str).sendToTarget();

                }catch (Exception e) {
                    System.out.print("read error"); 
                    break;
                }

            }
        }
    }

【问题讨论】:

  • 查看 android 示例,他们有一个蓝牙聊天应用程序 /samples/android-17/BluetoothChat

标签: android bluetooth inputstream


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 2015-04-21
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多