Distance calculation from RSSI BLE android

Ajay Pandya picture Ajay Pandya Β· Apr 4, 2016 Β· Viewed 15.8k times Β· Source

I know there is lot of question on stackoverflow related to my question, but I would like to know if there is any way to get the exact distance from RSSI.

I have followed this link and some other git library methods for distance calculation as well as this tutorial. But I can't get the correct solution.

This is what I'm using to measure the distance:

protected double calculateDistance(float txPower, double rssi) {

    if (rssi == 0) {
        return -1.0; // if we cannot determine distance, return -1.
    }

    double ratio = rssi * 1.0 / txPower;

    if (ratio < 1.0) {
        return Math.pow(ratio, 10);
    } else {
        double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
        return accuracy;
    }
}

When I call this method I pass the standard and rssi what i'm get from my mLeScanCallBack()

private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        BluetoothDeviceModel bluetoothDeviceModel = new BluetoothDeviceModel(device, rssi);

                        mLeDeviceListAdapter.addDevice(bluetoothDeviceModel, rssi, bluetoothDeviceModel.device.getAddress());
                        mLeDeviceListAdapter.notifyDataSetChanged();

                        if (mapBluetooth != null) {
                            mapBluetooth.put(bluetoothDeviceModel.getDevice().getAddress(), bluetoothDeviceModel);
                            mLeDeviceListAdapter.refresh(mapBluetooth);
                        }
                    }
                });
            }
        };

What Problem i'm facing?

There is nothing wrong with the code above. It gives me the distance but i'm not satisfied with that because it's not the correct distance. So can anyone tell me if it is possible to get the exact distance with the above method or if there is any other way?

Answer

Nishant Thapliyal picture Nishant Thapliyal Β· Jun 9, 2016

I am also working on the same thing. With this method you are able to calculate the distance but that distance is changing frequently. This is because RSSI value is also changing frequently.

What you need to do is to smooth out your result and for that you have to apply Kalman Filter or (linear quadratic estimation). If you want to stick with kalman filter then start from here. Beacon Tracking

Still I am looking for better implementation of Kalman Filter for my project. Alternatively, you can smooth up your RSSI value

π‘…π‘†π‘†πΌπ‘ π‘šπ‘œπ‘œπ‘‘h = 𝛼 βˆ— 𝑅𝑆𝑆𝐼𝑛 + (1 βˆ’ 𝛼) βˆ— π‘…π‘†π‘†πΌπ‘›βˆ’1

𝑅𝑆𝑆𝐼𝑛 is the recent 𝑅𝑆𝑆𝐼 value and π‘…π‘†π‘†πΌπ‘›βˆ’1 is the mean 𝑅𝑆𝑆𝐼 value till previous 𝑅𝑆𝑆𝐼.

𝛼 varies from 0 to 1 [consider 𝛼=0.75]

Source :- RSSI Smoothing