2019年2月20日 星期三

Android:active listen to sensor & active notification in background executive Service

in this article  I implement:

USE Andriod studio project & source code download:
GitHub:https://github.com/agathakuan/Andorid-Background-execution-double-service

Figure:

1.services communicate with Broadcast2.services send different actions Broadcast to activity3.HOW to make a broadcast receiver in service4.after Android 8.0(Oreo), how to send a system Notification(in NotificationHelper.java)
5.HOW to implement a sensor-listening class(in ShakeListener.java)

My structure of ShakingWarning app:

1.main Activity only for maintain GUI
2.Input service for active listening to hardware sensor
(accelerometer for example)
3.Output service for send warning messenge by system notification.
4.if trigger shaking event,Input service send broadcast to main activity & Output service
- main activity will show Toast after receive broadcast from input service.
- if main activity is onPause,output service still working in background,which can send system notification.

Main source code

AndroidManifest.xml
user-permission for system notification VIBRATE

 <uses-permission android:name="android.permission.VIBRATE"/>


register input & output service

 <service
            android:name=".outputService"
            android:enabled="true"
            android:exported="true"></service>
        <service
            android:name=".inputService"
            android:enabled="true"
            android:exported="true" />


MainActivity.java
connect to 2 services (input &output)
private ServiceConnection inputConnect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            inputBinder =(inputService.inputBinder) service;
            inputBinder.startInput();

            printInputServiceInfo("onInputServiceConnected ...");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            printInputServiceInfo("onInputServiceDisconnected ...");

        }
    };

    private ServiceConnection outputConnect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            outputBinder = (outputService.outputBinder) service;
            outputBinder.startOutput();

            printOutputServiceInfo("onOutputServiceConnected ...");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            printOutputServiceInfo("onOutputServiceDisconnected ...");
        }
    };

onDestroy disconnect 2 services

@Override
    protected void onDestroy() {
        Intent mInputService_stop = new Intent(this, inputService.class);
        stopService(mInputService_stop);

        Intent mOutputService_stop = new Intent(this, outputService.class);
        stopService(mOutputService_stop);

        localBroadcastManager.unregisterReceiver(localReceiver);
        super.onDestroy();
    }

input service receiver
//https://www.truiton.com/2014/09/android-service-broadcastreceiver-example/
    private IntentFilter mInputIntentFilter;
    private BroadcastReceiver mInputReceiver;

    public static final String mInputBroadcastStringAction = "com.agathakuannewgmail.doubleservicetest.inputstring";
    public static final String mInputBroadcastIntAction = "com.agathakuannewgmail.doubleservicetest.inputint";
    public static final String mInputBroadcastArrayListAction = "com.agathakuannewgmail.doubleservicetest.inputarraylist";


mInputIntentFilter = new IntentFilter();
        mInputIntentFilter.addAction(mInputBroadcastStringAction);
        mInputIntentFilter.addAction(mInputBroadcastIntAction);
        mInputIntentFilter.addAction(mInputBroadcastArrayListAction);
        mInputReceiver = new inputserviceReceiver();

 class inputserviceReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
           if(intent.getAction().equals(mInputBroadcastIntAction))
            {
                Toast.makeText(context, "received:"+String.valueOf(intent.getIntExtra("Data", 0)), Toast.LENGTH_SHORT).show();

            }

        }
    }


inputService.java
Thread:listen to ShakeListener

 ShakeListener mShakeListener = null;

@Override
    public void onCreate() {
        super.onCreate();
        inputHandler = new Handler();
        mShakeListener = new ShakeListener(this);
        mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener()
        {
            public void onShake()
            {
                mShakeListener.stop();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent broadcast2OutputService = new Intent();
                        broadcast2OutputService.setAction(outputService.mInputBroadcastStringAction);
                        broadcast2OutputService.putExtra("Data","shaking!!");
                        sendBroadcast(broadcast2OutputService);

                        mShakeListener.start();

                    }
                },600);
            }
        });

    }

 @Override
    public void onDestroy() {
        if(mShakeListener != null)
        {
            mShakeListener.stop();
        }
        super.onDestroy();
    }



outputService.java
broadcast receiver of input service
private IntentFilter mInputIntentFilter;
    private BroadcastReceiver mInputReceiver;

    public static final String mInputBroadcastStringAction = "com.agathakuannewgmail.doubleservicetest.inputstring";

@Override
    public void onCreate() {
        super.onCreate();

        mInputIntentFilter = new IntentFilter();
        mInputIntentFilter.addAction(mInputBroadcastStringAction);
        mInputReceiver = new inputReceiver();

        //https://blog.csdn.net/jdsjlzx/article/details/84327815
        mNotificationHelper = new NotificationHelper(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed by Alex");

        registerReceiver(mInputReceiver, mInputIntentFilter);
        return super.onStartCommand(intent, flags, startId);
    }



    @Override
    public void onDestroy() {
        stopTimer();
        unregisterReceiver(mInputReceiver);
        super.onDestroy();

    }

 public class inputReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent.getAction().equals(mInputBroadcastStringAction))
            {
                Log.d("in inputReceiver",intent.getStringExtra("Data"));

                showNotification("From outputService",intent.getStringExtra("Data") );
            }
        }
    }


NotificationHelper.java
package com.agathakuannewgmail.doubleservicetest;


import android.content.ContextWrapper;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.graphics.Color;
//https://blog.csdn.net/jdsjlzx/article/details/84327815\
//https://github.com/XuMiaoLee/AndroidNotificationChannel

public class NotificationHelper extends ContextWrapper{

    private NotificationManager mNotificationManager;
    private NotificationChannel mNotificationChannel;

    public static final  String CHANNEL_ID          = "default";
    private static final String CHANNEL_NAME        = "Default Channel";
    private static final String CHANNEL_DESCRIPTION = "this is default channel!";

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
    private Date date = new Date(System.currentTimeMillis());


    public NotificationHelper(Context base)
    {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationChannel.setDescription(CHANNEL_DESCRIPTION);

            mNotificationChannel.enableLights(true);
            mNotificationChannel.setLightColor(Color.BLUE);
            mNotificationChannel.setShowBadge(true);
            mNotificationChannel.setBypassDnd(true);
            mNotificationChannel.setVibrationPattern(new long[]{100, 100, 200});
            mNotificationChannel.shouldShowLights();

            getNotificationManager().createNotificationChannel(mNotificationChannel);
        }
    }

    public NotificationCompat.Builder getNotification(String title, String content)
    {
        NotificationCompat.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        } else
        {
            builder = new NotificationCompat.Builder(this);
            builder.setPriority(NotificationCompat.PRIORITY_MAX);
        }
        builder.setContentTitle(title);
        builder.setContentText(content+" "+"at"+" "+simpleDateFormat.format(date));
        builder.setSmallIcon(R.mipmap.comments);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.comments));
        builder.setVibrate(new long[]{0,1000,1000,1000});
        builder.setLights(Color.BLUE, 1000,200);
        //builder.setDefaults(NotificationCompat.DEFAULT_ALL);

        builder.setAutoCancel(true);//点击自动删除通知

        return builder;
    }

    public void notify(int id, NotificationCompat.Builder builder)
    {
        if (getNotificationManager() != null)
        {
            getNotificationManager().notify(id, builder.build());
        }
    }

    private NotificationManager getNotificationManager()
    {
        if (mNotificationManager == null)
            mNotificationManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
        return mNotificationManager;
    }

}

ShakeListener.java
package com.agathakuannewgmail.doubleservicetest;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

public class ShakeListener implements SensorEventListener {
    private static final int SPEED_THERSHOLD = 3000;
    private static final int UPDATE_INTERVAL = 70;
    private SensorManager sensorManager;
    private Sensor sensor;
    private OnShakeListener onShakeListener;
    private Context mContext;

    private float last_x, last_y, last_z;
    private long update_time;



    public ShakeListener(Context c)
    {
        mContext = c;
        start();
    }

    public void start()
    {
        sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);

        if(sensorManager != null)
        {
            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }

        if(sensor != null)
        {
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
        }
    }

    public void stop()
    {
        sensorManager.unregisterListener(this);
    }

    public void setOnShakeListener(OnShakeListener listener)
    {
        onShakeListener = listener;
    }



    @Override
    public void onSensorChanged(SensorEvent event)
    {
        long currentUpdateTime = System.currentTimeMillis();
        long timeInterval = currentUpdateTime - update_time;

        if(timeInterval <UPDATE_INTERVAL)return;

        update_time = currentUpdateTime;

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        float deltaX = x -last_x;
        float deltaY = x -last_y;
        float deltaZ = x -last_z;

        last_x = x;
        last_y = y;
        last_z = z;

        double speed = Math.sqrt(deltaX*deltaX+ deltaY*deltaY+ deltaZ*deltaZ)/timeInterval*10000;

        Log.d("ShakeListener", String.valueOf(x)+String.valueOf(y)+String.valueOf(z));

        if(speed >= SPEED_THERSHOLD)
        {
            onShakeListener.onShake();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public interface OnShakeListener
    {
        public void onShake();
    }

}


REFERENCE:
https://blog.csdn.net/guolin_blog/article/details/11952435
https://blog.csdn.net/qq_31939617/article/details/80118302




2019年2月4日 星期一

Android : google BLE example mix GraphView plotting library

This article implement "BLE example MIX Graphview lib"
(resolvation of conflict Android API version)

0.Intention:
Generally speaking, almost every Andorid Plotting 3 party library, each release restricts to specified Android API version.

My application needs to combine BLE with Data-Plotting function.
Most time I use GraphView as plotting library, which is easy to modify.

GraphView latest release ver 4.2.2 run on API 27.1.1
but newest BLE example only update to API 27.0.2 
This example combine them together.


Files to Modify:
1.app/bulid.gradle (import graphView library)
downgrade graphView version to 4.0.0 





2.DeviceControlActivity.java
add in import statement


add in DeviceControlActivity extends Activity 


add in onCreate funciton


add initGraph function 


add in onResume ,
add getRandom (random y-axis as input)



3.gatt_services_characteristics.xml (for graph layout)


here is the result:


MOdify code:

app/build.gradle
buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
    }
}

apply plugin: 'com.android.application'

repositories {
    jcenter()
    google()
}

dependencies {
    compile "com.android.support:support-v4:27.0.2"
    compile "com.android.support:support-v13:27.0.2"
    compile "com.android.support:cardview-v7:27.0.2"
    compile "com.android.support:appcompat-v7:27.0.2"

    //#GraphView
    compile "com.jjoe64:graphview:4.0.0"
}

// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
    'main',     // main sample code; look here for the interesting stuff.
    'common',   // components that are reused by multiple samples
    'template'] // boilerplate code that is generated by the sample template process

android {
    compileSdkVersion 27

    buildToolsVersion "27.0.2"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 27
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            dirs.each { dir ->
                java.srcDirs "src/${dir}/java"
                res.srcDirs "src/${dir}/res"
            }
        }
        androidTest.setRoot('tests')
        androidTest.java.srcDirs = ['tests/src']

    }

}



DeviceControlActivity.java


/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.bluetoothlegatt;

import android.app.Activity;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//#graphView
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.GridLabelRenderer;

import java.util.Random;

/**
 * For a given BLE device, this Activity provides the user interface to connect, display data,
 * and display GATT services and characteristics supported by the device.  The Activity
 * communicates with {@code BluetoothLeService}, which in turn interacts with the
 * Bluetooth LE API.
 */
public class DeviceControlActivity extends Activity {
    private final static String TAG = DeviceControlActivity.class.getSimpleName();

    public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
    public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";

    private TextView mConnectionState;
    private TextView mDataField;
    private String mDeviceName;
    private String mDeviceAddress;
    private ExpandableListView mGattServicesList;
    private BluetoothLeService mBluetoothLeService;
    private ArrayList<ArrayList<BluetoothGattCharacteristic>> mGattCharacteristics =
            new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
    private boolean mConnected = false;
    private BluetoothGattCharacteristic mNotifyCharacteristic;

    private final String LIST_NAME = "NAME";
    private final String LIST_UUID = "UUID";

    //#graphView
    private final Handler mHandler = new Handler();
    private Runnable mTimer;
    private double graphLastXValue = 5d;
    private LineGraphSeries<DataPoint> mSeries;

    // Code to manage Service lifecycle.
    private final ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
            if (!mBluetoothLeService.initialize()) {
                Log.e(TAG, "Unable to initialize Bluetooth");
                finish();
            }
            // Automatically connects to the device upon successful start-up initialization.
            mBluetoothLeService.connect(mDeviceAddress);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mBluetoothLeService = null;
        }
    };

    // Handles various events fired by the Service.
    // ACTION_GATT_CONNECTED: connected to a GATT server.
    // ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
    // ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
    // ACTION_DATA_AVAILABLE: received data from the device.  This can be a result of read
    //                        or notification operations.
    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
                mConnected = true;
                updateConnectionState(R.string.connected);
                invalidateOptionsMenu();
            } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
                mConnected = false;
                updateConnectionState(R.string.disconnected);
                invalidateOptionsMenu();
                clearUI();
            } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
                // Show all the supported services and characteristics on the user interface.
                displayGattServices(mBluetoothLeService.getSupportedGattServices());
            } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
                displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
            }
        }
    };

    // If a given GATT characteristic is selected, check for supported features.  This sample
    // demonstrates 'Read' and 'Notify' features.  See
    // http://d.android.com/reference/android/bluetooth/BluetoothGatt.html for the complete
    // list of supported characteristic features.
    private final ExpandableListView.OnChildClickListener servicesListClickListner =
            new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                            int childPosition, long id) {
                    if (mGattCharacteristics != null) {
                        final BluetoothGattCharacteristic characteristic =
                                mGattCharacteristics.get(groupPosition).get(childPosition);
                        final int charaProp = characteristic.getProperties();
                        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                            // If there is an active notification on a characteristic, clear
                            // it first so it doesn't update the data field on the user interface.
                            if (mNotifyCharacteristic != null) {
                                mBluetoothLeService.setCharacteristicNotification(
                                        mNotifyCharacteristic, false);
                                mNotifyCharacteristic = null;
                            }
                            mBluetoothLeService.readCharacteristic(characteristic);
                        }
                        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                            mNotifyCharacteristic = characteristic;
                            mBluetoothLeService.setCharacteristicNotification(
                                    characteristic, true);
                        }
                        return true;
                    }
                    return false;
                }
    };

    private void clearUI() {
        mGattServicesList.setAdapter((SimpleExpandableListAdapter) null);
        mDataField.setText(R.string.no_data);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gatt_services_characteristics);

        final Intent intent = getIntent();
        mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
        mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);

        // Sets up UI references.
        ((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress);
        mGattServicesList = (ExpandableListView) findViewById(R.id.gatt_services_list);
        mGattServicesList.setOnChildClickListener(servicesListClickListner);
        mConnectionState = (TextView) findViewById(R.id.connection_state);
        mDataField = (TextView) findViewById(R.id.data_value);

        getActionBar().setTitle(mDeviceName);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
        bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

        //#graphView https://github.com/jjoe64/GraphView
        GraphView graph = (GraphView) findViewById(R.id.graph);
        initGraph(graph);
    }

    public void initGraph(GraphView graph)
    {
        graph.getViewport().setXAxisBoundsManual(true);

        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(4);

        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(0);
        graph.getViewport().setMaxY(42);

        graph.getViewport().setScrollable(true); // enables horizontal scrolling
        graph.getViewport().setScalable(true); // enables horizontal zooming and scrolling


        //graph.getGridLabelRenderer().setLabelVerticalWidth(100);
        GridLabelRenderer gridLabel = graph.getGridLabelRenderer();
        gridLabel.setHorizontalAxisTitle("x axis");
        gridLabel.setVerticalAxisTitle("y axis");

        // first mSeries is a line
        mSeries = new LineGraphSeries<>();
        mSeries.setDrawDataPoints(true);
        mSeries.setDrawBackground(true);
        graph.addSeries(mSeries);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
        if (mBluetoothLeService != null) {
            final boolean result = mBluetoothLeService.connect(mDeviceAddress);
            Log.d(TAG, "Connect request result=" + result);
        }

        //#graphView
        mTimer = new Runnable()
        {
            @Override
            public void run()
            {
                graphLastXValue += 1.0;

                /*mSeries.appendData(new DataPoint(graphLastXValue, getRandom()+speed_num),
                        true, 22);*/

                mSeries.appendData(new DataPoint(graphLastXValue, getRandom()),
                        true, 22);
                mHandler.postDelayed(this, 330);
            }
        };
        mHandler.postDelayed(mTimer, 1500);
    }

    //#graphView
    double mLastRandom = 2;
    Random mRand = new Random();
    private double getRandom()
    {
        mLastRandom += mRand.nextDouble()*0.05 - 0.05;
        return mLastRandom ;
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mGattUpdateReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
        mBluetoothLeService = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gatt_services, menu);
        if (mConnected) {
            menu.findItem(R.id.menu_connect).setVisible(false);
            menu.findItem(R.id.menu_disconnect).setVisible(true);
        } else {
            menu.findItem(R.id.menu_connect).setVisible(true);
            menu.findItem(R.id.menu_disconnect).setVisible(false);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_connect:
                mBluetoothLeService.connect(mDeviceAddress);
                return true;
            case R.id.menu_disconnect:
                mBluetoothLeService.disconnect();
                return true;
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateConnectionState(final int resourceId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mConnectionState.setText(resourceId);
            }
        });
    }

    private void displayData(String data) {
        if (data != null) {
            mDataField.setText(data);
        }
    }

    // Demonstrates how to iterate through the supported GATT Services/Characteristics.
    // In this sample, we populate the data structure that is bound to the ExpandableListView
    // on the UI.
    private void displayGattServices(List<BluetoothGattService> gattServices) {
        if (gattServices == null) return;
        String uuid = null;
        String unknownServiceString = getResources().getString(R.string.unknown_service);
        String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
        ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
        ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
                = new ArrayList<ArrayList<HashMap<String, String>>>();
        mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

        // Loops through available GATT Services.
        for (BluetoothGattService gattService : gattServices) {
            HashMap<String, String> currentServiceData = new HashMap<String, String>();
            uuid = gattService.getUuid().toString();
            currentServiceData.put(
                    LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
            currentServiceData.put(LIST_UUID, uuid);
            gattServiceData.add(currentServiceData);

            ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
                    new ArrayList<HashMap<String, String>>();
            List<BluetoothGattCharacteristic> gattCharacteristics =
                    gattService.getCharacteristics();
            ArrayList<BluetoothGattCharacteristic> charas =
                    new ArrayList<BluetoothGattCharacteristic>();

            // Loops through available Characteristics.
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                charas.add(gattCharacteristic);
                HashMap<String, String> currentCharaData = new HashMap<String, String>();
                uuid = gattCharacteristic.getUuid().toString();
                currentCharaData.put(
                        LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
                currentCharaData.put(LIST_UUID, uuid);
                gattCharacteristicGroupData.add(currentCharaData);
            }
            mGattCharacteristics.add(charas);
            gattCharacteristicData.add(gattCharacteristicGroupData);
        }

        SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(
                this,
                gattServiceData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] {LIST_NAME, LIST_UUID},
                new int[] { android.R.id.text1, android.R.id.text2 },
                gattCharacteristicData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] {LIST_NAME, LIST_UUID},
                new int[] { android.R.id.text1, android.R.id.text2 }
        );
        mGattServicesList.setAdapter(gattServiceAdapter);
    }

    private static IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        return intentFilter;
    }
}


gatt_services_characteristics.xml 


<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
    <LinearLayout android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="10dp">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/label_device_address"
                  android:textSize="18sp"/>
        <Space android:layout_width="5dp"
               android:layout_height="wrap_content"/>
        <TextView android:id="@+id/device_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="10dp">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/label_state"
                  android:textSize="18sp"/>
        <Space android:layout_width="5dp"
               android:layout_height="wrap_content"/>
        <TextView android:id="@+id/connection_state"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="@string/disconnected"
                  android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="10dp">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/label_data"
                  android:textSize="18sp"/>
        <Space android:layout_width="5dp"
               android:layout_height="wrap_content"/>
        <TextView android:id="@+id/data_value"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="@string/no_data"
                  android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.jjoe64.graphview.GraphView
            android:id="@+id/graph"
            android:layout_width="match_parent"
            android:layout_height="180dp" />

    </LinearLayout>
    <ExpandableListView android:id="@+id/gatt_services_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

 

ref:
1. https://github.com/jjoe64/GraphView
2. https://github.com/halfhp/androidplot/blob/master/demoapp/src/main/java/com/androidplot/demos/SimpleXYPlotActivity.java
3.https://mvnrepository.com/artifact/com.jjoe64/graphview