Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bletest;
- import android.os.RemoteException;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Spinner;
- import android.widget.TextView;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.android.volley.toolbox.Volley;
- import org.altbeacon.beacon.Beacon;
- import org.altbeacon.beacon.BeaconConsumer;
- import org.altbeacon.beacon.BeaconManager;
- import org.altbeacon.beacon.RangeNotifier;
- import org.altbeacon.beacon.Region;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.UUID;
- import com.squareup.picasso.Picasso;
- public class page3Test extends AppCompatActivity implements BeaconConsumer { //implements Spinner.OnItemSelectedListener {
- protected static final String TAG = "RangingActivity";
- private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
- //Declaring an Spinner
- private Spinner spinner;
- //An ArrayList for Spinner Items
- private ArrayList<String> students;
- //JSON Array
- private JSONArray result;
- ArrayList<String> addArray = new ArrayList<String>();
- private Button scan;
- //TextViews to display details
- private TextView textViewName;
- private TextView textViewCourse;
- private TextView textViewSession;
- private TextView textViewUUID;
- private TextView textViewShopInfo;
- private ImageView ivBasicImage;
- String uuidChecker = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_page3_test);
- //Initializing the ArrayList
- students = new ArrayList<String>();
- ivBasicImage = (ImageView) findViewById(R.id.ivBasicImage2);
- //Initializing Spinner
- spinner = (Spinner) findViewById(R.id.spinner);
- //Adding an Item Selected Listener to our Spinner
- //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
- //spinner.setOnItemSelectedListener(page3Test.this);
- //Initializing TextViews
- textViewName = (TextView) findViewById(R.id.textViewName);
- textViewCourse = (TextView) findViewById(R.id.textViewCourse);
- textViewSession = (TextView) findViewById(R.id.textViewSession);
- textViewShopInfo = (TextView) findViewById(R.id.textViewShopInfo);
- //This method will fetch the data from the URL
- getData();
- }
- public class Config2 {
- public static final String DATA_URL2 = "http://192.168.1.111/retrieve2.php";
- //Tags used in the JSON String
- public static final String idInfo = "id";
- public static final String uuidInfo = "UUID";
- public static final String shopInfo = "shopInfo";
- public static final String shopItem = "shopItem";
- public static final String shopImage = "shopImage";
- public static final String allInfo = "result";
- }
- public void getData() {
- StringRequest stringRequest = new StringRequest(Config2.DATA_URL2,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- JSONObject j = null;
- try {
- //Parsing the fetched Json String to JSON Object
- j = new JSONObject(response);
- //Storing the Array of JSON String to our JSON Array
- result = j.getJSONArray(Config2.allInfo);
- //Calling method getStudents to get the students from the JSON Array
- getStudents(result);
- onScan();
- } catch (JSONException e) {
- Log.e("JSON Parser", "Error parsing data [" + e.getMessage() + "] " + response);
- e.printStackTrace();
- }
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- });
- //Creating a request queue
- RequestQueue requestQueue = Volley.newRequestQueue(this);
- //Adding request to the queue
- requestQueue.add(stringRequest);
- }
- private void getStudents(JSONArray j) {
- //Traversing through all the items in the json array
- for (int i = 0; i < j.length(); i++) {
- try {
- //Getting json object
- JSONObject json = j.getJSONObject(i);
- //Adding the name of the student to array list
- students.add(json.getString(page3Test.Config2.idInfo));
- Log.e("COUNTER NUMBER", String.valueOf(students));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- private String getUUID(int position) {
- String uuid = "";
- try {
- JSONObject json = result.getJSONObject(position);
- uuid = json.getString(page3Test.Config2.uuidInfo);
- uuidChecker = uuid;
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return uuid;
- }
- //Method to get student name of a particular position
- private String getName(int position) {
- String name = "";
- try {
- //Getting object of given index
- JSONObject json = result.getJSONObject(position);
- //Fetching name from that object
- name = json.getString(page3Test.Config2.idInfo);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- //Returning the name
- return name;
- }
- //Doing the same with this method as we did with getName()
- /* private String getCourse(int position){
- String course="";
- try {
- JSONObject json = result.getJSONObject(position);
- course = json.getString(Config2.shopInfo);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return course;
- }*/
- //Doing the same with this method as we did with getName()
- /*private String getShopItems(int position) {
- String session = "";
- try {
- JSONObject json = result.getJSONObject(position);
- session = json.getString(Config2.shopItem);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return session;
- }*/
- private String getShopInfo(int position) {
- String session = "";
- try {
- JSONObject json = result.getJSONObject(position);
- session = json.getString(Config2.shopInfo);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return session;
- }
- private String getShopPhoto(int position) {
- String session = "";
- try {
- JSONObject json = result.getJSONObject(position);
- session = json.getString(Config2.shopImage);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return session;
- }
- /*//this method will execute when we pic an item from the spinner
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- //Setting the values to textviews for a selected item
- textViewName.setText(getName(position));
- // textViewCourse.setText(getCourse(position));
- textViewSession.setText(getSession(position));
- textViewUUID.setText(getUUID(position));
- textViewShopInfo.setText(getShopInfo(position));
- //textViewShopInfo.setText(getShopPhoto(position));
- //String imgurl = getShopPhoto(position);
- Picasso.with(this).load(getShopPhoto(position)).into(ivBasicImage);
- }*/
- //When no item is selected this method would execute
- /* @Override
- public void onNothingSelected(AdapterView<?> parent) {
- textViewName.setText("");
- textViewCourse.setText("");
- textViewSession.setText("");
- textViewUUID.setText("");
- textViewShopInfo.setText("");
- }*/
- @Override
- protected void onDestroy() {
- super.onDestroy();
- }
- @Override
- protected void onPause() {
- super.onPause();
- beaconManager.unbind(this);
- }
- @Override
- protected void onResume() {
- super.onResume();
- beaconManager.bind(this);
- }
- @Override
- public void onBeaconServiceConnect() {
- RangeNotifier rangeNotifier = new RangeNotifier() {
- @Override
- public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
- if (beacons.size() > 0) {
- Log.d(TAG, "didRangeBeaconsInRegion called with beacon count: " + beacons.size());
- Beacon firstBeacon = beacons.iterator().next();
- String x = firstBeacon.getId1().toString();
- //logToDisplay("The first beacon " + firstBeacon.getId1() + " is about " + firstBeacon.getDistance() + " meters away.");
- double distance = firstBeacon.getDistance();
- int p = 0;
- while (p != 10) {
- addArray.add(x);
- if (addArray.contains(x)) {
- Log.e("DONE", addArray.toString());
- }
- p++;
- }
- String uuidChecker = "";
- Log.e("CHECK HERE FOR RESULT", x);
- Log.e("CHECK", String.valueOf(distance));
- for (int dCount = 0; dCount < 10; dCount++) {
- Log.e("PART2BEACON", x);
- Log.e("PART2dcount", String.valueOf(dCount));
- Log.e("PART2COUNTER", String.valueOf(distance));
- Log.e("PART2UUID", getUUID(dCount));
- String element = addArray.get(dCount);
- Log.e("CHECKER", element);
- if (element.equals(getUUID(dCount))) {
- if (element.equals(getUUID(dCount))) {
- //textViewName.setText(getShopInfo(0));
- textViewSession.setText(getUUID(dCount));
- textViewShopInfo.setText(getShopInfo(dCount));
- Picasso.with(page3Test.this).load(getShopPhoto(dCount)).resize(800, 600).into(ivBasicImage);
- } else {
- textViewSession.setText("NO RESULT");
- textViewShopInfo.setText("NO RESULT");
- String imageUri = "https://i.imgur.com/tGbaZCY.jpg";
- Picasso.with(page3Test.this).load(imageUri).into(ivBasicImage);
- }
- }
- }
- }
- }};
- try {
- beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
- beaconManager.addRangeNotifier(rangeNotifier);
- } catch (
- RemoteException e) {
- }
- }
- /*public void logToDisplay(final String line) {
- runOnUiThread(new Runnable() {
- public void run() {
- EditText editText = (EditText)page3Test.this.findViewById(R.id.rangingText);
- editText.append(line+"\n");
- }
- });
- }*/
- public void onScan() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement