简体   繁体   中英

bluetooth receive data and display it in textview

i'm doing an android app which can receive data from bluetooth and display it as a waveform. now the problem is i want to display the data in a textview to confirm the data is correct or not, but what i displayed is unrecognised(like @&zA...). anyone can help to convert the data to 8 bits value? thank you !

the related coding is shown below:

  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;
                }

            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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