2016-08-24 33 views
0

我试图通过使用以下代码以编程方式更改android中的移动热点广播频道,但它不更改任何值/频道。Android:如何通过编程方式更改移动热点广播频道?

注意:我可以通过编程方式更改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); 
+0

http://stackoverflow.com/questions/6394599/android-turn-on-off-wifi-hotspot-programmatically – 2016-08-24 06:42:03

+1

@Adnanul,上面的链接是以编程方式打开/关闭WiFi HotSpot,但我的问题是专门更改移动热点广播频道。谢谢, – kgsharathkumar

+0

kgsharathkumar,你有没有可用的热点广播频道?这应该是第一步,对吗? – 2016-08-24 07:07:13

回答

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(); 
+0

考虑添加一些解释 - 并正确缩进您的源代码。左侧不需要15个字符边框! – GhostCat

0

你的问题我完全同意,但没有API是avilabl e以编程方式在android中读取/写入移动热点广播频道。

0

您不能以编程方式。

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

相关问题