0

I am trying to covert the bytes into string what I am getting after reading from bluetooth device. Below is the code I am using, but it is not giving proper string. Output is like this

02-19 18:01:49.300: I/BluetoothReadService(17693): Message Read : ���8��1.04TTO��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

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, 0, buffer.length);

                    String readMessage = null;
                    byte[] getBytes = null;
                    try {
                        readMessage = BitConverter.toString(buffer);
                        getBytes = BitConverter.getBytes(readMessage);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    String s = new String(getBytes, "US-ASCII");
                    Log.i(TAG, "Message Read : "+readMessage +"ByteString"+s);
                    mHandler.obtainMessage(MainActivity.MESSAGE_READ).sendToTarget();
                } catch (IOException e)
                {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }

Please suggest what to do?

Divya Jain
  • 393
  • 1
  • 6
  • 22
Vid
  • 1,012
  • 1
  • 14
  • 29

6 Answers6

0

The readMessage String should be initialized in a different way. If you use readMessage = new String(buffer);, it should work.

Gerralt
  • 221
  • 2
  • 6
  • No, i have tried all this, but getting the same result. – Vid Feb 19 '15 at 13:10
  • Could you try `String readMessage = Arrays.toString(buffer);` ? It represents the number values of the bytes. It might just be that they're out of range of a normal String character. Maybe you can compare them that way? – Gerralt Feb 19 '15 at 13:39
0

By apache StringUtils you can use this method from document

 public static String newStringUsAscii(final byte[] bytes) {
    return new String(bytes, Charsets.US_ASCII);    
  }

And if still the problem persists than use below method for reading the String

public String readData() {

    String response = null;
    int bytesReceived=-1;
    byte[] readBuffer = new byte[1024];
    try{

        try{
            bytesReceived = inputStream.read(readBuffer);
            Logger.debugLog(MODULE ,"# Byte Receieved #" + bytesReceived);
        }catch(Exception e){
            Logger.errorLog(MODULE ,"Error in readData() , Reason:"+e.getMessage(),e);

        }
        if(bytesReceived > 0){          
            response = new String(readBuffer,0,bytesReceived);

            Logger.debugLog(MODULE + "Total Bytes Received: " + bytesReceived);
            Logger.debugLog(MODULE, "Total Time Taken : " + timetoread);
            Logger.debugLog(MODULE, "Length of data received : " + response.length());
            Logger.infoLog(MODULE, "Data Received : ####" + response + "####");
        }else{
            ////////// HERE MEANS TCP CONNECTION STATUS WILL CLOSE_WAIT
            ////////// SO RELEASING CONNECTION.
            Logger.infoLog(MODULE, " Releasing Resource. bytesReceived is <= 0 : Total Bytes Received :"+ bytesReceived );

                releaseResources();

            }
            Logger.infoLog(MODULE, " Resource has been released.");
        }
    }catch(Exception e){
        Logger.errorLog(MODULE, "In catch : Data Received : ####" + response + "####");
        Logger.errorLog(MODULE, "Exception in readdata() : " + e);
    }finally{
        readBuffer=null;
    }
    return response;
}
Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49
0

I think instead of

String s = new String(getBytes, "US-ASCII");

you should use

String s = new String(buffer, "US-ASCII");

and remove that part with BitConverter.

0

You seem to ignore the number of bytes actually read and create the new String out of the entire buffer of 1024 bytes.

Try this instead:

String readMessage = new String(buffer, 0, bytes, "US-ASCII");
diginoise
  • 7,352
  • 2
  • 31
  • 39
0

Read Android's official Bluetooth Chat example, in BluetoothChatFrangment.java, you see this:

// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);

in this example msg.arg1 is equal to bytes in your case. Also readBuf is byte[].

Kasra
  • 3,045
  • 3
  • 17
  • 34
0

This should do the job for you.

String s = new String(buffer, StandardCharsets.UTF_8)

Requires Api above KitKat.

Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
Gabriel Trifa
  • 173
  • 11