1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-10 23:44:05 +02: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.GattService;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; 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.GetBatteryLevelRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetFirmwareInfoRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetFirmwareInfoRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.Request; import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.Request;
@ -73,7 +74,7 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
// Enable notification // Enable notification
builder.notify(getCharacteristic(LefunConstants.UUID_CHARACTERISTIC_LEFUN_NOTIFY), true); 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 { try {
GetFirmwareInfoRequest firmwareReq = new GetFirmwareInfoRequest(this, builder); GetFirmwareInfoRequest firmwareReq = new GetFirmwareInfoRequest(this, builder);
firmwareReq.perform(); firmwareReq.perform();
@ -111,7 +112,16 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetTime() { 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 @Override
@ -196,7 +206,18 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onFindDevice(boolean start) { 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 @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; package nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.AbstractBTLEOperation;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.LefunDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.LefunDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
// Ripped from nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Request // Ripped from nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Request
public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport> { public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport> {
private Logger logger = (Logger) LoggerFactory.getLogger(getName());
protected TransactionBuilder builder; protected TransactionBuilder builder;
private Logger logger = (Logger) LoggerFactory.getLogger(getName());
protected Request(LefunDeviceSupport support, TransactionBuilder builder) { protected Request(LefunDeviceSupport support, TransactionBuilder builder) {
super(support); super(support);
@ -50,6 +52,7 @@ public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport>
public abstract byte[] createRequest(); public abstract byte[] createRequest();
public void handleResponse(byte[] data) { public void handleResponse(byte[] data) {
} }
public String getName() { public String getName() {
@ -69,6 +72,6 @@ public abstract class Request extends AbstractBTLEOperation<LefunDeviceSupport>
} }
protected void reportFailure(String message) { protected void reportFailure(String message) {
// TODO: Toast here or something GB.toast(getContext(), message, Toast.LENGTH_SHORT, GB.ERROR);
} }
} }