2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 115ek, Andreas Böhler, Andreas Shimokawa, Andrew
|
|
|
|
Watkins, angelpup, Carsten Pfeiffer, Cre3per, criogenic, DanialHanif, Daniel
|
|
|
|
Dakhno, Daniele Gobbetti, Daniel Thompson, Da Pa, Dmytro Bielik, Frank Ertl,
|
|
|
|
GeekosaurusR3x, Gordon Williams, Jean-François Greffier, jfgreffier, jhey,
|
|
|
|
João Paulo Barraca, Jochen S, Johannes Krude, José Rebelo, ladbsoft,
|
|
|
|
Lesur Frederic, mamucho, Manuel Ruß, maxirnilian, mkusnierz, narektor,
|
|
|
|
Noodlez, odavo32nof, opavlov, pangwalla, Pavel Elagin, Petr Kadlec, Petr
|
|
|
|
Vaněk, protomors, Quallenauge, Quang Ngô, Raghd Hamzeh, Sami Alaoui,
|
|
|
|
Sebastian Kranz, sedy89, Sergey Trofimov, Sophanimus, Stefan Bora, Taavi
|
|
|
|
Eomäe, thermatk, tiparega, Vadim Kaushan, x29a, xaos, Yukai Li
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-08-04 01:01:14 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.content.Context;
|
2023-08-20 04:14:06 +02:00
|
|
|
import android.util.Log;
|
2015-08-04 01:01:14 +02:00
|
|
|
import android.widget.Toast;
|
2023-08-20 04:14:06 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2023-08-20 04:14:06 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2017-01-23 01:08:36 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport;
|
2023-08-20 04:14:06 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
Garmin Vivomove HR support
- communication protocols
- device support implementation
- download FIT file storage
Features:
- basic connectivity: time sync, battery status, HW/FW version info
- real-time activity tracking
- fitness data sync
- find the device, find the phone
- factory reset
Features implemented but not working:
- notifications: fully implemented, seem to communicate correctly, but not shown on watch
Features implemented partially (not expected to work now):
- weather information (and in future possibly weather alerts)
- music info
- firmware update: only the initial file upload implemented, not used
Things to improve/change:
- Device name hardcoded in `VivomoveHrCoordinator.getSupportedType`, service UUIDs not available
- Download FIT file storage: Should be store (and offer the user to export?) the FIT data forever?
- Obviously, various code improvements, cleanup, etc.
2023-06-15 17:47:42 +02:00
|
|
|
import java.lang.reflect.Constructor;
|
2023-08-20 04:14:06 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.EnumSet;
|
Garmin Vivomove HR support
- communication protocols
- device support implementation
- download FIT file storage
Features:
- basic connectivity: time sync, battery status, HW/FW version info
- real-time activity tracking
- fitness data sync
- find the device, find the phone
- factory reset
Features implemented but not working:
- notifications: fully implemented, seem to communicate correctly, but not shown on watch
Features implemented partially (not expected to work now):
- weather information (and in future possibly weather alerts)
- music info
- firmware update: only the initial file upload implemented, not used
Things to improve/change:
- Device name hardcoded in `VivomoveHrCoordinator.getSupportedType`, service UUIDs not available
- Download FIT file storage: Should be store (and offer the user to export?) the FIT data forever?
- Obviously, various code improvements, cleanup, etc.
2023-06-15 17:47:42 +02:00
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
public class DeviceSupportFactory {
|
2023-08-20 04:14:06 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DeviceSupportFactory.class);
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
private final BluetoothAdapter mBtAdapter;
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Context mContext;
|
2015-08-04 01:01:14 +02:00
|
|
|
|
2019-08-02 23:49:14 +02:00
|
|
|
DeviceSupportFactory(Context context) {
|
2015-08-04 01:01:14 +02:00
|
|
|
mContext = context;
|
|
|
|
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
public synchronized DeviceSupport createDeviceSupport(GBDevice device) throws GBException {
|
2019-08-02 23:49:14 +02:00
|
|
|
DeviceSupport deviceSupport;
|
2015-12-13 00:43:07 +01:00
|
|
|
String deviceAddress = device.getAddress();
|
2015-08-22 01:08:46 +02:00
|
|
|
int indexFirstColon = deviceAddress.indexOf(":");
|
|
|
|
if (indexFirstColon > 0) {
|
|
|
|
if (indexFirstColon == deviceAddress.lastIndexOf(":")) { // only one colon
|
2015-12-13 00:43:07 +01:00
|
|
|
deviceSupport = createTCPDeviceSupport(device);
|
2015-08-22 01:08:46 +02:00
|
|
|
} else {
|
|
|
|
// multiple colons -- bt?
|
2015-12-13 00:43:07 +01:00
|
|
|
deviceSupport = createBTDeviceSupport(device);
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
2015-08-19 00:03:52 +02:00
|
|
|
} else {
|
2015-08-22 01:08:46 +02:00
|
|
|
// no colon at all, maybe a class name?
|
2015-12-13 00:43:07 +01:00
|
|
|
deviceSupport = createClassNameDeviceSupport(device);
|
2015-08-19 00:03:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
if (deviceSupport != null) {
|
|
|
|
return deviceSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no device found, check transport availability and warn
|
|
|
|
checkBtAvailability();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
private DeviceSupport createClassNameDeviceSupport(GBDevice device) throws GBException {
|
|
|
|
String className = device.getAddress();
|
2015-08-22 01:08:46 +02:00
|
|
|
try {
|
|
|
|
Class<?> deviceSupportClass = Class.forName(className);
|
|
|
|
Constructor<?> constructor = deviceSupportClass.getConstructor();
|
|
|
|
DeviceSupport support = (DeviceSupport) constructor.newInstance();
|
|
|
|
// has to create the device itself
|
2015-12-13 00:43:07 +01:00
|
|
|
support.setContext(device, null, mContext);
|
2015-08-22 01:08:46 +02:00
|
|
|
return support;
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
return null; // not a class, or not known at least
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new GBException("Error creating DeviceSupport instance for " + className, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
private void checkBtAvailability() {
|
|
|
|
if (mBtAdapter == null) {
|
|
|
|
GB.toast(mContext.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN);
|
|
|
|
} else if (!mBtAdapter.isEnabled()) {
|
|
|
|
GB.toast(mContext.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 04:14:06 +02:00
|
|
|
private ServiceDeviceSupport createServiceDeviceSupport(GBDevice device) throws GBException {
|
2023-09-27 23:11:02 +02:00
|
|
|
DeviceCoordinator coordinator = device.getDeviceCoordinator();
|
2023-08-20 04:14:06 +02:00
|
|
|
Class supportClass = coordinator.getDeviceSupportClass();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Constructor supportConstructor = supportClass.getConstructor(DeviceType.class);
|
|
|
|
DeviceSupport supportInstance = (DeviceSupport) supportConstructor.newInstance(device.getType());
|
|
|
|
return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags());
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
// ignore, let next call get the default, zero-argument constructor
|
|
|
|
} catch (ReflectiveOperationException e) {
|
|
|
|
LOG.error("error calling DeviceSupport constructor with argument 'DeviceType'");
|
|
|
|
throw new GBException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
DeviceSupport supportInstance = (DeviceSupport) supportClass.newInstance();
|
|
|
|
return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags());
|
|
|
|
} catch (ReflectiveOperationException e) {
|
|
|
|
LOG.error("error calling DeviceSupport constructor with zero arguments");
|
|
|
|
throw new GBException(e);
|
2022-06-14 18:05:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
private DeviceSupport createBTDeviceSupport(GBDevice gbDevice) throws GBException {
|
2015-08-04 01:01:14 +02:00
|
|
|
if (mBtAdapter != null && mBtAdapter.isEnabled()) {
|
|
|
|
try {
|
2022-06-14 18:05:41 +02:00
|
|
|
DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice);
|
2015-08-04 01:01:14 +02:00
|
|
|
if (deviceSupport != null) {
|
|
|
|
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
|
|
|
|
return deviceSupport;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2016-07-28 22:42:16 +02:00
|
|
|
throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e);
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-08-19 00:03:52 +02:00
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
private DeviceSupport createTCPDeviceSupport(GBDevice gbDevice) throws GBException {
|
2015-08-19 00:03:52 +02:00
|
|
|
try {
|
2023-08-20 04:14:06 +02:00
|
|
|
DeviceSupport deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
2015-08-19 00:03:52 +02:00
|
|
|
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
|
|
|
|
return deviceSupport;
|
|
|
|
} catch (Exception e) {
|
2015-12-13 00:43:07 +01:00
|
|
|
throw new GBException("cannot connect to " + gbDevice, e); // FIXME: localize
|
2015-08-19 00:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|