I'm trying to get the distance between an Android device and a Arduino. To do this, I thought I'd use the formula of calculating networks latency, as seen in this link:
How to calculate packet time from latency and bandwidth
where the propagation time would be given to obtaining the time I spent to send a signal to the Arduino and get the answer divided by 2 (one-way time care). However, the time I have got varies even when not moving the Android device, and even increasing the distance, remains in the same range, between 1 and 11 milliseconds. My idea is wrong or the way I am doing it? I can even make these deductions have I done? Here is my code:
My button click
public void onLedOnClick(View v)
{
long avg = 0;
for(int i = 1; i < 31; i++){
long t1 = System.currentTimeMillis();
sendData("1");
beginListenForData();
long t2 = System.currentTimeMillis();
avg = (avg + (t2-t1)) / i;
txtAnswerTime.setText("Answer time:" + String.valueOf((t2-t1)) + "ms");
}
sendData("3");
}
my sendData method:
public void sendData(String data){
if (btSocket != null) {
try {
// allows the output data from a socket
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error: "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
String mensagem = data;
byte[] msgBuffer = mensagem.getBytes();
try {
// sends content by bluetooth
outStream.write(msgBuffer);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error: "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Bluetooth is not connected", Toast.LENGTH_SHORT).show();
}
}
My getData method:
public void beginListenForData()
{
if (btSocket != null) {
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
try {
mmInputStream = btSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
txtBytesSize.setText("Received data: "+data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
} else {
Toast.makeText(getApplicationContext(),
"Bluetooth is not connected", Toast.LENGTH_SHORT).show();
}
}
My Arduino code:
const int ledPin = 7;
byte serialA;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
if (Serial.available() > 0) {serialA = Serial.read();Serial.println(serialA);}
switch (serialA) {
case 49://49 is the bytes of "1"
digitalWrite(ledPin, HIGH);
Serial.println(serialA);
break;
case 50: //50 is the bytes of "2"
digitalWrite(ledPin, LOW);
Serial.println(serialA);
break;
break;
}
}