0

我尝试使用以下代码以编程方式更改 android 中的移动 hotsport 广播频道,但它没有更改任何值/频道。

注意:我可以通过编程方式更改 SSID 和密码。
我尝试设置频道 11,仍然无法正常工作...

提前致谢

我的代码是

public void HotspotChannelWrite() {
    WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);

    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    netConfig = new WifiConfiguration();
    netConfig.SSID = "TipturInfo";
    netConfig.preSharedKey = "Sharath";
    netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    try {

        Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        boolean apstatus=(Boolean) setWifiApMethod.invoke(wifiManager, netConfig,true);

        Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled");
        while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){};

        Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState");
        int apstate=(Integer)getWifiApStateMethod.invoke(wifiManager);

        Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
        netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);

        Log.i("Writing HotspotData", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");

        // For Channel change
        Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
        int freq = 2462; // default to channel 11
        wcAdhocFreq.setInt(netConfig, freq);
        Log.i("HotspotData Channel", "\n Frequence:"+freq );
        Log.i("HotspotData Channel", "\n Frequence:"+wcAdhocFreq );

        // For Saving Data
        wifiManager.saveConfiguration();

    } catch (IllegalFormatException ife) {
        ife.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    wifiManager.saveConfiguration();
}

我也尝试了另一种方法:

Field wcFreq = WifiConfiguration.class.getField("channel");
       wcFreq.setInt(netConfig,11);
4

3 回答 3

1

试试这个,适用于Android 7.0

                Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
                netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);

                //Log.i("Writing HotspotData", "\nSSID:" + netConfig.SSID + "\nPassword:" + netConfig.preSharedKey + "\n");

                Field wcBand = WifiConfiguration.class.getField("apBand");
                int vb = wcBand.getInt(netConfig);
                Log.i("Band was", "val=" + vb);
                wcBand.setInt(netConfig, 2); // 2Ghz

                // For Channel change
                Field wcFreq = WifiConfiguration.class.getField("apChannel");
                int val = wcFreq.getInt(netConfig);
                Log.i("Config was", "val=" + val);
                wcFreq.setInt(netConfig,11); // channel 11

                Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
                setWifiApConfigurationMethod.invoke(wifiManager, netConfig);

                // For Saving Data
                wifiManager.saveConfiguration();
于 2017-07-07T18:52:52.753 回答
0

您的问题我完全同意,但是没有 API 可用于通过编程方式在 android 中读取/写入移动热点广播频道。

于 2016-08-26T05:05:41.463 回答
0

你不能以编程方式。

我完全同意您的问题,但在 Android 中,没有 API 可用于以编程方式读取/写入移动热点广播频道。

于 2016-09-23T10:31:18.940 回答