Advertisement
Guest User

java

a guest
Jan 15th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.99 KB | None | 0 0
  1. package com.harrysoft.androidbluetoothserial.demoapp;
  2.  
  3. import static android.content.ContentValues.TAG;
  4.  
  5. import androidx.annotation.NonNull;
  6. import androidx.annotation.RequiresApi;
  7. import androidx.appcompat.app.AppCompatActivity;
  8.  
  9. import android.content.Intent;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.Looper;
  14. import android.util.ArrayMap;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.CompoundButton;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21. import android.widget.ToggleButton;
  22.  
  23. import com.google.android.gms.tasks.OnFailureListener;
  24. import com.google.android.gms.tasks.OnSuccessListener;
  25. import com.google.firebase.firestore.FirebaseFirestore;
  26. import com.harrysoft.androidbluetoothserial.BluetoothManager;
  27. import com.harrysoft.androidbluetoothserial.BluetoothSerialDevice;
  28. import com.harrysoft.androidbluetoothserial.SimpleBluetoothDeviceInterface;
  29.  
  30. import java.io.File;
  31. import java.io.FileWriter;
  32. import java.io.IOException;
  33. import java.text.SimpleDateFormat;
  34. import java.util.ArrayList;
  35. import java.util.Arrays;
  36. import java.util.Collections;
  37. import java.util.HashMap;
  38. import java.util.Iterator;
  39. import java.util.List;
  40. import java.util.Map;
  41.  
  42. import ca.hss.heatmaplib.HeatMap;
  43. import io.reactivex.android.schedulers.AndroidSchedulers;
  44. import io.reactivex.schedulers.Schedulers;
  45. import java.util.Date;
  46. import java.util.Timer;
  47. import java.util.TimerTask;
  48. import java.util.TreeMap;
  49. import java.util.stream.Collectors;
  50.  
  51. public class Visualization extends AppCompatActivity {
  52. FirebaseFirestore db = FirebaseFirestore.getInstance();
  53. private Button startLeftBtn, startRightBtn;
  54. private ToggleButton connectLeftBtn, connectRightBtn;
  55. private String L_insole_mac = "20:19:06:20:08:07";
  56. private String R_insole_mac = "20:19:06:20:06:72";
  57.  
  58. private String start_bytes = "0 254 128";
  59. private String left_data,right_data, left_temp_bytes, right_temp_bytes;
  60. private String PATIENT_NAME, PATIENT_ID, DOCTOR_NAME, DOCTOR_ID;
  61. private boolean is_L_insole_started = false;
  62. private boolean is_R_insole_started = false;
  63. private boolean is_L_insole_connected = false;
  64. private boolean is_R_insole_connected = false;
  65. private boolean tesTestAsync_r, isTestAsync_l = false;
  66. private int right_data_len, left_data_len = 0;
  67. private int right_sensor_data_count, right_data_index, right_package_count = 0;
  68. private int left_sensor_data_count, left_data_index, left_package_count = 0;
  69. private int max_sensor_data_count = 89;
  70. private int max_data_len = 114;
  71. private int ns_list[] = {58,76,77,78,79,80,81,82,83,84,94,95,96,97,98,99,100,101,102,103,104,105,112,113,114};
  72. private Double r_data_double_arr[] = new Double[89];
  73. private Double l_data_double_arr[] = new Double[89];
  74. private List<Integer> non_sensor_indeces = new ArrayList<Integer>(ns_list.length+1);
  75. private SimpleBluetoothDeviceInterface left_insole_device_interface;
  76. private SimpleBluetoothDeviceInterface right_insole_device_interface;
  77. private TextView leftDataTv, rightDataTv; //just put leftDataTv, rightDataTv here for test
  78. private BluetoothManager bluetoothManager = BluetoothManager.getInstance();
  79. Map<String,Object> patientRecord = new HashMap<>();
  80. private HeatMap heatMapLeft, heatMapRight;
  81. private Timer right_timer = new Timer();
  82. private Timer left_timer = new Timer();
  83.  
  84. Map<String, Object> rightDataPack;
  85. Map<String, Object> leftDataPack;
  86.  
  87.  
  88. private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
  89. private SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  90.  
  91. float x_R[] = {0.25f, 0.25f,0.25f,0.25f,0.25f,0.25f,0.25f,0.25f,0.25f,0.25f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,
  92. 0.4f,0.4f,0.4f,0.4f,0.4f,0.4f,0.4f,0.4f,0.4f,0.4f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,
  93. 0.55f, 0.55f, 0.55f, 0.55f, 0.55f, 0.55f, 0.55f, 0.55f, 0.55f, 0.55f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,
  94. 0.7f,0.7f,0.7f,0.7f,0.7f,0.7f,0.7f,0.7f,0.7f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,
  95. 0.85f,0.85f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,
  96. 0.95f,0.95f,0.95f,0.95f,0.95f,0.95f};
  97.  
  98. float x_L[] = {0.75f,0.75f,0.75f,0.75f,0.75f,0.75f,0.75f,0.75f,0.75f,0.75f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,
  99. 0.6f, 0.6f,0.6f,0.6f,0.6f,0.6f,0.6f,0.6f,0.6f,0.6f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,0.65f,
  100. 0.45f,0.45f,0.45f,0.45f,0.45f,0.45f,0.45f,0.45f,0.45f,0.45f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,
  101. 0.3f,0.3f,0.3f,0.3f,0.3f,0.3f,0.3f,0.3f,0.3f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,0.35f,
  102. 0.15f,0.15f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,
  103. 0.05f,0.05f,0.05f,0.05f,0.05f,0.05f};
  104.  
  105.  
  106. float y[] = {0.95f,0.9f,0.85f,0.8f,0.75f,0.7f,0.65f,0.6f,0.55f,0.50f,0.45f,0.40f,0.35f,0.30f,0.25f,0.20f,0.15f,0.10f, 0.05f,
  107. 0.95f,0.9f,0.85f,0.8f,0.75f,0.7f,0.65f,0.6f,0.55f,0.50f,0.45f,0.40f,0.35f,0.30f,0.25f,0.20f,0.15f,0.10f, 0.05f,
  108. 0.95f,0.9f,0.85f,0.8f,0.75f,0.7f,0.65f,0.6f,0.55f,0.50f,0.45f,0.40f,0.35f,0.30f,0.25f,0.20f,0.15f,0.10f, 0.05f,
  109. 0.9f,0.85f,0.8f,0.75f,0.7f,0.65f,0.6f,0.55f,0.50f,0.45f,0.40f,0.35f,0.30f,0.25f,0.20f,0.15f,0.10f,
  110. 0.55f,0.50f,0.45f,0.40f,0.35f,0.30f,0.25f,0.20f,0.15f,
  111. 0.45f,0.40f,0.35f,0.30f,0.25f,0.20f };
  112.  
  113. List LList = new ArrayList();
  114. List RList = new ArrayList();
  115.  
  116. Map<String,Object> sliceLeft = new HashMap<>();
  117. Map<String,Object> sliceRight = new HashMap<>();
  118. Map<String,Object> testR = new HashMap<>();
  119.  
  120. final Handler handler = new Handler(Looper.getMainLooper());
  121.  
  122.  
  123.  
  124. @Override
  125. protected void onCreate(Bundle savedInstanceState) {
  126. super.onCreate(savedInstanceState);
  127. setContentView(R.layout.activity_visualization);
  128. Date date = new Date();
  129. Intent intent = getIntent();
  130. Bundle extras = intent.getExtras();
  131. PATIENT_NAME = extras.getString("PATIENT_NAME");
  132. PATIENT_ID = extras.getString("PATIENT_ID");
  133. DOCTOR_NAME = extras.getString("DOCTOR_NAME");
  134. DOCTOR_ID = extras.getString("DOCTOR_ID");
  135. patientRecord.put("PATIENT_NAME", PATIENT_NAME);
  136. patientRecord.put("PATIENT_ID", PATIENT_ID);
  137. patientRecord.put("DOCTOR_NAME", DOCTOR_NAME);
  138. patientRecord.put("DOCTOR_ID", DOCTOR_ID);
  139. patientRecord.put("Date", formatter.format(date));
  140. if (bluetoothManager == null) {
  141. // Bluetooth unavailable on this device :( tell the user
  142. Toast.makeText(getApplication(), "Bluetooth not available.", Toast.LENGTH_LONG).show(); // Replace context with your context instance.
  143. finish();
  144. }
  145.  
  146. connectRightBtn = (ToggleButton) findViewById(R.id.connectRight_button);
  147. connectLeftBtn = (ToggleButton) findViewById(R.id.connectLeft_button);
  148. startLeftBtn = (Button) findViewById(R.id.startLeft_button);
  149. startRightBtn = (Button) findViewById(R.id.startRight_button);
  150.  
  151. heatMapRight = (HeatMap) findViewById(R.id.heatmapRight);
  152. heatMapLeft = (HeatMap) findViewById(R.id.heatmapLeft);
  153.  
  154. //Set the range that you want the heat maps gradient to cover
  155. heatMapRight.setMinimum(0);
  156. heatMapRight.setMaximum(64);
  157.  
  158. heatMapLeft.setMinimum(0);
  159. heatMapLeft.setMaximum(64);
  160.  
  161. Map<String,Object> leftDataDict = new TreeMap<>();
  162. Map<String,Object> rightDataDict = new TreeMap<>();
  163. Map<String,Object> leftDataDictCon = new TreeMap<>();
  164. Map<String,Object> rightDataDictCon = new TreeMap<>();
  165. Map<String,Object> testR = new TreeMap<>();
  166. Map<String,Object> testL = new TreeMap<>();
  167.  
  168.  
  169. //make the colour gradient from yellow to red
  170. Map<Float, Integer> colorStops = new ArrayMap<>();
  171. colorStops.put(0.3f, 0xFFDACF03);
  172. colorStops.put(0.4f, 0xFFDA7203);
  173. colorStops.put(1.0f, 0xFFDA031C);
  174. heatMapRight.setColorStops(colorStops);
  175. heatMapRight.setRadius(180);
  176.  
  177. heatMapLeft.setColorStops(colorStops);
  178. heatMapLeft.setRadius(180);
  179.  
  180. for(int i = 0; i<ns_list.length; i++) {
  181. non_sensor_indeces.add(ns_list[i]);
  182. }
  183. connectLeftBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  184. @Override
  185. public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
  186. if(isChecked)
  187. {
  188. connectDevice(L_insole_mac);
  189. }
  190. else
  191. {
  192. if (is_L_insole_connected)
  193. if(is_L_insole_started)
  194. left_insole_device_interface.stopInsole();
  195. startLeftBtn.setText("Start Left");
  196. is_L_insole_started = false;
  197. bluetoothManager.closeDevice(left_insole_device_interface);
  198. is_L_insole_connected = false;
  199. Toast.makeText(Visualization.this, "Left Insole Disconnected.", Toast.LENGTH_SHORT).show();
  200.  
  201. }
  202. }
  203. private void connectDevice(String mac) {
  204. bluetoothManager.openSerialDevice(mac)
  205. .subscribeOn(Schedulers.io())
  206. .observeOn(AndroidSchedulers.mainThread())
  207. .subscribe(this::onConnected, this::onError);
  208. }
  209. @RequiresApi(api = Build.VERSION_CODES.N)
  210. private void onConnected(BluetoothSerialDevice connectedDevice) {
  211. // You are now connected to this device!
  212. // Here you may want to retain an instance to your device:
  213. left_insole_device_interface = connectedDevice.toSimpleDeviceInterface();
  214.  
  215. // Listen to bluetooth events
  216. left_insole_device_interface.setListeners(message -> onMessageReceived(message), this::onMessageSent, this::onError);
  217. left_insole_device_interface.stopInsole();
  218.  
  219. is_L_insole_connected = true;
  220. Toast.makeText(getApplication(), "Connected to Left Insole.", Toast.LENGTH_SHORT).show();
  221.  
  222. }
  223. @RequiresApi(api = Build.VERSION_CODES.N)
  224. private void onMessageReceived(String message) {
  225. //store incoming bytes temporarily
  226. if(!is_L_insole_started){
  227. left_temp_bytes+=message+" ";
  228. }
  229. //check whether the start_bytes exits in the temporary buffer
  230. if(!is_L_insole_started && left_temp_bytes.contains(start_bytes)){
  231. is_L_insole_started = true;
  232. left_temp_bytes ="";
  233. }
  234. //if the start_bytes are found in the temporary buffer, start storing the incoming messages in the actual buffer
  235. if(is_L_insole_started){
  236. left_data_len++;
  237. if(left_data_len>15) {
  238. left_sensor_data_count++;
  239. if (!non_sensor_indeces.contains(left_sensor_data_count)) {
  240. l_data_double_arr[left_data_index] = Double.parseDouble(message);
  241. // System.out.println("NON SENSOR INDEX:" + left_data_index + " "+ message);
  242. left_data_index++;
  243. }
  244. }
  245.  
  246. Date date = new Date();
  247.  
  248. // leftDataDict.put(String.valueOf(formatter.format(date)), Arrays.toString(l_data_double_arr));
  249. //
  250. // if (leftDataDict.size() == 200) {
  251. // leftDataDictCon.clear();
  252. // leftDataDictCon.putAll(leftDataDict);
  253. // leftDataDict.clear();
  254. // }
  255.  
  256. //if the data length reach the max_data_length, release the buffer and invert the start flag
  257. if(left_data_len>=max_data_len+15){
  258. heatMapLeft.clearData();
  259. for(int i=0; i<x_L.length; i++) {
  260. HeatMap.DataPoint point = new HeatMap.DataPoint(x_L[i], y[i], l_data_double_arr[i]);
  261. heatMapLeft.addData(point);
  262. heatMapLeft.forceRefresh();
  263. }
  264. // heatMapLeft.clearData();
  265.  
  266. // Date date = new Date();
  267. // leftDataDict.put(String.valueOf(formatter.format(date)), Arrays.toString(l_data_double_arr));
  268.  
  269. left_package_count++;
  270. left_data_index= 0;
  271. left_sensor_data_count = 0;
  272. left_data_len=0;
  273. is_L_insole_started=false;
  274. }
  275. }
  276. }
  277. private void onMessageSent(String message) {
  278. // We sent a message! Handle it here.
  279. // Toast.makeText(getApplication(), "Sent a message! Message was: " + message, Toast.LENGTH_LONG).show(); // Replace context with your context instance.
  280. }
  281.  
  282. private void onError(Throwable error) {
  283. // Handle the error
  284. }
  285.  
  286. });
  287.  
  288. connectRightBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  289. @Override
  290. public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
  291.  
  292. if (isChecked) {
  293. connectDevice(R_insole_mac);
  294.  
  295. } else {
  296. if (is_R_insole_connected) {
  297. if (is_R_insole_started) {
  298. right_insole_device_interface.stopInsole();
  299. startRightBtn.setText("Start Right");
  300. is_R_insole_started = false;
  301. }
  302. bluetoothManager.closeDevice(right_insole_device_interface);
  303. is_R_insole_connected = false;
  304. Toast.makeText(Visualization.this, "Right Insole Disconnected.", Toast.LENGTH_SHORT).show();
  305. }
  306. }
  307. }
  308.  
  309. private void connectDevice (String mac){
  310. bluetoothManager.openSerialDevice(mac)
  311. .subscribeOn(Schedulers.io())
  312. .observeOn(AndroidSchedulers.mainThread())
  313. .subscribe(this::onConnected, this::onError);
  314. }
  315.  
  316. @RequiresApi(api = Build.VERSION_CODES.O)
  317. private void onConnected (BluetoothSerialDevice connectedDevice){
  318. // You are now connected to this device!
  319. // Here you may want to retain an instance to your device:
  320. right_insole_device_interface = connectedDevice.toSimpleDeviceInterface();
  321.  
  322. // Listen to bluetooth events
  323. right_insole_device_interface.setListeners(message -> onMessageReceived(message), this::onMessageSent, this::onError);
  324. right_insole_device_interface.stopInsole();
  325.  
  326. is_R_insole_connected = true;
  327. Toast.makeText(getApplication(), "Connected to Right Insole.", Toast.LENGTH_SHORT).show();
  328. }
  329.  
  330. private void onMessageSent (String message){
  331. // We sent a message! Handle it here.
  332. }
  333.  
  334. @RequiresApi(api = Build.VERSION_CODES.N)
  335. private void onMessageReceived (String message){
  336. //store incoming bytes temporarily
  337. if(!is_R_insole_started){
  338. right_temp_bytes+=message+" ";
  339. }
  340. //check whether the start_bytes exits in the temporary buffer
  341. if(!is_R_insole_started && right_temp_bytes.contains(start_bytes)){
  342. is_R_insole_started = true;
  343. right_temp_bytes ="";
  344. }
  345. //if the start_bytes are found in the temporary buffer, start storing the incoming messages in the actual buffer
  346. if(is_R_insole_started){
  347. right_data_len++;
  348. if(right_data_len>15) {
  349. right_sensor_data_count++;
  350. if (!non_sensor_indeces.contains(right_sensor_data_count)) {
  351. r_data_double_arr[right_data_index] = Double.parseDouble(message);
  352. // System.out.println("NON SENSOR INDEX:" + right_data_index + " "+ message);
  353. right_data_index++;
  354. }
  355. }
  356.  
  357.  
  358. String a = Arrays.toString(r_data_double_arr);
  359.  
  360. if (a.indexOf(2) > 50){
  361. Log.d(TAG, "babayo2: " + a);
  362. } else {
  363. Log.d(TAG, "babayoq: " + a);
  364. }
  365.  
  366. // if the data length reach the max_data_length, release the buffer and invert the start flag
  367. if(right_data_len>=max_data_len+15){
  368. heatMapRight.clearData();
  369. for(int i=0; i<x_R.length; i++) {
  370. HeatMap.DataPoint point = new HeatMap.DataPoint(x_R[i], y[i], r_data_double_arr[i]);
  371. heatMapRight.addData(point);
  372. heatMapRight.forceRefresh();
  373. }
  374.  
  375. right_data_index= 0;
  376. right_sensor_data_count = 0;
  377. right_data_len=0;
  378. is_R_insole_started=false;
  379. }
  380. }
  381. }
  382.  
  383.  
  384. private void onError(Throwable error) {
  385. // Handle the error
  386. }
  387. });
  388.  
  389. startLeftBtn.setOnClickListener(new View.OnClickListener() {
  390. @Override
  391. public void onClick(View view) {
  392. if (is_L_insole_connected) {
  393. if (is_L_insole_started) {
  394. left_insole_device_interface.stopInsole();
  395. is_L_insole_started = false;
  396. startLeftBtn.setText("Start Left");
  397. left_timer.cancel();
  398.  
  399. } else {
  400. left_insole_device_interface.startInsole();
  401. is_L_insole_started = true;
  402. startLeftBtn.setText("Stop Left");
  403.  
  404. left_timer.scheduleAtFixedRate(new TimerTask() {
  405. @Override
  406. public void run() {
  407. runOnUiThread(new Runnable() {
  408. @RequiresApi(api = Build.VERSION_CODES.N)
  409. public void run() {
  410. // Log.d("TAG", "Hashmap: "+ leftDataDict);
  411. sendToFirebase(leftDataDict, "Left_Insole");
  412. }
  413. });
  414. }
  415. },1000, 1000);
  416.  
  417. Toast.makeText(Visualization.this, "Left Insole Started.", Toast.LENGTH_SHORT).show();
  418.  
  419. }
  420. }else
  421. {
  422. Toast.makeText(Visualization.this, "Left Insole Not Connected!", Toast.LENGTH_SHORT).show();
  423. }
  424.  
  425. }
  426. });
  427.  
  428.  
  429. startRightBtn.setOnClickListener(new View.OnClickListener() {
  430. @Override
  431. public void onClick(View view) {
  432.  
  433. if(is_R_insole_connected) {
  434. if (is_R_insole_started) {
  435. right_insole_device_interface.stopInsole();
  436. is_R_insole_started = false;
  437. startRightBtn.setText("Start Right");
  438. right_timer.cancel();
  439. left_timer.cancel();
  440. } else {
  441. right_insole_device_interface.startInsole();
  442. is_R_insole_started = true;
  443. startRightBtn.setText("Stop Right");
  444.  
  445. right_timer.scheduleAtFixedRate(new TimerTask() {
  446. @Override
  447. public void run() {
  448. runOnUiThread(new Runnable() {
  449. public void run() {
  450.  
  451. String a = Arrays.toString(r_data_double_arr);
  452.  
  453. if (a.indexOf(2) > 50){
  454. Log.d(TAG, "babayo2: " + a);
  455. } else {
  456. Log.d(TAG, "babayoq: " + a);
  457. }
  458.  
  459. sendToFirebase(rightDataDictCon, "Right_Insole");
  460.  
  461. }
  462. });
  463. }
  464. },1000, 1000);
  465.  
  466.  
  467. Toast.makeText(Visualization.this, "Right Insole Started.", Toast.LENGTH_SHORT).show();
  468.  
  469. }
  470. }else{
  471. Toast.makeText(Visualization.this, "Right Insole Not Connected!", Toast.LENGTH_SHORT).show();
  472. }
  473. }
  474. });
  475.  
  476.  
  477.  
  478.  
  479. }
  480.  
  481. @Override
  482. protected void onStop() {
  483. super.onStop();
  484. left_insole_device_interface.stopInsole();
  485. right_insole_device_interface.stopInsole();
  486. bluetoothManager.close();
  487. is_L_insole_connected = false;
  488. is_R_insole_connected = false;
  489. }
  490.  
  491. public void sendToFirebase(Map<String,Object> data, String of_insole){
  492. Date date = new Date();
  493. db.collection("Experiments").document(PATIENT_NAME).set(patientRecord);
  494. db.collection("Experiments")
  495. .document(PATIENT_NAME)
  496. .collection(of_insole)
  497. .document(String.valueOf(formatter2.format(date)))
  498. .set(data)
  499. .addOnSuccessListener(new OnSuccessListener<Void>() {
  500. @Override
  501. public void onSuccess(Void unused) {
  502. // data.clear();
  503. Toast.makeText(Visualization.this, "Succesfully saved to Firebase", Toast.LENGTH_SHORT).show();
  504. }
  505. })
  506. .addOnFailureListener(new OnFailureListener() {
  507. @Override
  508. public void onFailure(@NonNull Exception e) {
  509. Toast.makeText(Visualization.this, "Failed to saved to Firebase", Toast.LENGTH_SHORT).show();
  510. }
  511. });
  512. }
  513.  
  514.  
  515.  
  516. }
  517.  
  518.  
  519.  
  520.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement