Skip to content

Instantly share code, notes, and snippets.

@geekykant
Created November 25, 2019 07:42
Show Gist options
  • Save geekykant/1260329bc4493ea2838b9985f210e464 to your computer and use it in GitHub Desktop.
Save geekykant/1260329bc4493ea2838b9985f210e464 to your computer and use it in GitHub Desktop.
Navigate.java
package com.diyandroid.beacon;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.diyandroid.beacon.estimote.BeaconID;
import com.diyandroid.beacon.estimote.EstimoteCloudBeaconDetails; import com.diyandroid.beacon.estimote.EstimoteCloudBeaconDetailsFactory; import com.diyandroid.beacon.estimote.ProximityContentManager;
import com.estimote.coresdk.common.requirements.SystemRequirementsChecker;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Navigate extends AppCompatActivity {
private static final String TAG = "Navigation";
int timeC;
private ProximityContentManager proximityContentManager;
HashMap<Integer, Integer> beacon_map = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigate);
timeC=0;
38
beacon_map.put(44798, 1);
beacon_map.put(15219, 2);
beacon_map.put(37342, 3);
beacon_map.put(37258, 4);
final EditText input_text = (EditText) findViewById(R.id.destination_input);
proximityContentManager = new ProximityContentManager(this, Arrays.asList(
TODO: replace with UUIDs, majors and minors of your own beacons new BeaconID("B9407F30-F5F8-466E-AFF9-25556B57FE6D", 44798,
63178),
new BeaconID("B9407F30-F5F8-466E-AFF9-25556B57FE6D", 15219,
40984),
new BeaconID("B9407F30-F5F8-466E-AFF9-25556B57FE6D", 37342,
62426),
new BeaconID("B9407F30-F5F8-466E-AFF9-25556B57FE6D", 37258,
40563)),
new EstimoteCloudBeaconDetailsFactory());
((Button) findViewById(R.id.go_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!TextUtils.isEmpty(input_text.getText())) {
int dest = Integer.parseInt(input_text.getText().toString()); getNavigationThing(dest);
}
}
});
}
private void getNavigationThing(final int dest) { proximityContentManager.setListener(new ProximityContentManager.Listener() {
@Override
public void onContentChanged(Object content) {
}
@Override
public void onDistanceChanged(ArrayList<EstimoteCloudBeaconDetails> beaconList) {
39
if(timeC!=0)
return;
EstimoteCloudBeaconDetails nearestBeacon;
if (beaconList.size() != 0) {
//TODO: Nearest algorithm here
Log.e(TAG, "Checking for the nearest beacon");
nearestBeacon = beaconList.get(0);
DjikstrasAlgorithm d=new DjikstrasAlgorithm();
//TODO: Add edittext
int[] solution = d.main(beacon_map.get(nearestBeacon.getHello_major()),
dest);
if (solution != null) {
((TextView) findViewById(R.id.d_val)).setText(solution[2] + "");
((TextView) findViewById(R.id.path_val)).setText((d.a)+" "); timeC++;
}
} else {
Log.e(TAG, "Only" + beaconList.size() + " beacons in range.");
}
}
});
}
@Override
protected void onResume() {
super.onResume();
timeC=0;
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");
40
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();
}
@Override
public boolean onNavigateUp() {
onBackPressed();
return super.onNavigateUp();
}
}
DijikstrasAlgorithm.java
package com.diyandroid.beacon;
import android.util.Log;
public class DjikstrasAlgorithm {
private static final int NO_PARENT = -1;
//public static int a = 0; ad-
public static String a = ""; //ad+
private static int[] djikstra(int[][] adjacencyMatrix, int startVertex, int dest) {
int nVertices = adjacencyMatrix[0].length;
41
int[] shortestDistances = new int[nVertices];
boolean[] added = new boolean[nVertices];
for (int vertexIndex = 0; vertexIndex < nVertices;
vertexIndex++) {
shortestDistances[vertexIndex] = Integer.MAX_VALUE;
added[vertexIndex] = false;
}
Distance of source vertex from itself is always 0 shortestDistances[startVertex] = 0;
int[] parents = new int[nVertices];
parents[startVertex] = NO_PARENT;
Find shortest path for all vertices for (int i = 1; i < nVertices; i++) {
int nearestVertex = -1;
int shortestDistance = Integer.MAX_VALUE; for (int vertexIndex = 0;
vertexIndex < nVertices; vertexIndex++) {
if (!added[vertexIndex] && shortestDistances[vertexIndex] <
shortestDistance) { nearestVertex = vertexIndex; shortestDistance = shortestDistances[vertexIndex];
}
}
Mark the picked vertex as processed added[nearestVertex] = true;
Update dist value of the adjacent vertices of the picked vertex. for (int vertexIndex = 0;
vertexIndex < nVertices; vertexIndex++) {
int edgeDistance = adjacencyMatrix[nearestVertex][vertexIndex];
if ((edgeDistance > 0) && (shortestDistance + edgeDistance) < (shortestDistances[vertexIndex])) {
parents[vertexIndex] = nearestVertex; shortestDistances[vertexIndex] = shortestDistance + edgeDistance;
}
}
42
}
return printSolution(startVertex, shortestDistances, parents, dest);
}
// A utility print function (distances & shortest paths)
private static int[] printSolution(int startVertex, int[] distances, int[] parents, int dest) { int nVertices = distances.length;
System.out.print("Vertex\t Distance\tPath");
int[] solution = null;
for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { if ((vertexIndex != startVertex) && (vertexIndex == dest)) {
Log.e("printSolution", "StartVertex: " + startVertex);
Log.e("printSolution", "vertexIndex: " + vertexIndex);
Log.e("printSolution", "vertexIndex: " + distances[vertexIndex]);
solution = new int[]{startVertex, vertexIndex, distances[vertexIndex]}; printPath(vertexIndex, parents);
}
}
return solution;
}
Function to print shortest path from source to currentVertex using parents array private static void printPath(int currentVertex,
int[] parents) {
a="";
Base case : Source node has been processed if (currentVertex == NO_PARENT) {
return;
}
printPath(parents[currentVertex], parents); a=a+currentVertex;
}
// Driver Code
public int[] main(int becon_number, int dest) {
int loc = becon_number;
43
int[][] adjacencyMatrix = {{0, 5, 0, 5, 0},
{5, 0, 8, 0, 0},
{0, 8, 0, 7, 0},
{0, 0, 7, 0, 0},
{0, 0, 0, 0, 0},
};
return djikstra(adjacencyMatrix, loc, dest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment