1

I would like to get data from InputStream() as a String eg. Hi, Start, Stop, etc.

My Code fragment is

byte[] buffer = new byte[1024];
inputStream.read(buffer);

My data is Command Sent from Bluetooth. The above code fragment only get (eg. 2 if I sent Hi, 5 if sent Start), I would like to get back Normal String as the same from the Sender Side.

I found only converting way from String to InputStream. any suggestion , I would like to appreciate!

2
  • 2
    C#? Java? Other? Adding these "clues" will make the question better. Commented Jul 12, 2012 at 4:19
  • You need to know the encoding of what you are receiving before you can convert it to a stream. Commented Jul 12, 2012 at 5:42

2 Answers 2

1

finally, i can solve this! inputStream.read(buffer); only return the int num of how many bytes is in the buffer and the data from the socket is stored in buffer. So, from the buffer you can make String eg. String result = new String (buffer);

Sign up to request clarification or add additional context in comments.

Comments

1

The simplest way is via the Apache common-io library:

String input = IOUtils.toString(inputStream);

See the javadoc for this method for more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.