I am currently sending data from my android to my robot like this:
Lets say I am sending a 10 and a 33...
//sending from android
- Code: Select all
public void send(int command, int value) {
if (Dos == null) {
return;
}
try {
Dos.writeInt(command);
Dos.writeInt(value);
Dos.flush();
} catch (IOException ioe) {
}
}
I then read the data on the robot like this:
//reading on robot
- Code: Select all
do {
// This does give me 10 and 33, but not all of the time.
int command = _dis.readInt();
int value = _dis.readInt();
Control(command, value);
Delay.msDelay(1);
} while (_dis.readInt() != 9999);
The problem is, sometimes the data can get a bit mixed up. It would be better if I could send over an array of two int's. EG:
- Code: Select all
int[] myArray = new int[2];
command = myArray[0];
value = myArray[1];
I can only see a byte array method but am not sure how to use it.
I've tried:
//sending from Anrdoid
- Code: Select all
public void send(int command, int value) {
if (Dos == null) {
return;
}
try {
byte[] buffer = new byte[2];
buffer[0] = (byte) command;
buffer[1] = (byte) value;
Dos.write(buffer);
Dos.flush();
} catch (IOException ioe) {
}
}
//reading on robot
- Code: Select all
byte[] buffer = new byte[2];
//DataInputStream
_dis.read(buffer);
//Here I would expect to see 10 and 33 at index 0 and 1, respectively.
int command = (int) buffer[0];
int value = (int) buffer[1];
but I'm not getting the expected results. Could someone please try explain to me how to read the byte array or perhaps point me in the right direction so that I may read up on it?
Thanks.
