2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import android.annotation.SystemApi;
20 import android.os.Parcel;
21 import android.os.Parcelable;
23 import java.util.Objects;
24 import java.util.regex.Pattern;
27 * Information identifying a Wi-Fi network.
33 public class WifiKey implements Parcelable {
35 // Patterns used for validation.
36 private static final Pattern SSID_PATTERN = Pattern.compile("(\".*\")|(0x[\\p{XDigit}]+)",
38 private static final Pattern BSSID_PATTERN =
39 Pattern.compile("([\\p{XDigit}]{2}:){5}[\\p{XDigit}]{2}");
42 * The service set identifier (SSID) of an 802.11 network. If the SSID can be decoded as
43 * UTF-8, it will be surrounded by double quotation marks. Otherwise, it will be a string of
44 * hex digits starting with 0x.
46 public final String ssid;
49 * The basic service set identifier (BSSID) of an access point for this network. This will
50 * be in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX}, where each X is a
53 public final String bssid;
56 * Construct a new {@link WifiKey} for the given Wi-Fi SSID/BSSID pair.
58 * @param ssid the service set identifier (SSID) of an 802.11 network. If the SSID can be
59 * decoded as UTF-8, it should be surrounded by double quotation marks. Otherwise,
60 * it should be a string of hex digits starting with 0x.
61 * @param bssid the basic service set identifier (BSSID) of this network's access point.
62 * This should be in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX},
63 * where each X is a hexadecimal digit.
64 * @throws IllegalArgumentException if either the SSID or BSSID is invalid.
66 public WifiKey(String ssid, String bssid) {
67 if (!SSID_PATTERN.matcher(ssid).matches()) {
68 throw new IllegalArgumentException("Invalid ssid: " + ssid);
70 if (!BSSID_PATTERN.matcher(bssid).matches()) {
71 throw new IllegalArgumentException("Invalid bssid: " + bssid);
77 private WifiKey(Parcel in) {
78 ssid = in.readString();
79 bssid = in.readString();
83 public int describeContents() {
88 public void writeToParcel(Parcel out, int flags) {
89 out.writeString(ssid);
90 out.writeString(bssid);
94 public boolean equals(Object o) {
95 if (this == o) return true;
96 if (o == null || getClass() != o.getClass()) return false;
98 WifiKey wifiKey = (WifiKey) o;
100 return Objects.equals(ssid, wifiKey.ssid) && Objects.equals(bssid, wifiKey.bssid);
104 public int hashCode() {
105 return Objects.hash(ssid, bssid);
109 public String toString() {
110 return "WifiKey[SSID=" + ssid + ",BSSID=" + bssid + "]";
113 public static final Creator<WifiKey> CREATOR =
114 new Creator<WifiKey>() {
116 public WifiKey createFromParcel(Parcel in) {
117 return new WifiKey(in);
121 public WifiKey[] newArray(int size) {
122 return new WifiKey[size];