package com.example.tarunkhajuria.vaccdec;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Binder;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.DataPoint;
import java.io.IOException;
import java.util.UUID;
import android.os.Message;
import android.os.Handler;
import java.io.InputStream;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.DataPoint;
public class BluetoothService extends Service {
    private final IBinder mbinder=new btbinder();
    private BluetoothSocket msocket;
    private InputStream mstream;
    private Handler mHandler= new Handler();
    private byte[] mbuffer = new byte[1024];
    public LineGraphSeries mseries;
    public BluetoothService() {
    }
    public class btbinder extends Binder
    {
        public BluetoothService getService()
        {
            return BluetoothService.this;
        }
    }
    public int connectBt(BluetoothDevice btdevice)
    {
        try {
            msocket = btdevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        }
        catch(Exception e)
        {
            Log.e("Bluetooth","Socket Not given:"+e);
            return -1;
        }
        try{
            msocket.connect();
        }catch(IOException e)
        {
            Log.e("Bluetooth","Could not connect with error:"+e);
            return -1;
        }
        return 1;

    }
    public void readData(final LineGraphSeries series)
    {
     try{
         Log.d("BluetoothisConnected",""+msocket.isConnected());
         mstream=msocket.getInputStream();
     }catch (Exception e)
     {
         Log.e("Bluetooth","Error:"+e);
     }
        new Thread(){
            public void run() {
                while (true) {
                    try {

                        int numBytes=mstream.read(mbuffer);
                        if (mbuffer != null) {
                            String a=new String(mbuffer,"UTF-8");
                            Log.d("BeforeData",""+a);
                            setdata(a);
                        }
                    } catch (IOException e) {
                        Log.d("Bluetooth", "" + e);
                    }
                }
            }
            void setdata(String data)
            {   float temp=-30,time=-30,prevtime=-30,prevtemp=-30;
                boolean halt=false;
                for(int i=0;i<data.length();i++)
                {
                    switch (data.charAt(i))
                    {
                        case 'C':temp=getnum(data,i+1);
                                break;

                        case 't':time=getnum(data,i+1);
                                if(temp!=-30) {
                                    halt = true;
                                }
                                break;
                        default:break;
                    }
                    if(halt)
                    {
                        break;
                    }
                }
                if(time!=-30 && temp!=-30) {
                    Log.d("Data","temp:"+temp+"time:"+time);
                    if(time>prevtime) {
                        series.appendData(new DataPoint(time, temp), true, 20);
                        prevtime = time;
                        prevtemp = temp;
                    }
                }
            }
            float getnum(String data,int i)
            {
                String num="";
                for(;data.charAt(i)!='\t' && data.charAt(i)!='\n';i++)
                {
                    if(data.charAt(i)!='t'&&data.charAt(i)!='C')
                        num+=data.charAt(i);
                }
                if(num=="")
                    return -30;
                else
                    return Float.parseFloat(num);
            }

        }.start();


    }


    @Override
    public IBinder onBind(Intent intent) {
        return mbinder;
    }
}

@@ -1935,16 +1935,71 @@
 read()%7B%0A
+            float prevtime=-30,prevtemp=-30,time,temp;%0A
         
@@ -2079,16 +2079,118 @@
   try %7B%0A
+                        mbuffer=new byte%5B1024%5D;%0A                        while(mstream.available()%3C50);
 %0A       
@@ -2349,16 +2349,61 @@
 fer,
-%22UTF-8%22)
+0,numBytes);%0A                            mbuffer=null
 ;%0A  
@@ -2594,28 +2594,28 @@
     Log.
-d(%22Bluetooth
+e(%22Exception
 %22, %22%22 + 
@@ -2664,32 +2664,33 @@
 %7D%0A            %7D%0A
+%0A
             void
@@ -2731,22 +2731,16 @@
 %7B   
-float 
 temp=-30
 ,tim
@@ -2739,39 +2739,13 @@
 =-30
-,time=-30,prevtime=-30,prevtemp
+;time
 =-30
@@ -3594,61 +3594,97 @@
     
-series.appendData(new DataPoint(time, temp), true, 20
+Log.d(%22Values%22,%22time:%22+time);%0A                        Log.d(%22Values%22,%22prevtime:%22+prevtime
 );%0A 
@@ -3760,24 +3760,105 @@
 emp = temp;%0A
+                        series.appendData(new DataPoint(time, temp), true, 20);%0A%0A
             
@@ -3988,16 +3988,74 @@
 num=%22%22;%0A
+                if(i%3Cdata.length())%0A                %7B%0A    
         
@@ -4125,34 +4125,42 @@
                 
-%7B%0A
+    %7B%0A    
                 
@@ -4166,16 +4166,17 @@
      if(
+(
 data.cha
@@ -4185,15 +4185,16 @@
 t(i)
-!='t'
+%3E=48 
 &&
+ 
 data
@@ -4207,15 +4207,40 @@
 t(i)
-!='C
+%3C=57)%7C%7Cdata.charAt(i)=='.
 ')%0A
+    
     
@@ -4292,34 +4292,32 @@
             
-%7D%0A
         
         if(n
@@ -4304,24 +4304,29 @@
         
+else%0A
         
 if(num==
@@ -4321,20 +4321,8 @@
     
-if(num==%22%22)%0A
     
@@ -4357,37 +4357,74 @@
                 
-else%0A
+    %7D%0A                    if(num!=%22%22)%0A    
                 
@@ -4457,16 +4457,149 @@
 t(num);%0A
+                    else%0A                        return -30;%0A                %7D%0A                else%0A                    return -30;%0A%0A
         

Clean Plot and Data Extraction