6

I am reading a constant stream of data coming from a device via Bluetooth. I'm wondering how I can convert this data to a string and print it out? The buffer will contain an ASCII string but when I run it it prints out integers, i would like to be able to see the string.

 while (true) {
                try {
                    //read the data from socket stream
                    if(mmInStream != null) {
                       int input = mmInStream.read(buffer);

                       System.out.println(input);
                    }
                    // Send the obtained bytes to the UI Activity
                } catch (IOException e) {
                    //an exception here marks connection loss
                    //send message to UI Activity
                    break;
                }
            }
1
  • You should print the contents of that buffer to begin with. Further you should have told us what is readed. What will the buffer contain? Commented May 25, 2018 at 18:00

1 Answer 1

5

you can try this.

public String isToString(InputStream is) {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(inputStream, "UTF-8");
        for (; ; ) {
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz < 0)
                break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.