Small refactoring of BtLE actions
This commit is contained in:
parent
4389c1cca3
commit
804a85d31f
|
@ -16,6 +16,7 @@ import java.util.Set;
|
|||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.CheckInitializedAction;
|
||||
|
||||
/**
|
||||
* Abstract base class for all devices connected through Bluetooth Low Energy (LE) aka
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.service.btle;
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.btle.actions;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbortTransactionAction;
|
||||
|
||||
/**
|
||||
* A special action that is executed at the very front of the initialization
|
|
@ -0,0 +1,29 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.service.btle.actions;
|
||||
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
|
||||
public abstract class ConditionalWriteAction extends WriteAction {
|
||||
public ConditionalWriteAction(BluetoothGattCharacteristic characteristic) {
|
||||
super(characteristic, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean writeValue(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value) {
|
||||
byte[] conditionalValue = checkCondition();
|
||||
if (conditionalValue != null) {
|
||||
return super.writeValue(gatt, characteristic, conditionalValue);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the condition whether the write shall happen or not.
|
||||
* Returns the actual value to be written or null in case nothing shall be written.
|
||||
*
|
||||
* Note that returning null will not cause run() to return false, in other words,
|
||||
* the rest of the queue will still be executed.
|
||||
* @return the value to be written or null to not write anything
|
||||
*/
|
||||
protected abstract byte[] checkCondition();
|
||||
}
|
|
@ -22,16 +22,26 @@ public class WriteAction extends BtLEAction {
|
|||
|
||||
@Override
|
||||
public boolean run(BluetoothGatt gatt) {
|
||||
int properties = getCharacteristic().getProperties();
|
||||
BluetoothGattCharacteristic characteristic = getCharacteristic();
|
||||
int properties = characteristic.getProperties();
|
||||
//TODO: expectsResult should return false if PROPERTY_WRITE_NO_RESPONSE is true, but this yelds to timing issues
|
||||
if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 || ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0)) {
|
||||
if (getCharacteristic().setValue(value)) {
|
||||
return gatt.writeCharacteristic(getCharacteristic());
|
||||
}
|
||||
return writeValue(gatt, characteristic, value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean writeValue(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value) {
|
||||
if (characteristic.setValue(value)) {
|
||||
return gatt.writeCharacteristic(characteristic);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected final byte[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean expectsResult() {
|
||||
return true;
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband;
|
||||
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.PlainAction;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbortTransactionAction;
|
||||
|
||||
public class CheckAuthenticationNeededAction extends PlainAction {
|
||||
public class CheckAuthenticationNeededAction extends AbortTransactionAction {
|
||||
private final GBDevice mDevice;
|
||||
|
||||
public CheckAuthenticationNeededAction(GBDevice device) {
|
||||
|
@ -14,14 +12,14 @@ public class CheckAuthenticationNeededAction extends PlainAction {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean run(BluetoothGatt gatt) {
|
||||
protected boolean shouldAbort() {
|
||||
// the state is set in MiBandSupport.handleNotificationNotif()
|
||||
switch (mDevice.getState()) {
|
||||
case AUTHENTICATION_REQUIRED: // fall through
|
||||
case AUTHENTICATING:
|
||||
return false; // abort the whole thing
|
||||
return true; // abort the whole thing
|
||||
default:
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue