i am newbie in android,i have class names LedControl,i have to display realtime linechart in mpandroid. I have tried lot but all vain.but no linechart is drawing.Please Help with full code.
i have class LedControl where i have code for hc-05 Bluetooth connection. After that in my inner class ConnectBt extended asyntask.I am taking data from hc-05 continuously and writing data in file hco5.csv.The data is in this format date,time,temperature,gasval,x,y,z(ex:"11/03/2021,09:12:30,45,56,0.67,0.45,0.89")and reading same file,now i have to read the file and plot linechart continously until i stop the process from android studio.I have addEntry method,that i am calling addEntry method from thread
Class LedControl
{
fileOutputStream=openFileOutput("hco5.csv",Context.MODE_PRIVATE);
//called Connectb innner class that extended Asyntask``
`new LedControl.ConnectBT().execute();`
` //calling addEntry from main class to plot linechart`
new Thread(new Runnable()
{
@Override`
public void run()
{
for(int i = 0; i < 100; i++)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
addEntry();
}
});
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
}
}).start();
//inner class where i m write data from Bluetooth in file n reading same
public class ConnectBT extends AsyncTask<Void, Void, Void>
{
private Boolean ConnectSuccess = true;
@Override
protected void onPreExecute ()
{
//progress = ProgressDialog.show(LedControl.this, "Connecting...", "Please Wait!!!");
}
@Override
protected Void doInBackground (Void... devices)
{
try
{
if ( btSocket==null || !isBtConnected ) {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
}
catch (IOException e)
{
ConnectSuccess = false;
}
return null;
}
@Override
protected void onPostExecute (Void result)
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
} else
{
msg("Connected");
isBtConnected = true;
if ( btSocket != null )
{
try
{
String line = null;
LineData data = lineChart.getData();
//btSocket.getOutputStream().write(number.toString().getBytes());
inputStream=btSocket.getInputStream();
while (true)
{
try
{
bytesCount = inputStream.read(buffer);
String message = new String(buffer, 0, bytesCount);
//Thread.sleep(1000);
fileOutputStream.write(message.getBytes());
Log.d("TAGh", "Received message: " + message);
fileInputStream = openFileInput("hc05xyz.csv");
BufferedReader br =new BufferedReader(new InputStreamReader(fileInputStream));
while((line = br.readLine()) != null)
{
// Log.e("file data", line);
parse = line.split(",");
for (int i = 0; i < parse.length; i++)
{
// Log.d("TAGh", "data " + parse[i]);
date.add(parse[i]);
String a=parse[++i];
res =a.split(":");
//maketime(res);
bul=parse[++i];
//makegas(bul);
//temp.add(parse[++i]);
// bul=parse[++i];
// makegas(bul);
gas.add(parse[++i]);
Xyz xyz1 = new Xyz();
xyz1.setX(parse[++i]);
xyz1.setY(parse[++i]);
xyz1.setZ(parse[++i]);
xyzs.add(xyz1);
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
catch (IOException e)
{
msg("Error");
}
datadate.setText(date.get(0));
}
}
}
}
private void addEntry()
{
LineData data = lineChart.getData();
if (data != null)
{
ILineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = createSet();
data.addDataSet(set);
}
//sample from google
data.addEntry(new Entry((float) ((Math.random() * 20) + 2f), set.getEntryCount()), 0);
// let the chart know it's data has changed
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
// limit the number of visible entries
lineChart.setVisibleXRangeMaximum(15);
// move to the latest entry
lineChart.moveViewToX(data.getEntryCount());
}
` }
}