Problem to build an real time monitoring graph using graphview in android studio

Thread Starter

Lemon_Foam

Joined Sep 18, 2017
34
I am building a realtime monitoring graph app using data that send via bluetooth from a bluno controller with the help of android studio, but it shown no error in android studio, but after install in phone, the app cannot run. What is wrong with my code?

This is my xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidpaddingBottom="@dimen/activity_vertical_margin"
androidpaddingLeft="@dimen/activity_horizontal_margin"
androidpaddingRight="@dimen/activity_horizontal_margin"
androidpaddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#0c0c50">


<TextView
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/serialSendText"
android:layout_below="@+id/serialSendText"
android:layout_marginTop="20dp"
android:ems="10"
android:text="GSR DATA:"
android:textSize="20sp"
android:textColor="#FFFFFF"/>

<Button
android:id="@+id/buttonScan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SCAN"
android:textSize="20dip"
android:background="#1a8f1e"
android:textColor="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<com.jjoe64.graphview.GraphlView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/graph"
android:layout_below="@+id/editText2"
android:layout_alignStart="@+id/editText2">

<TextView
android:id="@+id/serialReveicedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:layout_below="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_alignEnd="@+id/serialSendText"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"/>
</com.jjoe64.graphview.GraphlView>

<EditText
android:id="@+id/serialSendText"
android:layout_width="310dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#11b0b9"
android:bufferType="editable"
android:ems="10"
android:inputType="text"
android:text="REAL TIME DATABASE AVAILABLE IN FIREBASE"
android:textColor="#ffffff"
android:textSize="13dip"
android:layout_below="@+id/buttonScan"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSerialSend"
android:layout_alignParentStart="true"
android:layout_above="@+id/serialSendText"
android:background="#1FBBA6"/>

</RelativeLayout>




This is my java code

package com.dfrobot.angelo.blunobasicdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.firebase.client.Firebase;
import android.text. format.Time;

import java.util.Random;

import android.app.Activity;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.sql.Timestamp;


public class MainActivity extends BlunoLibrary {
private Button buttonScan;
private Button buttonSerialSend;
private EditText serialSendText;
private TextView serialReceivedText;


private static final Random RANDOM = new Random();
private LineGraphSeries<DataPoint> series;
private int lastX = 0;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// we get graph view instance
GraphView graph = (GraphView) findViewById(R.id.graph);
// data
series = new LineGraphSeries<DataPoint>();
graph.addSeries(series);
// customize a little bit viewport
Viewport viewport = graph.getViewport();
viewport.setYAxisBoundsManual(true);
viewport.setMinY(0);
viewport.setMaxY(10);
viewport.setScrollable(true);

Firebase.setAndroidContext(this);
onCreateProcess(); //onCreate Process by BlunoLibrary


serialBegin(115200); //set the Uart Baudrate on BLE chip to 115200

serialReceivedText = (TextView) findViewById(R.id.serialReveicedText); //initial the EditText of the received data
serialSendText = (EditText) findViewById(R.id.serialSendText); //initial the EditText of the sending data

buttonSerialSend = (Button) findViewById(R.id.buttonSerialSend); //initial the button for sending the data
buttonSerialSend.setOnClickListener(new OnClickListener() {

@override
public void onClick(View v) {


serialSend(serialSendText.getText().toString()); //send the data to the BLUNO
}
});

buttonScan = (Button) findViewById(R.id.buttonScan); //initial the button for scanning the BLE device
buttonScan.setOnClickListener(new OnClickListener() {

@override
public void onClick(View v) {


buttonScanOnClickProcess(); //Alert Dialog for selecting the BLE device
}
});


}

protected void onResume() {
super.onResume();
System.out.println("BlUNOActivity onResume");

onResumeProcess();

//onResume Process by BlunoLibrary

// we're going to simulate real time with thread that append data to the graph
new Thread(new Runnable() {

@override
public void run() {
// we add 4 new entries
for (int i = 0; i < 100; i++) {
runOnUiThread(new Runnable() {

@override
public void run() {
addEntry();
}
});

// sleep to slow down the add of entries
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// manage error ...
}
}
}
}).start();
}

// add random data to graph
private void addEntry() {
// here, we choose to display max 10 points on the viewport and we scroll to end
series.appendData(new DataPoint(lastX++, RANDOM.nextDouble() * 10d), true, 10);
}





@override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
onActivityResultProcess(requestCode, resultCode, data); //onActivityResult Process by BlunoLibrary
super.onActivityResult(requestCode, resultCode, data);
}



@override
public void onConectionStateChange(connectionStateEnum theConnectionState) {//Once connection state changes, this function will be called
switch (theConnectionState) { //Four connection state
case isConnected:
buttonScan.setText("CONNECTED");
break;
case isConnecting:
buttonScan.setText("CONNECTING");
break;
case isToScan:
buttonScan.setText("SCAN");
break;
case isScanning:
buttonScan.setText("SCANNING");
break;
case isDisconnecting:
buttonScan.setText("is Disconnecting");
break;
default:
break;
}
}
/** * Clock utility. */
public static String getNow() {
Time now = new Time();
now.setToNow();
String sTime = now.format("%Y_%m_%d %T");
return sTime;
} /** * get current time in human-readable form without spaces and special characters. * the returned value may be used to compose a file name. * @Return current time as a string. */ public static String getTimeStamp() { Time now = new Time(); now.setToNow(); String sTime = now.format("%Y_%m_%d_%H_%M_%S"); return sTime; }



@override
public void onSerialReceived(String theString) { //Once connection data received, this function will be called

/* int time = (int) (System.currentTimeMillis()); //
Timestamp tsTemp = new Timestamp(time); //
String ts = tsTemp.toString(); //
*/ String text = "@" + getNow() + "\n" + theString + "\n" ;
serialReceivedText.append(text);//append the text into the EditText

//The Serial data from the BLUNO may be sub-packaged, so using a buffer to hold the String is a good choice.
((ScrollView)serialReceivedText.getParent()).fullScroll(View.FOCUS_DOWN);

Firebase ref = new Firebase(Config.FIREBASE_URL);
Person person = new Person();
person.setName(theString);
ref.child("Person").child(getNow()).setValue(person);
}

}
 
Top