//John Paul Mardelli
//Last updated November 2nd, 2013

package com.northstar.minimap.beacon;

import com.northstar.minimap.Position;
import android.bluetooth.BluetoothDevice;

import java.text.DecimalFormat;

public class StickNFindBluetoothBeacon extends BluetoothBeacon {

    public static final double PROPAGATION_CONSTANT = 1.1;
    public static final int RSSI_AT_ONE_METER = -75;

    public StickNFindBluetoothBeacon(BluetoothDevice device, String beaconID, Position location) {
        super(device, beaconID, location);
    }

    /**
     * Method to use StickNFind's API to
     * for bluetooth to get signal Strength.
     */
    @Override
    public int getSignalStrength() {
        return 0;
    }

    /**
     * Method to use our own algorithm to compute distance to a
     * bluetooth beacon
     */
    @Override
    public Double computeDistance() {
        return Math.pow(10, (RSSI_AT_ONE_METER - signalStrength) / (10.0 * PROPAGATION_CONSTANT));
    }


    public String getFormattedDistance() {
        return (new DecimalFormat("#.##").format(computeDistance()) + " m");
    }
}

@@ -201,16 +201,45 @@
 lFormat;
+%0Aimport java.util.LinkedList;
 %0A%0Apublic
@@ -409,16 +409,116 @@
 R = -75;
+%0A    public static final int SMOOTHING_RANGE = 5;%0A%0A    private LinkedList%3CDouble%3E previousDistances;
 %0A%0A    pu
@@ -652,16 +652,71 @@
 cation);
+%0A%0A        previousDistances = new LinkedList%3CDouble%3E();
 %0A    %7D%0A%0A
@@ -1061,95 +1061,437 @@
     
-return Math.pow(10, (RSSI_AT_ONE_METER - signalStrength) / (10.0 * PROPAGATION_CONSTANT
+double distance = rssiToDistance(signalStrength);%0A        double smoothedDistance = 0.0;%0A%0A        if (previousDistances.size() == SMOOTHING_RANGE) %7B%0A            previousDistances.removeFirst();%0A        %7D%0A%0A        previousDistances.addLast(distance);%0A%0A        for (double previousDistance: previousDistances) %7B%0A            smoothedDistance += previousDistance;%0A        %7D%0A%0A        return (smoothedDistance / previousDistances.size(
 ));%0A
@@ -1497,17 +1497,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -1622,11 +1622,153 @@
 );%0A    %7D
+%0A%0A    private double rssiToDistance(int rssi) %7B%0A        return Math.pow(10, (RSSI_AT_ONE_METER - rssi) / (10.0 * PROPAGATION_CONSTANT));%0A    %7D
 %0A%7D%0A

Apply smoothing.