HTC Android手机无法修改热点 Hotspot SSID名称和密码!【已解决】

Some htc phones seems to use a class of type HotspotProfile to keep its configuration. So, before callingsetWifiApEnabled, you need set the ssid in htc's way:

boolean isHtc = false;
            try {
                isHtc = WifiConfiguration.class
                        .getDeclaredField("mWifiApProfile") != null;
            } catch (java.lang.NoSuchFieldException e) {
                isHtc = false;
            }

            WifiConfiguration apConfig = new WifiConfiguration();
            apConfig.SSID = "H3c_xx";
            apConfig.preSharedKey = "h3ch3ch3c";
            apConfig.allowedKeyManagement
                    .set(WifiConfiguration.KeyMgmt.WPA_PSK);

            if (isHtc) {
                setHTCSSID(apConfig);
            }

            // 通过反射调用设置热点
            Method method;
            method = wifiManager.getClass().getMethod("setWifiApEnabled",
                    WifiConfiguration.class, Boolean.TYPE);
            // 返回热点打开状态
            boolean flag = (Boolean) method.invoke(wifiManager, apConfig, true);


设置SSID和密码

    public void setHTCSSID(WifiConfiguration config) {
        try {
            Field mWifiApProfileField = WifiConfiguration.class
                    .getDeclaredField("mWifiApProfile");
            mWifiApProfileField.setAccessible(true);
            Object hotSpotProfile = mWifiApProfileField.get(config);
            mWifiApProfileField.setAccessible(false);


            if (hotSpotProfile != null) {
                Field ssidField = hotSpotProfile.getClass().getDeclaredField(
                        "SSID");
                ssidField.setAccessible(true);
                ssidField.set(hotSpotProfile, config.SSID);
                ssidField.setAccessible(false);


                Field localField3 = hotSpotProfile.getClass().getDeclaredField(
                        "key");
                localField3.setAccessible(true);
                localField3.set(hotSpotProfile, config.preSharedKey);
                localField3.setAccessible(false);


                Field localField6 = hotSpotProfile.getClass().getDeclaredField(
                        "dhcpEnable");
                localField6.setAccessible(true);
                localField6.setInt(hotSpotProfile, 1);
                localField6.setAccessible(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

可以通过遍历属性的方法查看还有那些可以修改的值:

    public void dumpHTCWifiFunction2() {
        Log.e(TAG, "dumpHTCWifiFunction2++");
        // Method[] classMethods = null;
        Field localField1;
        Field[] fieldlist;
        WifiConfiguration apConfig = new WifiConfiguration();
        try {
            localField1 = WifiConfiguration.class
                    .getDeclaredField("mWifiApProfile");
            localField1.setAccessible(true);
            Object localObject2 = localField1.get(apConfig);
            localField1.setAccessible(false);
            if (localObject2 != null) {
                fieldlist = localObject2.getClass().getDeclaredFields();
                for (int i = 0; i < fieldlist.length; i++) {
                    Log.e(TAG, "found api: " + fieldlist[i].getName()
                            + " type:" + fieldlist[i].toString());
                }
            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Log.e(TAG, "dumpHTCWifiFunction2--");
    }

如:

found api: DEFAULT_DHCP_MAX_AP_CONNECTION type:public static final int android.net.wifi.WifiConfiguration$HotspotProfile.DEFAULT_DHCP_MAX_AP_CONNECTION
found api: DEFAULT_MAX_AP_CONNECTION type:public static final int android.net.wifi.WifiConfiguration$HotspotProfile.DEFAULT_MAX_AP_CONNECTION
found api: DHCP_MAX_AP_CONNECTION type:public static final int android.net.wifi.WifiConfiguration$HotspotProfile.DHCP_MAX_AP_CONNECTION
found api: MAX_AP_CONNECTION type:public static final int android.net.wifi.WifiConfiguration$HotspotProfile.MAX_AP_CONNECTION
found api: OPEN type:public static final java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.OPEN
found api: WEP type:public static final java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.WEP
found api: WPA type:public static final java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.WPA
found api: WPA2 type:public static final java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.WPA2
found api: BSSID type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.BSSID
found api: SSID type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.SSID
found api: blocklist type:public [Ljava.lang.String; android.net.wifi.WifiConfiguration$HotspotProfile.blocklist
found api: whitelist type:public [Ljava.lang.String; android.net.wifi.WifiConfiguration$HotspotProfile.whitelist
found api: this$0 type:final android.net.wifi.WifiConfiguration android.net.wifi.WifiConfiguration$HotspotProfile.this$0
found api: startingIP type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.startingIP
found api: dhcpSubnetMask type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.dhcpSubnetMask
found api: dnsIPAddr1 type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.dnsIPAddr1
found api: dnsIPAddr2 type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.dnsIPAddr2
found api: secureType type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.secureType
found api: key type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.key
found api: ipAddress type:public java.lang.String android.net.wifi.WifiConfiguration$HotspotProfile.ipAddress
found api: hiddenSSID type:public boolean android.net.wifi.WifiConfiguration$HotspotProfile.hiddenSSID
found api: maxConns type:public int android.net.wifi.WifiConfiguration$HotspotProfile.maxConns
found api: maxDhcpClients type:public int android.net.wifi.WifiConfiguration$HotspotProfile.maxDhcpClients
found api: enableMacFilter type:public int android.net.wifi.WifiConfiguration$HotspotProfile.enableMacFilter
found api: sleepPolicy type:public int android.net.wifi.WifiConfiguration$HotspotProfile.sleepPolicy
found api: dhcpEnable type:public int android.net.wifi.WifiConfiguration$HotspotProfile.dhcpEnable
found api: connectionArray type:public int android.net.wifi.WifiConfiguration$HotspotProfile.connectionArray
found api: channel type:public int android.net.wifi.WifiConfiguration$HotspotProfile.channel

例子:http://t.cn/z83xYFG

你可能感兴趣的:(HTC Android手机无法修改热点 Hotspot SSID名称和密码!【已解决】)