FileDocCategorySizeDatePackage
WifiSsid.javaAPI DocAndroid 5.1 API8225Thu Mar 12 22:22:44 GMT 2015android.net.wifi

WifiSsid

public class WifiSsid extends Object implements android.os.Parcelable
Stores SSID octets and handles conversion. For Ascii encoded string, any octet < 32 or > 127 is encoded as a "\x" followed by the hex representation of the octet. Exception chars are ", \, \e, \n, \r, \t which are escaped by a \ See src/utils/common.c for the implementation in the supplicant.
hide

Fields Summary
private static final String
TAG
public final ByteArrayOutputStream
octets
private static final int
HEX_RADIX
public static final String
NONE
public static final Creator
CREATOR
Implement the Parcelable interface {@hide}
Constructors Summary
private WifiSsid()


      
    
Methods Summary
private voidconvertToBytes(java.lang.String asciiEncoded)

        int i = 0;
        int val = 0;
        while (i< asciiEncoded.length()) {
            char c = asciiEncoded.charAt(i);
            switch (c) {
                case '\\":
                    i++;
                    switch(asciiEncoded.charAt(i)) {
                        case '\\":
                            octets.write('\\");
                            i++;
                            break;
                        case '"":
                            octets.write('"");
                            i++;
                            break;
                        case 'n":
                            octets.write('\n");
                            i++;
                            break;
                        case 'r":
                            octets.write('\r");
                            i++;
                            break;
                        case 't":
                            octets.write('\t");
                            i++;
                            break;
                        case 'e":
                            octets.write(27); //escape char
                            i++;
                            break;
                        case 'x":
                            i++;
                            try {
                                val = Integer.parseInt(asciiEncoded.substring(i, i + 2), HEX_RADIX);
                            } catch (NumberFormatException e) {
                                val = -1;
                            }
                            if (val < 0) {
                                val = Character.digit(asciiEncoded.charAt(i), HEX_RADIX);
                                if (val < 0) break;
                                octets.write(val);
                                i++;
                            } else {
                                octets.write(val);
                                i += 2;
                            }
                            break;
                        case '0":
                        case '1":
                        case '2":
                        case '3":
                        case '4":
                        case '5":
                        case '6":
                        case '7":
                            val = asciiEncoded.charAt(i) - '0";
                            i++;
                            if (asciiEncoded.charAt(i) >= '0" && asciiEncoded.charAt(i) <= '7") {
                                val = val * 8 + asciiEncoded.charAt(i) - '0";
                                i++;
                            }
                            if (asciiEncoded.charAt(i) >= '0" && asciiEncoded.charAt(i) <= '7") {
                                val = val * 8 + asciiEncoded.charAt(i) - '0";
                                i++;
                            }
                            octets.write(val);
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    octets.write(c);
                    i++;
                    break;
            }
        }
    
public static android.net.wifi.WifiSsidcreateFromAsciiEncoded(java.lang.String asciiEncoded)

        WifiSsid a = new WifiSsid();
        a.convertToBytes(asciiEncoded);
        return a;
    
public static android.net.wifi.WifiSsidcreateFromHex(java.lang.String hexStr)

        WifiSsid a = new WifiSsid();
        if (hexStr == null) return a;

        if (hexStr.startsWith("0x") || hexStr.startsWith("0X")) {
            hexStr = hexStr.substring(2);
        }

        for (int i = 0; i < hexStr.length()-1; i += 2) {
            int val;
            try {
                val = Integer.parseInt(hexStr.substring(i, i + 2), HEX_RADIX);
            } catch(NumberFormatException e) {
                val = 0;
            }
            a.octets.write(val);
        }
        return a;
    
public intdescribeContents()
Implement the Parcelable interface {@hide}

        return 0;
    
public java.lang.StringgetHexString()

hide

        String out = "0x";
        byte[] ssidbytes = getOctets();
        for (int i = 0; i < octets.size(); i++) {
            out += String.format(Locale.US, "%02x", ssidbytes[i]);
        }
        return out;
    
public byte[]getOctets()

hide

        return octets.toByteArray();
    
private booleanisArrayAllZeroes(byte[] ssidBytes)

        for (int i = 0; i< ssidBytes.length; i++) {
            if (ssidBytes[i] != 0) return false;
        }
        return true;
    
public booleanisHidden()

hide

        return isArrayAllZeroes(octets.toByteArray());
    
public java.lang.StringtoString()

        byte[] ssidBytes = octets.toByteArray();
        // Supplicant returns \x00\x00\x00\x00\x00\x00\x00\x00 hex string
        // for a hidden access point. Make sure we maintain the previous
        // behavior of returning empty string for this case.
        if (octets.size() <= 0 || isArrayAllZeroes(ssidBytes)) return "";
        // TODO: Handle conversion to other charsets upon failure
        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder()
                .onMalformedInput(CodingErrorAction.REPLACE)
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
        CharBuffer out = CharBuffer.allocate(32);

        CoderResult result = decoder.decode(ByteBuffer.wrap(ssidBytes), out, true);
        out.flip();
        if (result.isError()) {
            return NONE;
        }
        return out.toString();
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface {@hide}

        dest.writeInt(octets.size());
        dest.writeByteArray(octets.toByteArray());