Change Configuration of mobile Hotspot

后端 未结 2 1075
终归单人心
终归单人心 2021-01-16 01:07

I\'m trying to run a hotspot with a new name and open accessibility.

    wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wifi         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-16 01:48

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

    if (isHtc) setHTCSSID(config);
    methodNum = getMethodNumber("setWifiApEnabled");
    try {
        wmMethods[methodNum].invoke(wifiManager, wifiConfig, true);
        ...
    

    isHtc can be calculated like:

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

    and setHTCSSID would be:

    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);
    
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    

    I found this information in some chinese blogs: http://xiaxingwork.iteye.com/blog/1727722 and http://blog.sina.com.cn/s/blog_53dd443a010109i8.html

提交回复
热议问题