1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 01:55:50 +01:00

Lefun: Add find device support, toast on error

This commit is contained in:
Yukai Li 2020-10-04 21:19:38 -06:00 committed by Gitea
parent 9454684974
commit 61039f1749
3 changed files with 61 additions and 5 deletions

View File

@ -46,6 +46,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSuppo
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.FindDeviceRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetBatteryLevelRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetFirmwareInfoRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.Request;
@ -73,7 +74,7 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
// Enable notification
builder.notify(getCharacteristic(LefunConstants.UUID_CHARACTERISTIC_LEFUN_NOTIFY), true);
// Init device (just get version and battery)
// Init device (get version info, battery level, and set time)
try {
GetFirmwareInfoRequest firmwareReq = new GetFirmwareInfoRequest(this, builder);
firmwareReq.perform();
@ -111,7 +112,16 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
@Override
public void onSetTime() {
try {
TransactionBuilder builder = createTransactionBuilder(SetTimeRequest.class.getSimpleName());
SetTimeRequest request = new SetTimeRequest(this, builder);
request.perform();
inProgressRequests.add(request);
builder.queue(getQueue());
} catch (IOException e) {
GB.toast(getContext(), "Failed to set time", Toast.LENGTH_SHORT,
GB.ERROR, e);
}
}
@Override
@ -196,7 +206,18 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
@Override
public void onFindDevice(boolean start) {
if (start) {
try {
TransactionBuilder builder = createTransactionBuilder(FindDeviceRequest.class.getSimpleName());
FindDeviceRequest request = new FindDeviceRequest(this, builder);
request.perform();
inProgressRequests.add(request);
builder.queue(getQueue());
} catch (IOException e) {
GB.toast(getContext(), "Failed to initiate find device", Toast.LENGTH_SHORT,
GB.ERROR, e);
}
}
}
@Override

View File

@ -0,0 +1,32 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests;
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.LefunConstants;
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.FindDeviceCommand;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.LefunDeviceSupport;
public class FindDeviceRequest extends Request {
public FindDeviceRequest(LefunDeviceSupport support, TransactionBuilder builder) {
super(support, builder);
}
@Override
public byte[] createRequest() {
FindDeviceCommand cmd = new FindDeviceCommand();
return cmd.serialize();
}
@Override
public int getCommandId() {
return LefunConstants.CMD_FIND_DEVICE;
}
@Override
public void handleResponse(byte[] data) {
FindDeviceCommand cmd = new FindDeviceCommand();
cmd.deserialize(data);
if (!cmd.isSuccess())
reportFailure("Could not initiate find device");
}
}

View File

@ -19,6 +19,7 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests;
import android.bluetooth.BluetoothGattCharacteristic;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,11 +30,12 @@ import nodomain.freeyourgadget.gadgetbridge.devices.lefun.LefunConstants;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.LefunDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
// Ripped from nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Request
public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport> {
private Logger logger = (Logger) LoggerFactory.getLogger(getName());
protected TransactionBuilder builder;
private Logger logger = (Logger) LoggerFactory.getLogger(getName());
protected Request(LefunDeviceSupport support, TransactionBuilder builder) {
super(support);
@ -50,6 +52,7 @@ public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport>
public abstract byte[] createRequest();
public void handleResponse(byte[] data) {
}
public String getName() {
@ -69,6 +72,6 @@ public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport>
}
protected void reportFailure(String message) {
// TODO: Toast here or something
GB.toast(getContext(), message, Toast.LENGTH_SHORT, GB.ERROR);
}
}