Reading corrupted bluetooth data in Android

Put your problem here if it does not fit any of the other categories.

Reading corrupted bluetooth data in Android

Postby Christo » Tue Sep 20, 2011 8:46 am

Hi, I used the bluetoothChat program from Android Development website to connect with a bluetooth device. I sent very little data to the device, but want to recieve a lot of data. Everything works great, but about half the data that I recieve are corrupted. Half the characters I get are funny questionmarks. I debuged the program and saw that the question marks are negative ascii digits. So do I read the data wrong or what is it?

This is the code that I use to read the data:

Code: Select all
public void run() {
            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;
                }
            }


This is the code that I use to display the data

Code: Select all
    // The Handler that gets information back from the BluetoothChatService
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
           Log.e(TAG, "# mHandler   handleMessage");
           Log.i(TAG, "DEBUG (mHandler: " + msg + " * " + msg.what);
            switch (msg.what) {
            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
               
                Log.e(TAG, "readBuf:" + readBuf);
               
                // construct a string from the valid bytes in the buffer
                String str = EncodingUtils.getAsciiString(readBuf, 0, msg.arg1);
                mConversationArrayAdapter.add(mConnectedDeviceName+":  " + str);
                break;
            }
        }
    };


Thanks
Christo
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Sep 20, 2011 8:13 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 20 guests