• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.Set;
33 
34 /**
35  * A class represents a device that can be discovered by multiple mediums.
36  *
37  * @hide
38  */
39 @SystemApi
40 public abstract class NearbyDevice {
41 
42     @Nullable
43     private final String mName;
44 
45     @Medium
46     private final List<Integer> mMediums;
47 
48     private final int mRssi;
49 
50     /**
51      * Creates a new NearbyDevice.
52      *
53      * @param name Local device name. Can be {@code null} if there is no name.
54      * @param mediums The {@link Medium}s over which the device is discovered.
55      * @param rssi The received signal strength in dBm.
56      * @hide
57      */
NearbyDevice(@ullable String name, List<Integer> mediums, int rssi)58     public NearbyDevice(@Nullable String name, List<Integer> mediums, int rssi) {
59         for (int medium : mediums) {
60             Preconditions.checkState(isValidMedium(medium),
61                     "Not supported medium: " + medium
62                             + ", scan medium must be one of NearbyDevice#Medium.");
63         }
64         mName = name;
65         mMediums = mediums;
66         mRssi = rssi;
67     }
68 
mediumToString(@edium int medium)69     static String mediumToString(@Medium int medium) {
70         switch (medium) {
71             case Medium.BLE:
72                 return "BLE";
73             case Medium.BLUETOOTH:
74                 return "Bluetooth Classic";
75             default:
76                 return "Unknown";
77         }
78     }
79 
80     /**
81      * True if the medium is defined in {@link Medium}.
82      *
83      * @param medium Integer that may represent a medium type.
84      */
isValidMedium(@edium int medium)85     public static boolean isValidMedium(@Medium int medium) {
86         return medium == Medium.BLE
87                 || medium == Medium.BLUETOOTH;
88     }
89 
90     /**
91      * The name of the device, or null if not available.
92      */
93     @Nullable
getName()94     public String getName() {
95         return mName;
96     }
97 
98     /** The medium over which this device was discovered. */
99     @NonNull
getMediums()100     @Medium public List<Integer> getMediums() {
101         return mMediums;
102     }
103 
104     /**
105      * Returns the received signal strength in dBm.
106      */
107     @IntRange(from = -127, to = 126)
getRssi()108     public int getRssi() {
109         return mRssi;
110     }
111 
112     @Override
toString()113     public String toString() {
114         StringBuilder stringBuilder = new StringBuilder();
115         stringBuilder.append("NearbyDevice [");
116         if (mName != null && !mName.isEmpty()) {
117             stringBuilder.append("name=").append(mName).append(", ");
118         }
119         stringBuilder.append("medium={");
120         for (int medium : mMediums) {
121             stringBuilder.append(mediumToString(medium));
122         }
123         stringBuilder.append("} rssi=").append(mRssi);
124         stringBuilder.append("]");
125         return stringBuilder.toString();
126     }
127 
128     @Override
equals(Object other)129     public boolean equals(Object other) {
130         if (!(other instanceof NearbyDevice)) {
131             return false;
132         }
133         NearbyDevice otherDevice = (NearbyDevice) other;
134         Set<Integer> mediumSet = new ArraySet<>(mMediums);
135         Set<Integer> otherMediumSet = new ArraySet<>(otherDevice.mMediums);
136         if (!mediumSet.equals(otherMediumSet)) {
137             return false;
138         }
139 
140         return Objects.equals(mName, otherDevice.mName) && mRssi == otherDevice.mRssi;
141     }
142 
143     @Override
hashCode()144     public int hashCode() {
145         return Objects.hash(mName, mMediums, mRssi);
146     }
147 
148     /**
149      * The medium where a NearbyDevice was discovered on.
150      *
151      * @hide
152      */
153     @IntDef({Medium.BLE, Medium.BLUETOOTH})
154     @Retention(RetentionPolicy.SOURCE)
155     public @interface Medium {
156         int BLE = 1;
157         int BLUETOOTH = 2;
158     }
159 }
160 
161