Skip to content

Instantly share code, notes, and snippets.

@xlbruce
Created January 14, 2017 19:42
Show Gist options
  • Save xlbruce/5978adc25c0c57980e6091a6d37c9c0c to your computer and use it in GitHub Desktop.
Save xlbruce/5978adc25c0c57980e6091a6d37c9c0c to your computer and use it in GitHub Desktop.
This snippet reads a message from a bluetooth device and converts it content into a String object
public void run() {
StringBuilder sb = new StringBuilder();
while (true) {
try {
byte[] buffer = new byte[1024];
int bytes;
int bytesRead = -1;
do {
bytes = mmInStream.read(buffer, bytesRead+1, 1);
bytesRead+=bytes;
} while(buffer[bytesRead] != '\n');
Log.i("Log info:", new String( Arrays.copyOfRange(buffer, 0, bytesRead-1)));
sb = new StringBuilder();
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment