I am using proximity template and How to use private static EstimoteLocation findNearestBeacon in MainActivity. I have tried many solutions from stack but these are not working. below is the code of my both classes.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final Map<Color, Integer> BACKGROUND_COLORS = new HashMap<>();
static {
BACKGROUND_COLORS.put(Color.ICY_MARSHMALLOW, android.graphics.Color.rgb(109, 170, 199));
BACKGROUND_COLORS.put(Color.BLUEBERRY_PIE, android.graphics.Color.rgb(98, 84, 158));
BACKGROUND_COLORS.put(Color.MINT_COCKTAIL, android.graphics.Color.rgb(155, 186, 160));
}
private static final int BACKGROUND_COLOR_NEUTRAL = android.graphics.Color.rgb(160, 169, 172);
private ProximityContentManager proximityContentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
proximityContentManager = new ProximityContentManager(this,
Arrays.asList(
"34de270e463c32833ff916b32abf8916",
"e3646402366ab11acf9bfef5e8b2bd03",
"f2b0f77e201e45a27d9d1f1192767635"),
new EstimoteCloudBeaconDetailsFactory());
proximityContentManager.setListener(new ProximityContentManager.Listener() {
@Override
public void onContentChanged(Object content) {
String text, text2, text3, text4;
Integer backgroundColor;
if (content != null) {
EstimoteCloudBeaconDetails beaconDetails = (EstimoteCloudBeaconDetails) content;
text = "You're in " + beaconDetails.getBeaconName() + "'s range!";
text2 = "Major is "+beaconDetails.getDetail();
text3 = "Minor is " +beaconDetails.getMinor();
text4 = "Unique Id is" + beaconDetails.getUuid();
backgroundColor = BACKGROUND_COLORS.get(beaconDetails.getBeaconColor());
} else {
text = "No beacons in range.";
text2 = "No major Found ";
text3 = "No minor Found";
text4 = "No uuid found";
backgroundColor = null;
}
((TextView)findViewById(R.id.textUuid)).setText(text4);
((TextView)findViewById(R.id.textView3)).setText(text3);
((TextView)findViewById(R.id.textView2)).setText(text2);
((TextView) findViewById(R.id.textView)).setText(text);
findViewById(R.id.relativeLayout).setBackgroundColor(
backgroundColor != null ? backgroundColor : BACKGROUND_COLOR_NEUTRAL);
}
});
}
@Override
protected void onResume() {
super.onResume();
if (!SystemRequirementsChecker.checkWithDefaultDialogs(this)) {
Log.e(TAG, "Can't scan for beacons, some pre-conditions were not met");
Log.e(TAG, "Read more about what's required at: http://estimote.github.io/Android-SDK/JavaDocs/com/estimote/sdk/SystemRequirementsChecker.html");
Log.e(TAG, "If this is fixable, you should see a popup on the app's screen right now, asking to enable what's necessary");
} else {
Log.d(TAG, "Starting ProximityContentManager content updates");
proximityContentManager.startContentUpdates();
}
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "Stopping ProximityContentManager content updates");
proximityContentManager.stopContentUpdates();
}
@Override
protected void onDestroy() {
super.onDestroy();
proximityContentManager.destroy();
}
}
public class NearestBeaconManager {
private static final String TAG = "NearestBeaconManager";
private List<String> deviceIDs;
private Listener listener;
private String currentlyNearestDeviceID;
private boolean firstEventSent = false;
private BeaconManager beaconManager;
public NearestBeaconManager(Context context, List<String> deviceIDs) {
this.deviceIDs = deviceIDs;
beaconManager = new BeaconManager(context);
beaconManager.setLocationListener(new BeaconManager.LocationListener() {
@Override
public void onLocationsFound(List<EstimoteLocation> locations) {
checkForNearestBeacon(locations);
}
});
}
public void setListener(Listener listener) {
this.listener = listener;
}
public interface Listener {
void onNearestBeaconChanged(String deviceID);
}
public void startNearestBeaconUpdates() {
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startLocationDiscovery();
}
});
}
public void stopNearestBeaconUpdates() {
beaconManager.stopLocationDiscovery();
}
public void destroy() {
beaconManager.disconnect();
}
private void checkForNearestBeacon(List<EstimoteLocation> allEstimoteLocations) {
List<EstimoteLocation> beaconsOfInterest = filterOutBeaconsByIDs(allEstimoteLocations, deviceIDs);
EstimoteLocation nearestLocation = findNearestBeacon(beaconsOfInterest);
if (nearestLocation != null) {
String nearestBeaconID = nearestLocation.id.toHexString();
if (!nearestBeaconID.equals(currentlyNearestDeviceID) || !firstEventSent) {
updateNearestBeacon(nearestBeaconID);
}
} else if (currentlyNearestDeviceID != null || !firstEventSent) {
updateNearestBeacon(null);
}
}
private void updateNearestBeacon(String beaconID) {
currentlyNearestDeviceID = beaconID;
firstEventSent = true;
if (listener != null) {
listener.onNearestBeaconChanged(beaconID);
}
}
private static List<EstimoteLocation> filterOutBeaconsByIDs(List<EstimoteLocation> estimoteLocations, List<String> deviceIDs) {
List<EstimoteLocation> filteredBeacons = new ArrayList<>();
for (EstimoteLocation estimoteLocation : estimoteLocations) {
String beaconID = estimoteLocation.id.toHexString();
if (deviceIDs.contains(beaconID)) {
filteredBeacons.add(estimoteLocation);
}
}
return filteredBeacons;
}
public static EstimoteLocation findNearestBeacon(List<EstimoteLocation> estimoteLocations) {
EstimoteLocation nearestLocation = null;
double nearestBeaconsDistance = -1;
for (EstimoteLocation estimoteLocation : estimoteLocations) {
double distance = computeAccuracy(estimoteLocation);
if (distance > -1 &&
(distance < nearestBeaconsDistance || nearestLocation == null)) {
nearestLocation = estimoteLocation;
nearestBeaconsDistance = distance;
}
}
Log.d(TAG, "Nearest beacon: " + nearestLocation + ", distance: " + nearestBeaconsDistance);
return nearestLocation;
}
}