WifiInfo.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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.os.Parcelable; 20import android.os.Parcel; 21import android.net.NetworkInfo.DetailedState; 22 23import java.util.EnumMap; 24 25/** 26 * Describes the state of any Wifi connection that is active or 27 * is in the process of being set up. 28 */ 29public class WifiInfo implements Parcelable { 30 /** 31 * This is the map described in the Javadoc comment above. The positions 32 * of the elements of the array must correspond to the ordinal values 33 * of <code>DetailedState</code>. 34 */ 35 private static final EnumMap<SupplicantState, DetailedState> stateMap = 36 new EnumMap<SupplicantState, DetailedState>(SupplicantState.class); 37 38 static { 39 stateMap.put(SupplicantState.DISCONNECTED, DetailedState.DISCONNECTED); 40 stateMap.put(SupplicantState.INACTIVE, DetailedState.IDLE); 41 stateMap.put(SupplicantState.SCANNING, DetailedState.SCANNING); 42 stateMap.put(SupplicantState.ASSOCIATING, DetailedState.CONNECTING); 43 stateMap.put(SupplicantState.ASSOCIATED, DetailedState.CONNECTING); 44 stateMap.put(SupplicantState.FOUR_WAY_HANDSHAKE, DetailedState.AUTHENTICATING); 45 stateMap.put(SupplicantState.GROUP_HANDSHAKE, DetailedState.AUTHENTICATING); 46 stateMap.put(SupplicantState.COMPLETED, DetailedState.OBTAINING_IPADDR); 47 stateMap.put(SupplicantState.DORMANT, DetailedState.DISCONNECTED); 48 stateMap.put(SupplicantState.UNINITIALIZED, DetailedState.IDLE); 49 stateMap.put(SupplicantState.INVALID, DetailedState.FAILED); 50 } 51 52 private SupplicantState mSupplicantState; 53 private String mBSSID; 54 private String mSSID; 55 private int mNetworkId; 56 private boolean mHiddenSSID; 57 /** Received Signal Strength Indicator */ 58 private int mRssi; 59 60 /** Link speed in Mbps */ 61 public static final String LINK_SPEED_UNITS = "Mbps"; 62 private int mLinkSpeed; 63 64 private int mIpAddress; 65 66 private String mMacAddress; 67 68 WifiInfo() { 69 mSSID = null; 70 mBSSID = null; 71 mNetworkId = -1; 72 mSupplicantState = SupplicantState.UNINITIALIZED; 73 mRssi = -9999; 74 mLinkSpeed = -1; 75 mIpAddress = 0; 76 mHiddenSSID = false; 77 } 78 79 void setSSID(String SSID) { 80 mSSID = SSID; 81 // network is considered not hidden by default 82 mHiddenSSID = false; 83 } 84 85 /** 86 * Returns the service set identifier (SSID) of the current 802.11 network. 87 * If the SSID is an ASCII string, it will be returned surrounded by double 88 * quotation marks.Otherwise, it is returned as a string of hex digits. The 89 * SSID may be {@code null} if there is no network currently connected. 90 * @return the SSID 91 */ 92 public String getSSID() { 93 return mSSID; 94 } 95 96 void setBSSID(String BSSID) { 97 mBSSID = BSSID; 98 } 99 100 /** 101 * Return the basic service set identifier (BSSID) of the current access point. 102 * The BSSID may be {@code null} if there is no network currently connected. 103 * @return the BSSID, in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX} 104 */ 105 public String getBSSID() { 106 return mBSSID; 107 } 108 109 /** 110 * Returns the received signal strength indicator of the current 802.11 111 * network. 112 * <p><strong>This is not normalized, but should be!</strong></p> 113 * @return the RSSI, in the range ??? to ??? 114 */ 115 public int getRssi() { 116 return mRssi; 117 } 118 119 void setRssi(int rssi) { 120 mRssi = rssi; 121 } 122 123 /** 124 * Returns the current link speed in {@link #LINK_SPEED_UNITS}. 125 * @return the link speed. 126 * @see #LINK_SPEED_UNITS 127 */ 128 public int getLinkSpeed() { 129 return mLinkSpeed; 130 } 131 132 void setLinkSpeed(int linkSpeed) { 133 this.mLinkSpeed = linkSpeed; 134 } 135 136 /** 137 * Record the MAC address of the WLAN interface 138 * @param macAddress the MAC address in {@code XX:XX:XX:XX:XX:XX} form 139 */ 140 void setMacAddress(String macAddress) { 141 this.mMacAddress = macAddress; 142 } 143 144 public String getMacAddress() { 145 return mMacAddress; 146 } 147 148 void setNetworkId(int id) { 149 mNetworkId = id; 150 } 151 152 /** 153 * Each configured network has a unique small integer ID, used to identify 154 * the network when performing operations on the supplicant. This method 155 * returns the ID for the currently connected network. 156 * @return the network ID, or -1 if there is no currently connected network 157 */ 158 public int getNetworkId() { 159 return mNetworkId; 160 } 161 162 /** 163 * Return the detailed state of the supplicant's negotiation with an 164 * access point, in the form of a {@link SupplicantState SupplicantState} object. 165 * @return the current {@link SupplicantState SupplicantState} 166 */ 167 public SupplicantState getSupplicantState() { 168 return mSupplicantState; 169 } 170 171 void setSupplicantState(SupplicantState state) { 172 mSupplicantState = state; 173 } 174 175 void setIpAddress(int address) { 176 mIpAddress = address; 177 } 178 179 public int getIpAddress() { 180 return mIpAddress; 181 } 182 183 /** 184 * @return {@code true} if this network does not broadcast its SSID, so an 185 * SSID-specific probe request must be used for scans. 186 */ 187 public boolean getHiddenSSID() { 188 return mHiddenSSID; 189 } 190 191 /** {@hide} */ 192 public void setHiddenSSID(boolean hiddenSSID) { 193 mHiddenSSID = hiddenSSID; 194 } 195 196 /** 197 * Map a supplicant state into a fine-grained network connectivity state. 198 * @param suppState the supplicant state 199 * @return the corresponding {@link DetailedState} 200 */ 201 public static DetailedState getDetailedStateOf(SupplicantState suppState) { 202 return stateMap.get(suppState); 203 } 204 205 /** 206 * Set the <code>SupplicantState</code> from the string name 207 * of the state. 208 * @param stateName the name of the state, as a <code>String</code> returned 209 * in an event sent by {@code wpa_supplicant}. 210 */ 211 void setSupplicantState(String stateName) { 212 mSupplicantState = valueOf(stateName); 213 } 214 215 static SupplicantState valueOf(String stateName) { 216 if ("4WAY_HANDSHAKE".equalsIgnoreCase(stateName)) 217 return SupplicantState.FOUR_WAY_HANDSHAKE; 218 else { 219 try { 220 return SupplicantState.valueOf(stateName.toUpperCase()); 221 } catch (IllegalArgumentException e) { 222 return SupplicantState.INVALID; 223 } 224 } 225 } 226 227 @Override 228 public String toString() { 229 StringBuffer sb = new StringBuffer(); 230 String none = "<none>"; 231 232 sb.append("SSID: ").append(mSSID == null ? none : mSSID). 233 append(", BSSID: ").append(mBSSID == null ? none : mBSSID). 234 append(", MAC: ").append(mMacAddress == null ? none : mMacAddress). 235 append(", Supplicant state: "). 236 append(mSupplicantState == null ? none : mSupplicantState). 237 append(", RSSI: ").append(mRssi). 238 append(", Link speed: ").append(mLinkSpeed). 239 append(", Net ID: ").append(mNetworkId); 240 241 return sb.toString(); 242 } 243 244 /** Implement the Parcelable interface {@hide} */ 245 public int describeContents() { 246 return 0; 247 } 248 249 /** Implement the Parcelable interface {@hide} */ 250 public void writeToParcel(Parcel dest, int flags) { 251 dest.writeInt(mNetworkId); 252 dest.writeInt(mRssi); 253 dest.writeInt(mLinkSpeed); 254 dest.writeInt(mIpAddress); 255 dest.writeString(getSSID()); 256 dest.writeString(mBSSID); 257 dest.writeString(mMacAddress); 258 mSupplicantState.writeToParcel(dest, flags); 259 } 260 261 /** Implement the Parcelable interface {@hide} */ 262 public static final Creator<WifiInfo> CREATOR = 263 new Creator<WifiInfo>() { 264 public WifiInfo createFromParcel(Parcel in) { 265 WifiInfo info = new WifiInfo(); 266 info.setNetworkId(in.readInt()); 267 info.setRssi(in.readInt()); 268 info.setLinkSpeed(in.readInt()); 269 info.setIpAddress(in.readInt()); 270 info.setSSID(in.readString()); 271 info.mBSSID = in.readString(); 272 info.mMacAddress = in.readString(); 273 info.mSupplicantState = SupplicantState.CREATOR.createFromParcel(in); 274 return info; 275 } 276 277 public WifiInfo[] newArray(int size) { 278 return new WifiInfo[size]; 279 } 280 }; 281} 282