Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.harrysoft.androidbluetoothserial.demoapp;
- import static android.content.ContentValues.TAG;
- import androidx.annotation.NonNull;
- import androidx.annotation.RequiresApi;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.util.ArrayMap;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CompoundButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- import com.google.android.gms.tasks.OnFailureListener;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.firebase.firestore.FirebaseFirestore;
- import com.harrysoft.androidbluetoothserial.BluetoothManager;
- import com.harrysoft.androidbluetoothserial.BluetoothSerialDevice;
- import com.harrysoft.androidbluetoothserial.SimpleBluetoothDeviceInterface;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import ca.hss.heatmaplib.HeatMap;
- import io.reactivex.android.schedulers.AndroidSchedulers;
- import io.reactivex.schedulers.Schedulers;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.TreeMap;
- import java.util.stream.Collectors;
- public class Visualization extends AppCompatActivity {
- FirebaseFirestore db = FirebaseFirestore.getInstance();
- private Button startLeftBtn, startRightBtn;
- private ToggleButton connectLeftBtn, connectRightBtn;
- private String L_insole_mac = "20:19:06:20:08:07";
- private String R_insole_mac = "20:19:06:20:06:72";
- private String start_bytes = "0 254 128";
- private String left_data,right_data, left_temp_bytes, right_temp_bytes;
- private String PATIENT_NAME, PATIENT_ID, DOCTOR_NAME, DOCTOR_ID;
- private boolean is_L_insole_started = false;
- private boolean is_R_insole_started = false;
- private boolean is_L_insole_connected = false;
- private boolean is_R_insole_connected = false;
- private boolean tesTestAsync_r, isTestAsync_l = false;
- private int right_data_len, left_data_len = 0;
- private int right_sensor_data_count, right_data_index, right_package_count = 0;
- private int left_sensor_data_count, left_data_index, left_package_count = 0;
- private int max_sensor_data_count = 89;
- private int max_data_len = 114;
- 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};
- private Double r_data_double_arr[] = new Double[89];
- private Double l_data_double_arr[] = new Double[89];
- private List<Integer> non_sensor_indeces = new ArrayList<Integer>(ns_list.length+1);
- private SimpleBluetoothDeviceInterface left_insole_device_interface;
- private SimpleBluetoothDeviceInterface right_insole_device_interface;
- private TextView leftDataTv, rightDataTv; //just put leftDataTv, rightDataTv here for test
- private BluetoothManager bluetoothManager = BluetoothManager.getInstance();
- Map<String,Object> patientRecord = new HashMap<>();
- private HeatMap heatMapLeft, heatMapRight;
- private Timer right_timer = new Timer();
- private Timer left_timer = new Timer();
- Map<String, Object> rightDataPack;
- Map<String, Object> leftDataPack;
- private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
- private SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
- 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,
- 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,
- 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,
- 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,
- 0.85f,0.85f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,0.8f,
- 0.95f,0.95f,0.95f,0.95f,0.95f,0.95f};
- 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,
- 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,
- 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,
- 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,
- 0.15f,0.15f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,0.2f,
- 0.05f,0.05f,0.05f,0.05f,0.05f,0.05f};
- 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,
- 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,
- 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,
- 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.55f,0.50f,0.45f,0.40f,0.35f,0.30f,0.25f,0.20f,0.15f,
- 0.45f,0.40f,0.35f,0.30f,0.25f,0.20f };
- List LList = new ArrayList();
- List RList = new ArrayList();
- Map<String,Object> sliceLeft = new HashMap<>();
- Map<String,Object> sliceRight = new HashMap<>();
- Map<String,Object> testR = new HashMap<>();
- final Handler handler = new Handler(Looper.getMainLooper());
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_visualization);
- Date date = new Date();
- Intent intent = getIntent();
- Bundle extras = intent.getExtras();
- PATIENT_NAME = extras.getString("PATIENT_NAME");
- PATIENT_ID = extras.getString("PATIENT_ID");
- DOCTOR_NAME = extras.getString("DOCTOR_NAME");
- DOCTOR_ID = extras.getString("DOCTOR_ID");
- patientRecord.put("PATIENT_NAME", PATIENT_NAME);
- patientRecord.put("PATIENT_ID", PATIENT_ID);
- patientRecord.put("DOCTOR_NAME", DOCTOR_NAME);
- patientRecord.put("DOCTOR_ID", DOCTOR_ID);
- patientRecord.put("Date", formatter.format(date));
- if (bluetoothManager == null) {
- // Bluetooth unavailable on this device :( tell the user
- Toast.makeText(getApplication(), "Bluetooth not available.", Toast.LENGTH_LONG).show(); // Replace context with your context instance.
- finish();
- }
- connectRightBtn = (ToggleButton) findViewById(R.id.connectRight_button);
- connectLeftBtn = (ToggleButton) findViewById(R.id.connectLeft_button);
- startLeftBtn = (Button) findViewById(R.id.startLeft_button);
- startRightBtn = (Button) findViewById(R.id.startRight_button);
- heatMapRight = (HeatMap) findViewById(R.id.heatmapRight);
- heatMapLeft = (HeatMap) findViewById(R.id.heatmapLeft);
- //Set the range that you want the heat maps gradient to cover
- heatMapRight.setMinimum(0);
- heatMapRight.setMaximum(64);
- heatMapLeft.setMinimum(0);
- heatMapLeft.setMaximum(64);
- Map<String,Object> leftDataDict = new TreeMap<>();
- Map<String,Object> rightDataDict = new TreeMap<>();
- Map<String,Object> leftDataDictCon = new TreeMap<>();
- Map<String,Object> rightDataDictCon = new TreeMap<>();
- Map<String,Object> testR = new TreeMap<>();
- Map<String,Object> testL = new TreeMap<>();
- //make the colour gradient from yellow to red
- Map<Float, Integer> colorStops = new ArrayMap<>();
- colorStops.put(0.3f, 0xFFDACF03);
- colorStops.put(0.4f, 0xFFDA7203);
- colorStops.put(1.0f, 0xFFDA031C);
- heatMapRight.setColorStops(colorStops);
- heatMapRight.setRadius(180);
- heatMapLeft.setColorStops(colorStops);
- heatMapLeft.setRadius(180);
- for(int i = 0; i<ns_list.length; i++) {
- non_sensor_indeces.add(ns_list[i]);
- }
- connectLeftBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
- if(isChecked)
- {
- connectDevice(L_insole_mac);
- }
- else
- {
- if (is_L_insole_connected)
- if(is_L_insole_started)
- left_insole_device_interface.stopInsole();
- startLeftBtn.setText("Start Left");
- is_L_insole_started = false;
- bluetoothManager.closeDevice(left_insole_device_interface);
- is_L_insole_connected = false;
- Toast.makeText(Visualization.this, "Left Insole Disconnected.", Toast.LENGTH_SHORT).show();
- }
- }
- private void connectDevice(String mac) {
- bluetoothManager.openSerialDevice(mac)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(this::onConnected, this::onError);
- }
- @RequiresApi(api = Build.VERSION_CODES.N)
- private void onConnected(BluetoothSerialDevice connectedDevice) {
- // You are now connected to this device!
- // Here you may want to retain an instance to your device:
- left_insole_device_interface = connectedDevice.toSimpleDeviceInterface();
- // Listen to bluetooth events
- left_insole_device_interface.setListeners(message -> onMessageReceived(message), this::onMessageSent, this::onError);
- left_insole_device_interface.stopInsole();
- is_L_insole_connected = true;
- Toast.makeText(getApplication(), "Connected to Left Insole.", Toast.LENGTH_SHORT).show();
- }
- @RequiresApi(api = Build.VERSION_CODES.N)
- private void onMessageReceived(String message) {
- //store incoming bytes temporarily
- if(!is_L_insole_started){
- left_temp_bytes+=message+" ";
- }
- //check whether the start_bytes exits in the temporary buffer
- if(!is_L_insole_started && left_temp_bytes.contains(start_bytes)){
- is_L_insole_started = true;
- left_temp_bytes ="";
- }
- //if the start_bytes are found in the temporary buffer, start storing the incoming messages in the actual buffer
- if(is_L_insole_started){
- left_data_len++;
- if(left_data_len>15) {
- left_sensor_data_count++;
- if (!non_sensor_indeces.contains(left_sensor_data_count)) {
- l_data_double_arr[left_data_index] = Double.parseDouble(message);
- // System.out.println("NON SENSOR INDEX:" + left_data_index + " "+ message);
- left_data_index++;
- }
- }
- Date date = new Date();
- // leftDataDict.put(String.valueOf(formatter.format(date)), Arrays.toString(l_data_double_arr));
- //
- // if (leftDataDict.size() == 200) {
- // leftDataDictCon.clear();
- // leftDataDictCon.putAll(leftDataDict);
- // leftDataDict.clear();
- // }
- //if the data length reach the max_data_length, release the buffer and invert the start flag
- if(left_data_len>=max_data_len+15){
- heatMapLeft.clearData();
- for(int i=0; i<x_L.length; i++) {
- HeatMap.DataPoint point = new HeatMap.DataPoint(x_L[i], y[i], l_data_double_arr[i]);
- heatMapLeft.addData(point);
- heatMapLeft.forceRefresh();
- }
- // heatMapLeft.clearData();
- // Date date = new Date();
- // leftDataDict.put(String.valueOf(formatter.format(date)), Arrays.toString(l_data_double_arr));
- left_package_count++;
- left_data_index= 0;
- left_sensor_data_count = 0;
- left_data_len=0;
- is_L_insole_started=false;
- }
- }
- }
- private void onMessageSent(String message) {
- // We sent a message! Handle it here.
- // Toast.makeText(getApplication(), "Sent a message! Message was: " + message, Toast.LENGTH_LONG).show(); // Replace context with your context instance.
- }
- private void onError(Throwable error) {
- // Handle the error
- }
- });
- connectRightBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
- if (isChecked) {
- connectDevice(R_insole_mac);
- } else {
- if (is_R_insole_connected) {
- if (is_R_insole_started) {
- right_insole_device_interface.stopInsole();
- startRightBtn.setText("Start Right");
- is_R_insole_started = false;
- }
- bluetoothManager.closeDevice(right_insole_device_interface);
- is_R_insole_connected = false;
- Toast.makeText(Visualization.this, "Right Insole Disconnected.", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void connectDevice (String mac){
- bluetoothManager.openSerialDevice(mac)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(this::onConnected, this::onError);
- }
- @RequiresApi(api = Build.VERSION_CODES.O)
- private void onConnected (BluetoothSerialDevice connectedDevice){
- // You are now connected to this device!
- // Here you may want to retain an instance to your device:
- right_insole_device_interface = connectedDevice.toSimpleDeviceInterface();
- // Listen to bluetooth events
- right_insole_device_interface.setListeners(message -> onMessageReceived(message), this::onMessageSent, this::onError);
- right_insole_device_interface.stopInsole();
- is_R_insole_connected = true;
- Toast.makeText(getApplication(), "Connected to Right Insole.", Toast.LENGTH_SHORT).show();
- }
- private void onMessageSent (String message){
- // We sent a message! Handle it here.
- }
- @RequiresApi(api = Build.VERSION_CODES.N)
- private void onMessageReceived (String message){
- //store incoming bytes temporarily
- if(!is_R_insole_started){
- right_temp_bytes+=message+" ";
- }
- //check whether the start_bytes exits in the temporary buffer
- if(!is_R_insole_started && right_temp_bytes.contains(start_bytes)){
- is_R_insole_started = true;
- right_temp_bytes ="";
- }
- //if the start_bytes are found in the temporary buffer, start storing the incoming messages in the actual buffer
- if(is_R_insole_started){
- right_data_len++;
- if(right_data_len>15) {
- right_sensor_data_count++;
- if (!non_sensor_indeces.contains(right_sensor_data_count)) {
- r_data_double_arr[right_data_index] = Double.parseDouble(message);
- // System.out.println("NON SENSOR INDEX:" + right_data_index + " "+ message);
- right_data_index++;
- }
- }
- String a = Arrays.toString(r_data_double_arr);
- if (a.indexOf(2) > 50){
- Log.d(TAG, "babayo2: " + a);
- } else {
- Log.d(TAG, "babayoq: " + a);
- }
- // if the data length reach the max_data_length, release the buffer and invert the start flag
- if(right_data_len>=max_data_len+15){
- heatMapRight.clearData();
- for(int i=0; i<x_R.length; i++) {
- HeatMap.DataPoint point = new HeatMap.DataPoint(x_R[i], y[i], r_data_double_arr[i]);
- heatMapRight.addData(point);
- heatMapRight.forceRefresh();
- }
- right_data_index= 0;
- right_sensor_data_count = 0;
- right_data_len=0;
- is_R_insole_started=false;
- }
- }
- }
- private void onError(Throwable error) {
- // Handle the error
- }
- });
- startLeftBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (is_L_insole_connected) {
- if (is_L_insole_started) {
- left_insole_device_interface.stopInsole();
- is_L_insole_started = false;
- startLeftBtn.setText("Start Left");
- left_timer.cancel();
- } else {
- left_insole_device_interface.startInsole();
- is_L_insole_started = true;
- startLeftBtn.setText("Stop Left");
- left_timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @RequiresApi(api = Build.VERSION_CODES.N)
- public void run() {
- // Log.d("TAG", "Hashmap: "+ leftDataDict);
- sendToFirebase(leftDataDict, "Left_Insole");
- }
- });
- }
- },1000, 1000);
- Toast.makeText(Visualization.this, "Left Insole Started.", Toast.LENGTH_SHORT).show();
- }
- }else
- {
- Toast.makeText(Visualization.this, "Left Insole Not Connected!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- startRightBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(is_R_insole_connected) {
- if (is_R_insole_started) {
- right_insole_device_interface.stopInsole();
- is_R_insole_started = false;
- startRightBtn.setText("Start Right");
- right_timer.cancel();
- left_timer.cancel();
- } else {
- right_insole_device_interface.startInsole();
- is_R_insole_started = true;
- startRightBtn.setText("Stop Right");
- right_timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- public void run() {
- String a = Arrays.toString(r_data_double_arr);
- if (a.indexOf(2) > 50){
- Log.d(TAG, "babayo2: " + a);
- } else {
- Log.d(TAG, "babayoq: " + a);
- }
- sendToFirebase(rightDataDictCon, "Right_Insole");
- }
- });
- }
- },1000, 1000);
- Toast.makeText(Visualization.this, "Right Insole Started.", Toast.LENGTH_SHORT).show();
- }
- }else{
- Toast.makeText(Visualization.this, "Right Insole Not Connected!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- protected void onStop() {
- super.onStop();
- left_insole_device_interface.stopInsole();
- right_insole_device_interface.stopInsole();
- bluetoothManager.close();
- is_L_insole_connected = false;
- is_R_insole_connected = false;
- }
- public void sendToFirebase(Map<String,Object> data, String of_insole){
- Date date = new Date();
- db.collection("Experiments").document(PATIENT_NAME).set(patientRecord);
- db.collection("Experiments")
- .document(PATIENT_NAME)
- .collection(of_insole)
- .document(String.valueOf(formatter2.format(date)))
- .set(data)
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void unused) {
- // data.clear();
- Toast.makeText(Visualization.this, "Succesfully saved to Firebase", Toast.LENGTH_SHORT).show();
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Toast.makeText(Visualization.this, "Failed to saved to Firebase", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement