1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.nearby; 18 19 import android.annotation.IntDef; 20 import android.annotation.IntRange; 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.annotation.SystemApi; 24 import android.util.ArraySet; 25 26 import com.android.internal.util.Preconditions; 27 28 import java.util.List; 29 import java.util.Objects; 30 import java.util.Set; 31 32 /** 33 * A class represents a device that can be discovered by multiple mediums. 34 * 35 * @hide 36 */ 37 @SystemApi 38 public abstract class NearbyDevice { 39 40 @Nullable 41 private final String mName; 42 43 @Medium 44 private final List<Integer> mMediums; 45 46 private final int mRssi; 47 48 /** 49 * Creates a new NearbyDevice. 50 * 51 * @param name Local device name. Can be {@code null} if there is no name. 52 * @param mediums The {@link Medium}s over which the device is discovered. 53 * @param rssi The received signal strength in dBm. 54 * @hide 55 */ NearbyDevice(@ullable String name, List<Integer> mediums, int rssi)56 public NearbyDevice(@Nullable String name, List<Integer> mediums, int rssi) { 57 for (int medium : mediums) { 58 Preconditions.checkState(isValidMedium(medium), 59 "Not supported medium: " + medium 60 + ", scan medium must be one of NearbyDevice#Medium."); 61 } 62 mName = name; 63 mMediums = mediums; 64 mRssi = rssi; 65 } 66 mediumToString(@edium int medium)67 static String mediumToString(@Medium int medium) { 68 switch (medium) { 69 case Medium.BLE: 70 return "BLE"; 71 case Medium.BLUETOOTH: 72 return "Bluetooth Classic"; 73 default: 74 return "Unknown"; 75 } 76 } 77 78 /** 79 * True if the medium is defined in {@link Medium}. 80 * 81 * @param medium Integer that may represent a medium type. 82 */ isValidMedium(@edium int medium)83 public static boolean isValidMedium(@Medium int medium) { 84 return medium == Medium.BLE 85 || medium == Medium.BLUETOOTH; 86 } 87 88 /** 89 * The name of the device, or null if not available. 90 */ 91 @Nullable getName()92 public String getName() { 93 return mName; 94 } 95 96 /** The medium over which this device was discovered. */ 97 @NonNull getMediums()98 @Medium public List<Integer> getMediums() { 99 return mMediums; 100 } 101 102 /** 103 * Returns the received signal strength in dBm. 104 */ 105 @IntRange(from = -127, to = 126) getRssi()106 public int getRssi() { 107 return mRssi; 108 } 109 110 @Override toString()111 public String toString() { 112 StringBuilder stringBuilder = new StringBuilder(); 113 stringBuilder.append("NearbyDevice ["); 114 if (mName != null && !mName.isEmpty()) { 115 stringBuilder.append("name=").append(mName).append(", "); 116 } 117 stringBuilder.append("medium={"); 118 for (int medium : mMediums) { 119 stringBuilder.append(mediumToString(medium)); 120 } 121 stringBuilder.append("} rssi=").append(mRssi); 122 stringBuilder.append("]"); 123 return stringBuilder.toString(); 124 } 125 126 @Override equals(Object other)127 public boolean equals(Object other) { 128 if (!(other instanceof NearbyDevice)) { 129 return false; 130 } 131 NearbyDevice otherDevice = (NearbyDevice) other; 132 Set<Integer> mediumSet = new ArraySet<>(mMediums); 133 Set<Integer> otherMediumSet = new ArraySet<>(otherDevice.mMediums); 134 if (!mediumSet.equals(otherMediumSet)) { 135 return false; 136 } 137 138 return Objects.equals(mName, otherDevice.mName) && mRssi == otherDevice.mRssi; 139 } 140 141 @Override hashCode()142 public int hashCode() { 143 return Objects.hash(mName, mMediums, mRssi); 144 } 145 146 /** 147 * The medium where a NearbyDevice was discovered on. 148 * 149 * @hide 150 */ 151 @IntDef({Medium.BLE, Medium.BLUETOOTH}) 152 public @interface Medium { 153 int BLE = 1; 154 int BLUETOOTH = 2; 155 } 156 } 157 158