2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Daniel
|
|
|
|
Dakhno, Daniele Gobbetti, José Rebelo, Taavi Eomäe
|
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-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.impl;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.os.Parcel;
|
2016-06-28 00:35:50 +02:00
|
|
|
import android.os.ParcelUuid;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.os.Parcelable;
|
|
|
|
|
2016-06-28 00:35:50 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-07-05 22:39:05 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2016-11-27 02:41:52 +01:00
|
|
|
import java.util.Arrays;
|
2023-08-31 23:23:10 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2016-11-27 02:41:52 +01:00
|
|
|
import java.util.Set;
|
2016-06-28 00:35:50 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2015-05-05 00:48:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2016-11-27 02:41:52 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
2023-10-28 10:49:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
/**
|
2015-10-26 23:32:03 +01:00
|
|
|
* A device candidate is a Bluetooth device that is not yet managed by
|
|
|
|
* Gadgetbridge. Only if a DeviceCoordinator steps up and confirms to
|
|
|
|
* support this candidate, will the candidate be promoted to a GBDevice.
|
2015-05-05 00:48:02 +02:00
|
|
|
*/
|
2023-08-31 23:23:10 +02:00
|
|
|
public class GBDeviceCandidate implements Parcelable, Cloneable {
|
2016-06-28 00:35:50 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(GBDeviceCandidate.class);
|
|
|
|
|
2023-08-31 23:23:10 +02:00
|
|
|
private BluetoothDevice device;
|
|
|
|
private short rssi;
|
|
|
|
private ParcelUuid[] serviceUuids;
|
|
|
|
// Cached values for device name and bond status, to avoid querying the remote bt device
|
2022-11-27 18:56:10 +01:00
|
|
|
private String deviceName;
|
2023-08-31 23:23:10 +02:00
|
|
|
private Boolean isBonded = null;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2018-03-30 15:38:29 +02:00
|
|
|
public GBDeviceCandidate(BluetoothDevice device, short rssi, ParcelUuid[] serviceUuids) {
|
2015-05-05 00:48:02 +02:00
|
|
|
this.device = device;
|
|
|
|
this.rssi = rssi;
|
2023-10-08 22:44:59 +02:00
|
|
|
this.serviceUuids = serviceUuids != null ? serviceUuids : new ParcelUuid[0];
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
private GBDeviceCandidate(Parcel in) {
|
2015-05-05 00:48:02 +02:00
|
|
|
device = in.readParcelable(getClass().getClassLoader());
|
2016-11-27 01:09:20 +01:00
|
|
|
if (device == null) {
|
2015-05-05 00:48:02 +02:00
|
|
|
throw new IllegalStateException("Unable to read state from Parcel");
|
|
|
|
}
|
2016-11-27 02:41:52 +01:00
|
|
|
rssi = (short) in.readInt();
|
|
|
|
|
2023-08-31 23:23:10 +02:00
|
|
|
serviceUuids = AndroidUtils.toParcelUuids(in.readParcelableArray(getClass().getClassLoader()));
|
|
|
|
|
|
|
|
deviceName = in.readString();
|
|
|
|
final int isBondedInt = in.readInt();
|
|
|
|
if (isBondedInt != -1) {
|
|
|
|
isBonded = (isBondedInt == 1);
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeParcelable(device, 0);
|
|
|
|
dest.writeInt(rssi);
|
2018-03-30 15:38:29 +02:00
|
|
|
dest.writeParcelableArray(serviceUuids, 0);
|
2023-08-31 23:23:10 +02:00
|
|
|
dest.writeString(deviceName);
|
|
|
|
if (isBonded == null) {
|
|
|
|
dest.writeInt(-1);
|
|
|
|
} else {
|
|
|
|
dest.writeInt(isBonded ? 1 : 0);
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 02:41:52 +01:00
|
|
|
public static final Creator<GBDeviceCandidate> CREATOR = new Creator<GBDeviceCandidate>() {
|
|
|
|
@Override
|
|
|
|
public GBDeviceCandidate createFromParcel(Parcel in) {
|
|
|
|
return new GBDeviceCandidate(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GBDeviceCandidate[] newArray(int size) {
|
|
|
|
return new GBDeviceCandidate[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-28 00:35:50 +02:00
|
|
|
public BluetoothDevice getDevice() {
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
public String getMacAddress() {
|
|
|
|
return device != null ? device.getAddress() : GBApplication.getContext().getString(R.string._unknown_);
|
|
|
|
}
|
|
|
|
|
2018-03-30 15:38:29 +02:00
|
|
|
private ParcelUuid[] mergeServiceUuids(ParcelUuid[] serviceUuids, ParcelUuid[] deviceUuids) {
|
2023-08-31 23:23:10 +02:00
|
|
|
Set<ParcelUuid> uuids = new LinkedHashSet<>();
|
2018-03-30 15:38:29 +02:00
|
|
|
if (serviceUuids != null) {
|
|
|
|
uuids.addAll(Arrays.asList(serviceUuids));
|
2016-11-27 02:41:52 +01:00
|
|
|
}
|
|
|
|
if (deviceUuids != null) {
|
|
|
|
uuids.addAll(Arrays.asList(deviceUuids));
|
|
|
|
}
|
|
|
|
return uuids.toArray(new ParcelUuid[0]);
|
|
|
|
}
|
|
|
|
|
2023-08-31 23:23:10 +02:00
|
|
|
public void addUuids(ParcelUuid[] newUuids) {
|
|
|
|
this.serviceUuids = mergeServiceUuids(serviceUuids, newUuids);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRssi(short rssi) {
|
|
|
|
this.rssi = rssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBonded() {
|
|
|
|
if (isBonded == null) {
|
|
|
|
try {
|
|
|
|
isBonded = device.getBondState() == BluetoothDevice.BOND_BONDED;
|
|
|
|
} catch (final SecurityException e) {
|
|
|
|
/* This should never happen because we need all the permissions
|
|
|
|
to get to the point where we can even scan, but 'SecurityException' check
|
|
|
|
is added to stop Android Studio errors */
|
|
|
|
LOG.error("SecurityException on getBonded");
|
|
|
|
isBonded = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isBonded;
|
|
|
|
}
|
|
|
|
|
2016-11-27 02:41:52 +01:00
|
|
|
@NonNull
|
|
|
|
public ParcelUuid[] getServiceUuids() {
|
2018-03-30 15:38:29 +02:00
|
|
|
return serviceUuids;
|
2016-11-27 02:41:52 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:35:50 +02:00
|
|
|
public boolean supportsService(UUID aService) {
|
2016-11-27 02:41:52 +01:00
|
|
|
ParcelUuid[] uuids = getServiceUuids();
|
2023-10-08 21:27:19 +02:00
|
|
|
if (uuids == null || uuids.length == 0) {
|
2016-06-28 00:35:50 +02:00
|
|
|
LOG.warn("no cached services available for " + this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ParcelUuid uuid : uuids) {
|
|
|
|
if (uuid != null && aService.equals(uuid.getUuid())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
public String getName() {
|
2023-08-31 23:23:10 +02:00
|
|
|
if (isNameKnown()) {
|
|
|
|
return deviceName;
|
2022-11-27 18:56:10 +01:00
|
|
|
}
|
2023-08-31 23:23:10 +02:00
|
|
|
return "(unknown)";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void refreshNameIfUnknown() {
|
|
|
|
if (isNameKnown()) {
|
|
|
|
return;
|
2016-07-05 22:39:05 +02:00
|
|
|
}
|
2023-08-31 23:23:10 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
final Method method = device.getClass().getMethod("getAliasName");
|
|
|
|
deviceName = (String) method.invoke(device);
|
|
|
|
} catch (final NoSuchMethodException ignore) {
|
|
|
|
// ignored
|
|
|
|
} catch (final IllegalAccessException | InvocationTargetException ignore) {
|
|
|
|
LOG.warn("Could not get device alias for {}", device.getAddress());
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2023-08-31 23:23:10 +02:00
|
|
|
if (deviceName == null || deviceName.isEmpty()) {
|
|
|
|
try {
|
|
|
|
deviceName = device.getName();
|
|
|
|
} catch (final SecurityException e) {
|
|
|
|
// Should never happen
|
|
|
|
LOG.error("SecurityException on device.getName");
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2023-08-31 23:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isNameKnown() {
|
|
|
|
return deviceName != null && !deviceName.isEmpty();
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public short getRssi() {
|
|
|
|
return rssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-05 01:08:30 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (o == null || getClass() != o.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
GBDeviceCandidate that = (GBDeviceCandidate) o;
|
2015-05-05 01:08:30 +02:00
|
|
|
return device.getAddress().equals(that.device.getAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return device.getAddress().hashCode() ^ 37;
|
|
|
|
}
|
2016-06-28 00:35:50 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2023-10-28 10:49:16 +02:00
|
|
|
return getName() + ": " + getMacAddress();
|
2016-06-28 00:35:50 +02:00
|
|
|
}
|
2023-08-31 23:23:10 +02:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public GBDeviceCandidate clone() {
|
|
|
|
try {
|
|
|
|
final GBDeviceCandidate clone = (GBDeviceCandidate) super.clone();
|
|
|
|
clone.device = this.device;
|
|
|
|
clone.rssi = this.rssi;
|
|
|
|
clone.serviceUuids = this.serviceUuids;
|
|
|
|
clone.deviceName = this.deviceName;
|
|
|
|
clone.isBonded = this.isBonded;
|
|
|
|
return clone;
|
|
|
|
} catch (final CloneNotSupportedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|