0

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

            }
        }
    }
4

1 に答える 1

1

次の URL をお試しください: http://developer.android.com/guide/topics/wireless/bluetooth.html http://developer.android.com/resources/samples/BluetoothChat/index.html

于 2013-06-24T07:09:30.703 に答える