1

I am using the Bluetooth Chat sample app from android website. I want to send a long message in a particular format (mentioned below in the code) and receive it on the other side and display on my phone (HTC Desire). I will parse this message further, extract few things and use it for my app.

I am not able to receive all the characters in the above message (few characters get randomly omitted). Is it because of a problem in the InputStream "read" method of the bluetooth socket or that the bluetooth SPP profile has bugs and problems in HTC Desire or any other reason? I have tried the InputStream to be a byte buffer, a char buffer and then create a string out of the buffer contents (using normal new String, StringBuilder and StringBuffer). But somehow I am not able to display the entire message in the required format on my phone.

Any help/suggestion is greatly appreciated.

Thank you for your time

Cheers, Madhu Nandan

/** * 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");
        char[] buffer = new char[1024];
        int bytes;  
        //Writer writer = new StringWriter();

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
             Reader reader = new BufferedReader(
                        new InputStreamReader(mmInStream, "UTF-8"));
                //int n;
                while ((bytes = reader.read(buffer)) != -1) 
                 //writer.write(buffer, 0, bytes);
                //String str = writer.toString();

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

case MESSAGE_READ:             
         char[] readBuf = (char[]) msg.obj;             
            // construct a string from the valid bytes in the buffer 
         //StringBuffer sb = new StringBuffer("");
         String Incoming = new String (readBuf, 0, msg.arg1);
         StringBuilder sb = new StringBuilder("");             
            String readMessage = sb.append(Incoming).toString();             
         //String readMessage = new String(sb.append(Incoming));
            mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);  
            mTitle.setText(readMessage);
            //String readMessage = new String(sb);
            //Make sure there is a received message. Extract it's name and value
            //int msgLen = readMessage.length();
            //if( msgLen!= 0)

            //Message format: <MSG><N>xxx<!N><V>yyy<!V><!MSG> (xxx-name of the  //message, yyy-value of the message)
            if (readMessage.matches("<MSG><N>.*<!N><V>.*<!V><!MSG>"))                
             extractData(readMessage); 
            //else mTitle.setText(R.string.floor_it); 
            sb.setLength(0);
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Madhu Nandan
  • 162
  • 7
  • 20

0 Answers0