1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-09-11 00:36:32 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java

98 lines
3.1 KiB
Java
Raw Normal View History

package nodomain.freeyourgadget.gadgetbridge.service.btle;
import android.bluetooth.BluetoothGattCharacteristic;
import android.support.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.NotifyAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ReadAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction;
public class TransactionBuilder {
private static final Logger LOG = LoggerFactory.getLogger(TransactionBuilder.class);
2015-11-23 23:04:46 +01:00
private final Transaction mTransaction;
private boolean mQueued;
2015-04-19 11:28:03 +02:00
public TransactionBuilder(String taskName) {
mTransaction = new Transaction(taskName);
}
2015-04-19 11:28:03 +02:00
public TransactionBuilder read(BluetoothGattCharacteristic characteristic) {
if (characteristic == null) {
LOG.warn("Unable to read characteristic: null");
return this;
}
ReadAction action = new ReadAction(characteristic);
return add(action);
}
2015-04-19 11:28:03 +02:00
public TransactionBuilder write(BluetoothGattCharacteristic characteristic, byte[] data) {
if (characteristic == null) {
LOG.warn("Unable to write characteristic: null");
return this;
}
WriteAction action = new WriteAction(characteristic, data);
return add(action);
}
2015-04-19 11:28:03 +02:00
public TransactionBuilder notify(BluetoothGattCharacteristic characteristic, boolean enable) {
if (characteristic == null) {
LOG.warn("Unable to notify characteristic: null");
return this;
}
NotifyAction action = createNotifyAction(characteristic, enable);
return add(action);
}
protected NotifyAction createNotifyAction(BluetoothGattCharacteristic characteristic, boolean enable) {
return new NotifyAction(characteristic, enable);
}
public TransactionBuilder wait(int millis) {
WaitAction action = new WaitAction(millis);
return add(action);
}
2015-04-19 11:28:03 +02:00
public TransactionBuilder add(BtLEAction action) {
mTransaction.add(action);
return this;
}
2015-04-19 11:28:03 +02:00
/**
* Sets a GattCallback instance that will be called when the transaction is executed,
* resulting in GattCallback events.
*
* @param callback the callback to set, may be null
*/
public void setGattCallback(@Nullable GattCallback callback) {
mTransaction.setGattCallback(callback);
}
public
@Nullable
GattCallback getGattCallback() {
return mTransaction.getGattCallback();
}
/**
* To be used as the final step to execute the transaction by the given queue.
2015-04-19 11:28:03 +02:00
*
* @param queue
*/
public void queue(BtLEQueue queue) {
if (mQueued) {
throw new IllegalStateException("This builder had already been queued. You must not reuse it.");
}
mQueued = true;
queue.add(mTransaction);
}
public Transaction getTransaction() {
return mTransaction;
}
}