I am wanting to read data via bluetooth to the lego mindstorm NXT brick. The current problem I have is that the DataInputStream.read(byte[]) returns an int.
How do I read an ASCII string through bluetooth as a byte array?
Moderators: roger, 99jonathan, imaqine
void sendAscii(String s, DataOutputStream dos) throws IOException {
byte[] b = new byte[s.length()];
for(int i =0;i<s.length();i++) {
b[i] = (byte) s.charAt(i);
}
dos.writeInt(s.length];
dos.write(b);
dos.flush();
}
String readAscii(DataInputStream dis) throws IOException {
int sl = dis.readInt();
byte[] b = new byte[sl];
StringBuffer sb = new StringBuffer();
int l = dis.read(b);
for(int i=0;i<l;i++) {
sb.append((char) b[i]);
}
return sb.toString();
}
Users browsing this forum: No registered users and 1 guest