2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
2015-10-18 01:01:13 +02:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-12-13 00:43:07 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.widget.Toast;
|
2015-10-18 01:01:13 +02:00
|
|
|
|
2016-05-07 21:46:20 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-09-30 22:57:16 +02:00
|
|
|
import java.lang.reflect.Method;
|
2015-05-05 00:48:02 +02:00
|
|
|
import java.util.ArrayList;
|
2016-08-31 00:33:54 +02:00
|
|
|
import java.util.Collections;
|
2015-12-13 00:43:07 +01:00
|
|
|
import java.util.LinkedHashSet;
|
2015-05-05 00:48:02 +02:00
|
|
|
import java.util.List;
|
2015-12-13 00:43:07 +01:00
|
|
|
import java.util.Set;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-09-30 23:07:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2016-08-31 00:33:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.UnknownDeviceCoordinator;
|
2016-08-17 00:53:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Coordinator;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleCoordinator;
|
2016-09-19 12:37:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.vibratissimo.VibratissimoCoordinator;
|
2016-08-31 00:33:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public class DeviceHelper {
|
2016-05-07 21:46:20 +02:00
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DeviceHelper.class);
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private static final DeviceHelper instance = new DeviceHelper();
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public static DeviceHelper getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazily created
|
|
|
|
private List<DeviceCoordinator> coordinators;
|
|
|
|
|
2016-11-27 01:09:20 +01:00
|
|
|
public DeviceType getSupportedType(GBDeviceCandidate candidate) {
|
2015-05-05 00:48:02 +02:00
|
|
|
for (DeviceCoordinator coordinator : getAllCoordinators()) {
|
2016-11-27 01:09:20 +01:00
|
|
|
DeviceType deviceType = coordinator.getSupportedType(candidate);
|
|
|
|
if (deviceType.isSupported()) {
|
|
|
|
return deviceType;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 01:09:20 +01:00
|
|
|
return DeviceType.UNKNOWN;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 01:09:20 +01:00
|
|
|
public boolean getSupportedType(GBDevice device) {
|
2016-08-31 00:33:54 +02:00
|
|
|
for (DeviceCoordinator coordinator : getAllCoordinators()) {
|
|
|
|
if (coordinator.supports(device)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
public GBDevice findAvailableDevice(String deviceAddress, Context context) {
|
|
|
|
Set<GBDevice> availableDevices = getAvailableDevices(context);
|
|
|
|
for (GBDevice availableDevice : availableDevices) {
|
|
|
|
if (deviceAddress.equals(availableDevice.getAddress())) {
|
|
|
|
return availableDevice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:40:37 +02:00
|
|
|
/**
|
|
|
|
* Returns the list of all available devices that are supported by Gadgetbridge.
|
|
|
|
* Note that no state is known about the returned devices. Even if one of those
|
|
|
|
* devices is connected, it will report the default not-connected state.
|
|
|
|
*
|
|
|
|
* Clients interested in the "live" devices being managed should use the class
|
|
|
|
* DeviceManager.
|
|
|
|
* @param context
|
|
|
|
* @return
|
|
|
|
*/
|
2015-12-13 00:43:07 +01:00
|
|
|
public Set<GBDevice> getAvailableDevices(Context context) {
|
|
|
|
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
|
|
|
|
Set<GBDevice> availableDevices = new LinkedHashSet<GBDevice>();
|
|
|
|
|
|
|
|
if (btAdapter == null) {
|
|
|
|
GB.toast(context, context.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN);
|
|
|
|
} else if (!btAdapter.isEnabled()) {
|
|
|
|
GB.toast(context, context.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
|
2016-09-02 10:54:32 +02:00
|
|
|
}
|
|
|
|
List<GBDevice> dbDevices = getDatabaseDevices();
|
2016-09-29 22:40:16 +02:00
|
|
|
// these come first, as they have the most information already
|
|
|
|
availableDevices.addAll(dbDevices);
|
2016-09-02 10:54:32 +02:00
|
|
|
if (btAdapter != null) {
|
2016-08-31 00:33:54 +02:00
|
|
|
List<GBDevice> bondedDevices = getBondedDevices(btAdapter);
|
|
|
|
availableDevices.addAll(bondedDevices);
|
2016-09-02 10:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
String miAddr = prefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, "");
|
|
|
|
if (miAddr.length() > 0) {
|
|
|
|
GBDevice miDevice = new GBDevice(miAddr, "MI", DeviceType.MIBAND);
|
2016-09-29 22:40:16 +02:00
|
|
|
availableDevices.add(miDevice);
|
2016-09-02 10:54:32 +02:00
|
|
|
}
|
2015-12-13 00:43:07 +01:00
|
|
|
|
2016-09-02 10:54:32 +02:00
|
|
|
String pebbleEmuAddr = prefs.getString("pebble_emu_addr", "");
|
|
|
|
String pebbleEmuPort = prefs.getString("pebble_emu_port", "");
|
|
|
|
if (pebbleEmuAddr.length() >= 7 && pebbleEmuPort.length() > 0) {
|
|
|
|
GBDevice pebbleEmuDevice = new GBDevice(pebbleEmuAddr + ":" + pebbleEmuPort, "Pebble qemu", DeviceType.PEBBLE);
|
|
|
|
availableDevices.add(pebbleEmuDevice);
|
2015-12-13 00:43:07 +01:00
|
|
|
}
|
|
|
|
return availableDevices;
|
|
|
|
}
|
|
|
|
|
2015-10-18 01:01:13 +02:00
|
|
|
public GBDevice toSupportedDevice(BluetoothDevice device) {
|
|
|
|
GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN);
|
2016-05-07 21:46:20 +02:00
|
|
|
|
2015-10-18 01:01:13 +02:00
|
|
|
for (DeviceCoordinator coordinator : getAllCoordinators()) {
|
|
|
|
if (coordinator.supports(candidate)) {
|
2016-08-18 21:29:26 +02:00
|
|
|
return coordinator.createDevice(candidate);
|
2015-10-18 01:01:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
public DeviceCoordinator getCoordinator(GBDeviceCandidate device) {
|
2015-05-05 00:48:02 +02:00
|
|
|
synchronized (this) {
|
|
|
|
for (DeviceCoordinator coord : getAllCoordinators()) {
|
|
|
|
if (coord.supports(device)) {
|
2016-10-10 20:50:50 +02:00
|
|
|
return coord;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new UnknownDeviceCoordinator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public DeviceCoordinator getCoordinator(GBDevice device) {
|
|
|
|
synchronized (this) {
|
|
|
|
for (DeviceCoordinator coord : getAllCoordinators()) {
|
|
|
|
if (coord.supports(device)) {
|
2016-10-10 20:50:50 +02:00
|
|
|
return coord;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new UnknownDeviceCoordinator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized List<DeviceCoordinator> getAllCoordinators() {
|
|
|
|
if (coordinators == null) {
|
|
|
|
coordinators = createCoordinators();
|
|
|
|
}
|
|
|
|
return coordinators;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<DeviceCoordinator> createCoordinators() {
|
|
|
|
List<DeviceCoordinator> result = new ArrayList<>(2);
|
2016-08-17 00:53:16 +02:00
|
|
|
result.add(new MiBand2Coordinator()); // Note: MiBand2 must come before MiBand because detection is hacky, atm
|
2015-05-05 00:48:02 +02:00
|
|
|
result.add(new MiBandCoordinator());
|
|
|
|
result.add(new PebbleCoordinator());
|
2016-09-19 12:37:41 +02:00
|
|
|
result.add(new VibratissimoCoordinator());
|
2015-05-05 00:48:02 +02:00
|
|
|
return result;
|
|
|
|
}
|
2016-08-31 00:33:54 +02:00
|
|
|
|
|
|
|
private List<GBDevice> getDatabaseDevices() {
|
|
|
|
List<GBDevice> result = new ArrayList<>();
|
|
|
|
try (DBHandler lockHandler = GBApplication.acquireDB()) {
|
|
|
|
List<Device> activeDevices = DBHelper.getActiveDevices(lockHandler.getDaoSession());
|
|
|
|
for (Device dbDevice : activeDevices) {
|
|
|
|
GBDevice gbDevice = toGBDevice(dbDevice);
|
2016-11-27 01:09:20 +01:00
|
|
|
if (gbDevice != null && DeviceHelper.getInstance().getSupportedType(gbDevice)) {
|
2016-08-31 00:33:54 +02:00
|
|
|
result.add(gbDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
GB.toast("Error retrieving devices from database", Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a known device from the database to a GBDevice.
|
|
|
|
* Note: The device might not be supported anymore, so callers should verify that.
|
|
|
|
* @param dbDevice
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private GBDevice toGBDevice(Device dbDevice) {
|
|
|
|
DeviceType deviceType = DeviceType.fromKey(dbDevice.getType());
|
|
|
|
GBDevice gbDevice = new GBDevice(dbDevice.getIdentifier(), dbDevice.getName(), deviceType);
|
|
|
|
List<DeviceAttributes> deviceAttributesList = dbDevice.getDeviceAttributesList();
|
|
|
|
if (deviceAttributesList.size() > 0) {
|
|
|
|
gbDevice.setModel(dbDevice.getModel());
|
|
|
|
DeviceAttributes attrs = deviceAttributesList.get(0);
|
|
|
|
gbDevice.setFirmwareVersion(attrs.getFirmwareVersion1());
|
|
|
|
gbDevice.setFirmwareVersion2(attrs.getFirmwareVersion2());
|
|
|
|
}
|
|
|
|
|
|
|
|
return gbDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<GBDevice> getBondedDevices(BluetoothAdapter btAdapter) {
|
|
|
|
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
|
|
|
|
List<GBDevice> result = new ArrayList<>(pairedDevices.size());
|
|
|
|
DeviceHelper deviceHelper = DeviceHelper.getInstance();
|
|
|
|
for (BluetoothDevice pairedDevice : pairedDevices) {
|
|
|
|
GBDevice device = deviceHelper.toSupportedDevice(pairedDevice);
|
|
|
|
if (device != null) {
|
|
|
|
result.add(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-09-30 22:57:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to removing the bonding with the given device. Returns true
|
|
|
|
* if bonding was supposedly successful and false if anything went wrong
|
|
|
|
* @param device
|
|
|
|
* @return
|
|
|
|
*/
|
2016-09-30 23:07:30 +02:00
|
|
|
public boolean removeBond(GBDevice device) throws GBException {
|
2016-09-30 22:57:16 +02:00
|
|
|
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
if (defaultAdapter != null) {
|
|
|
|
BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress());
|
|
|
|
if (remoteDevice != null) {
|
|
|
|
try {
|
|
|
|
Method method = BluetoothDevice.class.getMethod("removeBond", (Class[]) null);
|
|
|
|
Object result = method.invoke(remoteDevice, (Object[]) null);
|
|
|
|
return Boolean.TRUE.equals(result);
|
|
|
|
} catch (Exception e) {
|
2016-09-30 23:07:30 +02:00
|
|
|
throw new GBException("Error removing bond to device: " + device, e);
|
2016-09-30 22:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|