1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-26 09:37:33 +01:00

Mijia LYWSD: Make support class generic

This commit is contained in:
José Rebelo 2023-12-22 12:26:15 +00:00
parent 4c0750e75a
commit 149b6236a0
3 changed files with 18 additions and 101 deletions

View File

@ -15,9 +15,8 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd02; package nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
@ -28,15 +27,11 @@ import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.mijia_lywsd02.MijiaLywsd02Support; import nodomain.freeyourgadget.gadgetbridge.service.devices.mijia_lywsd.MijiaLywsdSupport;
public class MijiaLywsd02Coordinator extends AbstractBLEDeviceCoordinator { public class MijiaLywsd02Coordinator extends AbstractBLEDeviceCoordinator {
@Override @Override
@ -49,11 +44,6 @@ public class MijiaLywsd02Coordinator extends AbstractBLEDeviceCoordinator {
return BONDING_STYLE_NONE; return BONDING_STYLE_NONE;
} }
@Override
public Class<? extends Activity> getPairingActivity() {
return null;
}
@Override @Override
public InstallHandler findInstallHandler(Uri uri, Context context) { public InstallHandler findInstallHandler(Uri uri, Context context) {
return null; return null;
@ -64,75 +54,15 @@ public class MijiaLywsd02Coordinator extends AbstractBLEDeviceCoordinator {
return false; return false;
} }
@Override
public boolean supportsActivityTracking() {
return false;
}
@Override
public SampleProvider<? extends ActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
return null;
}
@Override
public boolean supportsScreenshots() {
return false;
}
@Override
public int getAlarmSlotCount(GBDevice device) {
return 0;
}
@Override
public boolean supportsSmartWakeup(GBDevice device) {
return false;
}
@Override
public boolean supportsHeartRateMeasurement(GBDevice device) {
return false;
}
@Override @Override
public String getManufacturer() { public String getManufacturer() {
return "Xiaomi"; return "Xiaomi";
} }
@Override
public boolean supportsAppsManagement(final GBDevice device) {
return false;
}
@Override
public Class<? extends Activity> getAppsManagementActivity() {
return null;
}
@Override
public boolean supportsCalendarEvents() {
return false;
}
@Override
public boolean supportsRealtimeData() {
return false;
}
@Override
public boolean supportsWeather() {
return false;
}
@Override
public boolean supportsFindDevice() {
return false;
}
@NonNull @NonNull
@Override @Override
public Class<? extends DeviceSupport> getDeviceSupportClass() { public Class<? extends DeviceSupport> getDeviceSupportClass() {
return MijiaLywsd02Support.class; return MijiaLywsdSupport.class;
} }
@Override @Override
@ -147,13 +77,11 @@ public class MijiaLywsd02Coordinator extends AbstractBLEDeviceCoordinator {
// nothing to delete, yet // nothing to delete, yet
} }
@Override @Override
public int getDeviceNameResource() { public int getDeviceNameResource() {
return R.string.devicetype_mijia_lywsd02; return R.string.devicetype_mijia_lywsd02;
} }
@Override @Override
public int getDefaultIconResource() { public int getDefaultIconResource() {
return R.drawable.ic_device_pebble; return R.drawable.ic_device_pebble;

View File

@ -109,7 +109,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.lenovo.watchxplus.WatchXPlus
import nodomain.freeyourgadget.gadgetbridge.devices.liveview.LiveviewCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.liveview.LiveviewCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.makibeshr3.MakibesHR3Coordinator; import nodomain.freeyourgadget.gadgetbridge.devices.makibeshr3.MakibesHR3Coordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd02.MijiaLywsd02Coordinator; import nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd.MijiaLywsd02Coordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miscale2.MiScale2DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.miscale2.MiScale2DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.no1f1.No1F1Coordinator; import nodomain.freeyourgadget.gadgetbridge.devices.no1f1.No1F1Coordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.nothing.Ear1Coordinator; import nodomain.freeyourgadget.gadgetbridge.devices.nothing.Ear1Coordinator;

View File

@ -1,5 +1,5 @@
/* Copyright (C) 2016-2021 Andreas Shimokawa, Carsten Pfeiffer, Sebastian /* Copyright (C) 2016-2021 Andreas Shimokawa, Carsten Pfeiffer, Sebastian
Kranz, Davis Mosenkovs Kranz, Davis Mosenkovs, José Rebelo
This file is part of Gadgetbridge. This file is part of Gadgetbridge.
@ -15,19 +15,17 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.mijia_lywsd02; package nodomain.freeyourgadget.gadgetbridge.service.devices.mijia_lywsd;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.widget.Toast; import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.UUID; import java.util.UUID;
@ -37,32 +35,23 @@ import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSett
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
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.btle.profiles.IntentListener; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.IntentListener;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.battery.BatteryInfoProfile;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class MijiaLywsd02Support extends AbstractBTLEDeviceSupport { public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(MijiaLywsd02Support.class); private static final Logger LOG = LoggerFactory.getLogger(MijiaLywsdSupport.class);
private static final UUID UUID_TIME = UUID.fromString("ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6"); private static final UUID UUID_TIME = UUID.fromString("ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6");
private static final UUID UUID_BATTERY = UUID.fromString("ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6"); private static final UUID UUID_BATTERY = UUID.fromString("ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6");
private static final UUID UUID_SCALE = UUID.fromString("ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6"); private static final UUID UUID_SCALE = UUID.fromString("ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6");
private static final UUID UUID_CONN_INTERVAL = UUID.fromString("ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6"); private static final UUID UUID_CONN_INTERVAL = UUID.fromString("ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6");
private final DeviceInfoProfile<MijiaLywsd02Support> deviceInfoProfile; private final DeviceInfoProfile<MijiaLywsdSupport> deviceInfoProfile;
private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo(); private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();
private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo(); private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo();
private final IntentListener mListener = new IntentListener() { private final IntentListener mListener = new IntentListener() {
@ -75,7 +64,7 @@ public class MijiaLywsd02Support extends AbstractBTLEDeviceSupport {
} }
}; };
public MijiaLywsd02Support() { public MijiaLywsdSupport() {
super(LOG); super(LOG);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS); addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE); addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
@ -102,7 +91,7 @@ public class MijiaLywsd02Support extends AbstractBTLEDeviceSupport {
} }
private void setTime(TransactionBuilder builder) { private void setTime(TransactionBuilder builder) {
BluetoothGattCharacteristic timeCharacteristc = getCharacteristic(MijiaLywsd02Support.UUID_TIME); BluetoothGattCharacteristic timeCharacteristc = getCharacteristic(MijiaLywsdSupport.UUID_TIME);
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
byte offsetHours = (byte) (SimpleTimeZone.getDefault().getOffset(ts) / (1000 * 60 * 60)); byte offsetHours = (byte) (SimpleTimeZone.getDefault().getOffset(ts) / (1000 * 60 * 60));
ts = (ts + 250 + 500) / 1000; // round to seconds with +250 ms to compensate for BLE connection interval ts = (ts + 250 + 500) / 1000; // round to seconds with +250 ms to compensate for BLE connection interval
@ -115,17 +104,17 @@ public class MijiaLywsd02Support extends AbstractBTLEDeviceSupport {
} }
private void setConnectionInterval(TransactionBuilder builder) { private void setConnectionInterval(TransactionBuilder builder) {
BluetoothGattCharacteristic intervalCharacteristc = getCharacteristic(MijiaLywsd02Support.UUID_CONN_INTERVAL); BluetoothGattCharacteristic intervalCharacteristc = getCharacteristic(MijiaLywsdSupport.UUID_CONN_INTERVAL);
builder.write(intervalCharacteristc, new byte[]{(byte) 0xf4, (byte) 0x01}); // maximum interval of 500 ms builder.write(intervalCharacteristc, new byte[]{(byte) 0xf4, (byte) 0x01}); // maximum interval of 500 ms
} }
private void getBatteryInfo(TransactionBuilder builder) { private void getBatteryInfo(TransactionBuilder builder) {
BluetoothGattCharacteristic batteryCharacteristc = getCharacteristic(MijiaLywsd02Support.UUID_BATTERY); BluetoothGattCharacteristic batteryCharacteristc = getCharacteristic(MijiaLywsdSupport.UUID_BATTERY);
builder.read(batteryCharacteristc); builder.read(batteryCharacteristc);
} }
private void setTemperatureScale(TransactionBuilder builder, String scale) { private void setTemperatureScale(TransactionBuilder builder, String scale) {
BluetoothGattCharacteristic scaleCharacteristc = getCharacteristic(MijiaLywsd02Support.UUID_SCALE); BluetoothGattCharacteristic scaleCharacteristc = getCharacteristic(MijiaLywsdSupport.UUID_SCALE);
builder.write(scaleCharacteristc, new byte[]{(byte) ("f".equals(scale) ? 0x01 : 0xff)}); builder.write(scaleCharacteristc, new byte[]{(byte) ("f".equals(scale) ? 0x01 : 0xff)});
} }
@ -191,7 +180,7 @@ public class MijiaLywsd02Support extends AbstractBTLEDeviceSupport {
} }
UUID characteristicUUID = characteristic.getUuid(); UUID characteristicUUID = characteristic.getUuid();
if(MijiaLywsd02Support.UUID_BATTERY.equals(characteristicUUID)) { if (MijiaLywsdSupport.UUID_BATTERY.equals(characteristicUUID)) {
handleBatteryInfo(characteristic.getValue(), status); handleBatteryInfo(characteristic.getValue(), status);
return true; return true;
} }