Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. package com.example.bletest;
  2.  
  3. import android.os.RemoteException;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.ImageView;
  13. import android.widget.Spinner;
  14. import android.widget.TextView;
  15.  
  16. import com.android.volley.RequestQueue;
  17. import com.android.volley.Response;
  18. import com.android.volley.VolleyError;
  19. import com.android.volley.toolbox.StringRequest;
  20. import com.android.volley.toolbox.Volley;
  21.  
  22. import org.altbeacon.beacon.Beacon;
  23. import org.altbeacon.beacon.BeaconConsumer;
  24. import org.altbeacon.beacon.BeaconManager;
  25. import org.altbeacon.beacon.RangeNotifier;
  26. import org.altbeacon.beacon.Region;
  27. import org.json.JSONArray;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.UUID;
  34.  
  35. import com.squareup.picasso.Picasso;
  36.  
  37. public class page3Test extends AppCompatActivity implements BeaconConsumer { //implements Spinner.OnItemSelectedListener {
  38.  
  39. protected static final String TAG = "RangingActivity";
  40. private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
  41. //Declaring an Spinner
  42. private Spinner spinner;
  43.  
  44. //An ArrayList for Spinner Items
  45. private ArrayList<String> students;
  46.  
  47. //JSON Array
  48. private JSONArray result;
  49. ArrayList<String> addArray = new ArrayList<String>();
  50.  
  51. private Button scan;
  52. //TextViews to display details
  53. private TextView textViewName;
  54. private TextView textViewCourse;
  55. private TextView textViewSession;
  56. private TextView textViewUUID;
  57. private TextView textViewShopInfo;
  58. private ImageView ivBasicImage;
  59.  
  60. String uuidChecker = "";
  61.  
  62. @Override
  63. protected void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.activity_page3_test);
  66.  
  67. //Initializing the ArrayList
  68. students = new ArrayList<String>();
  69.  
  70.  
  71. ivBasicImage = (ImageView) findViewById(R.id.ivBasicImage2);
  72. //Initializing Spinner
  73. spinner = (Spinner) findViewById(R.id.spinner);
  74.  
  75. //Adding an Item Selected Listener to our Spinner
  76. //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
  77. //spinner.setOnItemSelectedListener(page3Test.this);
  78.  
  79. //Initializing TextViews
  80. textViewName = (TextView) findViewById(R.id.textViewName);
  81. textViewCourse = (TextView) findViewById(R.id.textViewCourse);
  82. textViewSession = (TextView) findViewById(R.id.textViewSession);
  83. textViewShopInfo = (TextView) findViewById(R.id.textViewShopInfo);
  84.  
  85.  
  86. //This method will fetch the data from the URL
  87. getData();
  88.  
  89. }
  90.  
  91. public class Config2 {
  92.  
  93. public static final String DATA_URL2 = "http://192.168.1.111/retrieve2.php";
  94.  
  95. //Tags used in the JSON String
  96. public static final String idInfo = "id";
  97. public static final String uuidInfo = "UUID";
  98. public static final String shopInfo = "shopInfo";
  99. public static final String shopItem = "shopItem";
  100. public static final String shopImage = "shopImage";
  101.  
  102. public static final String allInfo = "result";
  103. }
  104.  
  105.  
  106. public void getData() {
  107.  
  108. StringRequest stringRequest = new StringRequest(Config2.DATA_URL2,
  109.  
  110. new Response.Listener<String>() {
  111. @Override
  112. public void onResponse(String response) {
  113. JSONObject j = null;
  114. try {
  115. //Parsing the fetched Json String to JSON Object
  116. j = new JSONObject(response);
  117.  
  118. //Storing the Array of JSON String to our JSON Array
  119. result = j.getJSONArray(Config2.allInfo);
  120.  
  121. //Calling method getStudents to get the students from the JSON Array
  122. getStudents(result);
  123. onScan();
  124. } catch (JSONException e) {
  125. Log.e("JSON Parser", "Error parsing data [" + e.getMessage() + "] " + response);
  126.  
  127. e.printStackTrace();
  128. }
  129. }
  130. },
  131. new Response.ErrorListener() {
  132. @Override
  133. public void onErrorResponse(VolleyError error) {
  134.  
  135. }
  136. });
  137.  
  138. //Creating a request queue
  139. RequestQueue requestQueue = Volley.newRequestQueue(this);
  140.  
  141. //Adding request to the queue
  142. requestQueue.add(stringRequest);
  143. }
  144.  
  145. private void getStudents(JSONArray j) {
  146. //Traversing through all the items in the json array
  147. for (int i = 0; i < j.length(); i++) {
  148. try {
  149. //Getting json object
  150. JSONObject json = j.getJSONObject(i);
  151.  
  152. //Adding the name of the student to array list
  153. students.add(json.getString(page3Test.Config2.idInfo));
  154.  
  155. Log.e("COUNTER NUMBER", String.valueOf(students));
  156. } catch (JSONException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160.  
  161.  
  162. }
  163.  
  164. private String getUUID(int position) {
  165. String uuid = "";
  166. try {
  167. JSONObject json = result.getJSONObject(position);
  168. uuid = json.getString(page3Test.Config2.uuidInfo);
  169.  
  170. uuidChecker = uuid;
  171. } catch (JSONException e) {
  172. e.printStackTrace();
  173. }
  174. return uuid;
  175. }
  176.  
  177. //Method to get student name of a particular position
  178. private String getName(int position) {
  179. String name = "";
  180. try {
  181. //Getting object of given index
  182. JSONObject json = result.getJSONObject(position);
  183.  
  184. //Fetching name from that object
  185. name = json.getString(page3Test.Config2.idInfo);
  186. } catch (JSONException e) {
  187. e.printStackTrace();
  188. }
  189. //Returning the name
  190. return name;
  191. }
  192.  
  193. //Doing the same with this method as we did with getName()
  194. /* private String getCourse(int position){
  195. String course="";
  196. try {
  197. JSONObject json = result.getJSONObject(position);
  198. course = json.getString(Config2.shopInfo);
  199. } catch (JSONException e) {
  200. e.printStackTrace();
  201. }
  202. return course;
  203. }*/
  204.  
  205. //Doing the same with this method as we did with getName()
  206. /*private String getShopItems(int position) {
  207. String session = "";
  208. try {
  209. JSONObject json = result.getJSONObject(position);
  210. session = json.getString(Config2.shopItem);
  211. } catch (JSONException e) {
  212. e.printStackTrace();
  213. }
  214. return session;
  215. }*/
  216.  
  217.  
  218. private String getShopInfo(int position) {
  219. String session = "";
  220. try {
  221. JSONObject json = result.getJSONObject(position);
  222. session = json.getString(Config2.shopInfo);
  223. } catch (JSONException e) {
  224. e.printStackTrace();
  225. }
  226. return session;
  227. }
  228.  
  229. private String getShopPhoto(int position) {
  230. String session = "";
  231. try {
  232. JSONObject json = result.getJSONObject(position);
  233. session = json.getString(Config2.shopImage);
  234. } catch (JSONException e) {
  235. e.printStackTrace();
  236. }
  237.  
  238. return session;
  239.  
  240. }
  241.  
  242.  
  243.  
  244.  
  245.  
  246. /*//this method will execute when we pic an item from the spinner
  247. @Override
  248. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  249. //Setting the values to textviews for a selected item
  250. textViewName.setText(getName(position));
  251. // textViewCourse.setText(getCourse(position));
  252. textViewSession.setText(getSession(position));
  253. textViewUUID.setText(getUUID(position));
  254. textViewShopInfo.setText(getShopInfo(position));
  255. //textViewShopInfo.setText(getShopPhoto(position));
  256. //String imgurl = getShopPhoto(position);
  257. Picasso.with(this).load(getShopPhoto(position)).into(ivBasicImage);
  258.  
  259. }*/
  260.  
  261. //When no item is selected this method would execute
  262. /* @Override
  263. public void onNothingSelected(AdapterView<?> parent) {
  264. textViewName.setText("");
  265. textViewCourse.setText("");
  266. textViewSession.setText("");
  267. textViewUUID.setText("");
  268. textViewShopInfo.setText("");
  269.  
  270. }*/
  271.  
  272. @Override
  273. protected void onDestroy() {
  274. super.onDestroy();
  275. }
  276.  
  277. @Override
  278. protected void onPause() {
  279. super.onPause();
  280. beaconManager.unbind(this);
  281. }
  282.  
  283. @Override
  284. protected void onResume() {
  285. super.onResume();
  286. beaconManager.bind(this);
  287. }
  288.  
  289. @Override
  290. public void onBeaconServiceConnect() {
  291.  
  292. RangeNotifier rangeNotifier = new RangeNotifier() {
  293. @Override
  294. public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
  295. if (beacons.size() > 0) {
  296. Log.d(TAG, "didRangeBeaconsInRegion called with beacon count: " + beacons.size());
  297. Beacon firstBeacon = beacons.iterator().next();
  298. String x = firstBeacon.getId1().toString();
  299. //logToDisplay("The first beacon " + firstBeacon.getId1() + " is about " + firstBeacon.getDistance() + " meters away.");
  300.  
  301.  
  302. double distance = firstBeacon.getDistance();
  303.  
  304. int p = 0;
  305.  
  306. while (p != 10) {
  307. addArray.add(x);
  308. if (addArray.contains(x)) {
  309. Log.e("DONE", addArray.toString());
  310. }
  311. p++;
  312. }
  313.  
  314. String uuidChecker = "";
  315.  
  316.  
  317. Log.e("CHECK HERE FOR RESULT", x);
  318. Log.e("CHECK", String.valueOf(distance));
  319.  
  320. for (int dCount = 0; dCount < 10; dCount++) {
  321. Log.e("PART2BEACON", x);
  322. Log.e("PART2dcount", String.valueOf(dCount));
  323. Log.e("PART2COUNTER", String.valueOf(distance));
  324. Log.e("PART2UUID", getUUID(dCount));
  325.  
  326. String element = addArray.get(dCount);
  327. Log.e("CHECKER", element);
  328.  
  329. if (element.equals(getUUID(dCount))) {
  330.  
  331. if (element.equals(getUUID(dCount))) {
  332.  
  333. //textViewName.setText(getShopInfo(0));
  334. textViewSession.setText(getUUID(dCount));
  335. textViewShopInfo.setText(getShopInfo(dCount));
  336. Picasso.with(page3Test.this).load(getShopPhoto(dCount)).resize(800, 600).into(ivBasicImage);
  337.  
  338. } else {
  339. textViewSession.setText("NO RESULT");
  340. textViewShopInfo.setText("NO RESULT");
  341. String imageUri = "https://i.imgur.com/tGbaZCY.jpg";
  342. Picasso.with(page3Test.this).load(imageUri).into(ivBasicImage);
  343.  
  344. }
  345.  
  346.  
  347. }
  348. }
  349.  
  350. }
  351.  
  352. }};
  353. try {
  354. beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
  355. beaconManager.addRangeNotifier(rangeNotifier);
  356.  
  357.  
  358. } catch (
  359. RemoteException e) {
  360. }
  361.  
  362.  
  363. }
  364.  
  365. /*public void logToDisplay(final String line) {
  366. runOnUiThread(new Runnable() {
  367. public void run() {
  368. EditText editText = (EditText)page3Test.this.findViewById(R.id.rangingText);
  369. editText.append(line+"\n");
  370.  
  371. }
  372. });
  373. }*/
  374.  
  375. public void onScan() {
  376.  
  377.  
  378. }
  379.  
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement