Android Open Source - BLE-Heart-rate-variability-demo Bluetooth Gatt Executor






From Project

Back to project page BLE-Heart-rate-variability-demo.

License

The source code is released under:

MIT License

If you think the Android project BLE-Heart-rate-variability-demo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sample.hrv;
/*  ww  w  .  j a  v  a 2  s  . c  om*/
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothProfile;

import java.util.LinkedList;

import com.sample.hrv.sensor.BleSensor;


/**
 * Created by steven on 9/3/13.
 * Modified by olli on 3/28/2014.
 */
public class BluetoothGattExecutor extends BluetoothGattCallback {

    public interface ServiceAction {
        public static final ServiceAction NULL = new ServiceAction() {
            @Override
            public boolean execute(BluetoothGatt bluetoothGatt) {
                // it is null action. do nothing.
                return true;
            }
        };

        /***
         * Executes action.
         * @param bluetoothGatt
         * @return true - if action was executed instantly. false if action is waiting for
         *         feedback.
         */
        public boolean execute(BluetoothGatt bluetoothGatt);
    }

    private final LinkedList<BluetoothGattExecutor.ServiceAction> queue = new LinkedList<ServiceAction>();
    private volatile ServiceAction currentAction;

    public void update(final BleSensor sensor) {
        queue.add(sensor.update());
    }

    public void enable(BleSensor sensor, boolean enable) {
        final ServiceAction[] actions = sensor.enable(enable);
        for ( ServiceAction action : actions ) {
            this.queue.add(action);
        }
    }

    public void execute(BluetoothGatt gatt) {
        if (currentAction != null)
            return;

        boolean next = !queue.isEmpty();
        while (next) {
            final BluetoothGattExecutor.ServiceAction action = queue.pop();
            currentAction = action;
            if (!action.execute(gatt))
                break;

            currentAction = null;
            next = !queue.isEmpty();
        }
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);

        currentAction = null;
        execute(gatt);
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        currentAction = null;
        execute(gatt);
    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            queue.clear();
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        currentAction = null;
        execute(gatt);
    }
}




Java Source Code List

com.sample.hrv.BleService.java
com.sample.hrv.BluetoothGattExecutor.java
com.sample.hrv.DeviceScanActivity.java
com.sample.hrv.DeviceServicesActivity.java
com.sample.hrv.adapters.BleDevicesAdapter.java
com.sample.hrv.adapters.BleServicesAdapter.java
com.sample.hrv.demo.DemoGLSurfaceView.java
com.sample.hrv.demo.DemoHeartRateSensorActivity.java
com.sample.hrv.demo.DemoSensorActivity.java
com.sample.hrv.info.BleDeviceInfoService.java
com.sample.hrv.info.BleGapService.java
com.sample.hrv.info.BleGattService.java
com.sample.hrv.info.BleInfoService.java
com.sample.hrv.info.BleInfoServices.java
com.sample.hrv.sensor.BleHeartRateSensor.java
com.sample.hrv.sensor.BleSensorUtils.java
com.sample.hrv.sensor.BleSensor.java
com.sample.hrv.sensor.BleSensors.java
com.sample.hrv.sensor.BleTestSensor.java