0

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;
  }

}
Community
  • 1
  • 1
Daniel Nazareth
  • 530
  • 1
  • 6
  • 22
  • This is fundamentally not workable; the speed of light (in air) propagation time involved at the maximum plausible bluetooth range is smaller than anything in the radio is designed to measure or report to an app. It is possible that there will be a weak correlation where a more distant, and thus weaker signal requires more retries and thus more time to get through, but that would be very inconsistent. Time of flight could be a great idea with a radio designed for that purpose (we call those radars, basically), but not the radios found in today's phones and bluetooth gadgets. – Chris Stratton Mar 16 '14 at 20:24
  • This will be possible in the future. – danny117 Mar 16 '14 at 23:17
  • So, is there any way to do that? I've researched about RSSI, but doesn't look to solve my problem. – Daniel Nazareth Mar 17 '14 at 21:57

0 Answers0