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-06-28 00:35:50 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
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;
|
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
|
|
|
*/
|
2015-08-03 23:09:49 +02:00
|
|
|
public class GBDeviceCandidate implements Parcelable {
|
2016-06-28 00:35:50 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(GBDeviceCandidate.class);
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BluetoothDevice device;
|
|
|
|
private final short rssi;
|
2015-05-05 00:48:02 +02:00
|
|
|
private DeviceType deviceType = DeviceType.UNKNOWN;
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
public GBDeviceCandidate(BluetoothDevice device, short rssi) {
|
2015-05-05 00:48:02 +02:00
|
|
|
this.device = device;
|
|
|
|
this.rssi = rssi;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
rssi = (short) in.readInt();
|
|
|
|
deviceType = DeviceType.valueOf(in.readString());
|
|
|
|
|
|
|
|
if (device == null || deviceType == null) {
|
|
|
|
throw new IllegalStateException("Unable to read state from Parcel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeParcelable(device, 0);
|
|
|
|
dest.writeInt(rssi);
|
|
|
|
dest.writeString(deviceType.name());
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:35:50 +02:00
|
|
|
public BluetoothDevice getDevice() {
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
public DeviceType getDeviceType() {
|
|
|
|
return deviceType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMacAddress() {
|
|
|
|
return device != null ? device.getAddress() : GBApplication.getContext().getString(R.string._unknown_);
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:35:50 +02:00
|
|
|
public boolean supportsService(UUID aService) {
|
|
|
|
ParcelUuid[] uuids = device.getUuids();
|
|
|
|
if (uuids == null) {
|
|
|
|
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() {
|
2016-07-05 22:39:05 +02:00
|
|
|
String deviceName = null;
|
|
|
|
try {
|
|
|
|
Method method = device.getClass().getMethod("getAliasName");
|
|
|
|
if (method != null) {
|
|
|
|
deviceName = (String) method.invoke(device);
|
|
|
|
}
|
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {
|
|
|
|
LOG.info("Could not get device alias for " + deviceName);
|
|
|
|
}
|
|
|
|
if (deviceName == null || deviceName.length() == 0) {
|
|
|
|
deviceName = device.getName();
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2016-07-05 22:39:05 +02:00
|
|
|
if (deviceName == null || deviceName.length() == 0) {
|
|
|
|
deviceName = "(unknown)";
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2016-07-05 22:39:05 +02:00
|
|
|
return deviceName;
|
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() {
|
|
|
|
return getName() + ": " + getMacAddress();
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|