Back to project page BLE-Heart-rate-variability-demo.
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.
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); } }