1 /* 2 * Copyright (C) 2014 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.net; 18 19 import android.annotation.SystemApi; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import java.util.Objects; 24 import java.util.regex.Pattern; 25 26 /** 27 * Information identifying a Wi-Fi network. 28 * @see NetworkKey 29 * 30 * @hide 31 */ 32 @SystemApi 33 public class WifiKey implements Parcelable { 34 35 // Patterns used for validation. 36 private static final Pattern SSID_PATTERN = Pattern.compile("(\".*\")|(0x[\\p{XDigit}]+)"); 37 private static final Pattern BSSID_PATTERN = 38 Pattern.compile("([\\p{XDigit}]{2}:){5}[\\p{XDigit}]{2}"); 39 40 /** 41 * The service set identifier (SSID) of an 802.11 network. If the SSID can be decoded as 42 * UTF-8, it will be surrounded by double quotation marks. Otherwise, it will be a string of 43 * hex digits starting with 0x. 44 */ 45 public final String ssid; 46 47 /** 48 * The basic service set identifier (BSSID) of an access point for this network. This will 49 * be in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX}, where each X is a 50 * hexadecimal digit. 51 */ 52 public final String bssid; 53 54 /** 55 * Construct a new {@link WifiKey} for the given Wi-Fi SSID/BSSID pair. 56 * 57 * @param ssid the service set identifier (SSID) of an 802.11 network. If the SSID can be 58 * decoded as UTF-8, it should be surrounded by double quotation marks. Otherwise, 59 * it should be a string of hex digits starting with 0x. 60 * @param bssid the basic service set identifier (BSSID) of this network's access point. 61 * This should be in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX}, 62 * where each X is a hexadecimal digit. 63 * @throws IllegalArgumentException if either the SSID or BSSID is invalid. 64 */ 65 public WifiKey(String ssid, String bssid) { 66 if (!SSID_PATTERN.matcher(ssid).matches()) { 67 throw new IllegalArgumentException("Invalid ssid: " + ssid); 68 } 69 if (!BSSID_PATTERN.matcher(bssid).matches()) { 70 throw new IllegalArgumentException("Invalid bssid: " + bssid); 71 } 72 this.ssid = ssid; 73 this.bssid = bssid; 74 } 75 76 private WifiKey(Parcel in) { 77 ssid = in.readString(); 78 bssid = in.readString(); 79 } 80 81 @Override 82 public int describeContents() { 83 return 0; 84 } 85 86 @Override 87 public void writeToParcel(Parcel out, int flags) { 88 out.writeString(ssid); 89 out.writeString(bssid); 90 } 91 92 @Override 93 public boolean equals(Object o) { 94 if (this == o) return true; 95 if (o == null || getClass() != o.getClass()) return false; 96 97 WifiKey wifiKey = (WifiKey) o; 98 99 return Objects.equals(ssid, wifiKey.ssid) && Objects.equals(bssid, wifiKey.bssid); 100 } 101 102 @Override 103 public int hashCode() { 104 return Objects.hash(ssid, bssid); 105 } 106 107 @Override 108 public String toString() { 109 return "WifiKey[SSID=" + ssid + ",BSSID=" + bssid + "]"; 110 } 111 112 public static final Creator<WifiKey> CREATOR = 113 new Creator<WifiKey>() { 114 @Override 115 public WifiKey createFromParcel(Parcel in) { 116 return new WifiKey(in); 117 } 118 119 @Override 120 public WifiKey[] newArray(int size) { 121 return new WifiKey[size]; 122 } 123 }; 124 } 125