ScanResult.java revision a4864472313208e4f1de02f45d3eadad237c54af
1/*
2 * Copyright (C) 2008 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
17package android.net.wifi;
18
19import android.net.wifi.passpoint.PasspointInfo;
20import android.net.wifi.passpoint.PasspointManager;
21import android.os.Parcelable;
22import android.os.Parcel;
23
24/**
25 * Describes information about a detected access point. In addition
26 * to the attributes described here, the supplicant keeps track of
27 * {@code quality}, {@code noise}, and {@code maxbitrate} attributes,
28 * but does not currently report them to external clients.
29 */
30public class ScanResult implements Parcelable {
31    /** The network name. */
32    public String SSID;
33
34    /** Ascii encoded SSID. This will replace SSID when we deprecate it. @hide */
35    public WifiSsid wifiSsid;
36
37    /** The address of the access point. */
38    public String BSSID;
39    /**
40     * Describes the authentication, key management, and encryption schemes
41     * supported by the access point.
42     */
43    public String capabilities;
44    /**
45     * The detected signal level in dBm.
46     */
47    public int level;
48    /**
49     * The frequency in MHz of the channel over which the client is communicating
50     * with the access point.
51     */
52    public int frequency;
53
54    /**
55     * Time Synchronization Function (tsf) timestamp in microseconds when
56     * this result was last seen.
57     */
58    public long timestamp;
59
60    /**
61     * Timestamp representing date when this result was last seen, in milliseconds from 1970
62     * {@hide}
63     */
64    public long seen;
65
66
67    /**
68     * The approximate distance to the AP in centimeter, if available.  Else
69     * {@link UNSPECIFIED}.
70     * {@hide}
71     */
72    public int distanceCm;
73
74    /**
75     * The standard deviation of the distance to the AP, if available.
76     * Else {@link UNSPECIFIED}.
77     * {@hide}
78     */
79    public int distanceSdCm;
80
81    /**
82     * Passpoint ANQP information. This is not fetched automatically.
83     * Use {@link PasspointManager#requestAnqpInfo} to request ANQP info.
84     */
85    public PasspointInfo passpoint;
86
87    /**
88     * {@hide}
89     */
90    public final static int UNSPECIFIED = -1;
91
92    /** {@hide} */
93    public ScanResult(WifiSsid wifiSsid, String BSSID, String caps, int level, int frequency,
94            long tsf) {
95        this.wifiSsid = wifiSsid;
96        this.SSID = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
97        this.BSSID = BSSID;
98        this.capabilities = caps;
99        this.level = level;
100        this.frequency = frequency;
101        this.timestamp = tsf;
102        this.distanceCm = UNSPECIFIED;
103        this.distanceSdCm = UNSPECIFIED;
104    }
105
106    /** {@hide} */
107    public ScanResult(WifiSsid wifiSsid, String BSSID, String caps, int level, int frequency,
108            long tsf, int distCm, int distSdCm) {
109        this.wifiSsid = wifiSsid;
110        this.SSID = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
111        this.BSSID = BSSID;
112        this.capabilities = caps;
113        this.level = level;
114        this.frequency = frequency;
115        this.timestamp = tsf;
116        this.distanceCm = distCm;
117        this.distanceSdCm = distSdCm;
118    }
119
120    /** copy constructor {@hide} */
121    public ScanResult(ScanResult source) {
122        if (source != null) {
123            wifiSsid = source.wifiSsid;
124            SSID = source.SSID;
125            BSSID = source.BSSID;
126            capabilities = source.capabilities;
127            level = source.level;
128            frequency = source.frequency;
129            timestamp = source.timestamp;
130            distanceCm = source.distanceCm;
131            distanceSdCm = source.distanceSdCm;
132            seen = source.seen;
133            if (source.passpoint != null)
134                passpoint = new PasspointInfo(source.passpoint);
135        }
136    }
137
138    /** empty scan result
139     *
140     * {@hide}
141     * */
142    public ScanResult() {
143    }
144
145    @Override
146    public String toString() {
147        StringBuffer sb = new StringBuffer();
148        String none = "<none>";
149
150        sb.append("SSID: ").
151            append(wifiSsid == null ? WifiSsid.NONE : wifiSsid).
152            append(", BSSID: ").
153            append(BSSID == null ? none : BSSID).
154            append(", capabilities: ").
155            append(capabilities == null ? none : capabilities).
156            append(", level: ").
157            append(level).
158            append(", frequency: ").
159            append(frequency).
160            append(", timestamp: ").
161            append(timestamp);
162
163        sb.append(", distance: ").append((distanceCm != UNSPECIFIED ? distanceCm : "?")).
164                append("(cm)");
165        sb.append(", distanceSd: ").append((distanceSdCm != UNSPECIFIED ? distanceSdCm : "?")).
166                append("(cm)");
167
168        if (passpoint != null) sb.append(passpoint.toString());
169
170        return sb.toString();
171    }
172
173    /** Implement the Parcelable interface {@hide} */
174    public int describeContents() {
175        return 0;
176    }
177
178    /** Implement the Parcelable interface {@hide} */
179    public void writeToParcel(Parcel dest, int flags) {
180        if (wifiSsid != null) {
181            dest.writeInt(1);
182            wifiSsid.writeToParcel(dest, flags);
183        } else {
184            dest.writeInt(0);
185        }
186        dest.writeString(BSSID);
187        dest.writeString(capabilities);
188        dest.writeInt(level);
189        dest.writeInt(frequency);
190        dest.writeLong(timestamp);
191        dest.writeInt(distanceCm);
192        dest.writeInt(distanceSdCm);
193        if (passpoint != null) {
194            dest.writeInt(1);
195            passpoint.writeToParcel(dest, flags);
196        } else {
197            dest.writeInt(0);
198        }
199    }
200
201    /** Implement the Parcelable interface {@hide} */
202    public static final Creator<ScanResult> CREATOR =
203        new Creator<ScanResult>() {
204            public ScanResult createFromParcel(Parcel in) {
205                WifiSsid wifiSsid = null;
206                if (in.readInt() == 1) {
207                    wifiSsid = WifiSsid.CREATOR.createFromParcel(in);
208                }
209                ScanResult sr = new ScanResult(
210                    wifiSsid,
211                    in.readString(),
212                    in.readString(),
213                    in.readInt(),
214                    in.readInt(),
215                    in.readLong(),
216                    in.readInt(),
217                    in.readInt()
218                );
219                if (in.readInt() == 1) {
220                    sr.passpoint = PasspointInfo.CREATOR.createFromParcel(in);
221                }
222                return sr;
223            }
224
225            public ScanResult[] newArray(int size) {
226                return new ScanResult[size];
227            }
228        };
229
230}
231