我们正在尝试以编程方式切换ON wifi热点,但有时它会进入无限循环并且不会打开但大部分都可以正常工作。这是它挂起的行“while(!(Boolean)isWifiApEnabledMethod.invoke(wifiManager)){ 了Thread.sleep(500); };” 整个代码放在下面。这只是Android 5.0 / 5.1设备的问题。任何输入都将受到赞赏。
[hadoop@ip-x-x-x-x ~]$ ls -la /etc/spark/conf/
total 64
drwxr-xr-x 2 root root 4096 Oct 4 08:08 .
drwxr-xr-x 3 root root 4096 Oct 4 07:41 ..
-rw-r--r-- 1 root root 987 Jul 26 21:56 docker.properties.template
-rw-r--r-- 1 root root 1105 Jul 26 21:56 fairscheduler.xml.template
-rw-r--r-- 1 root root 2373 Oct 4 07:42 hive-site.xml
-rw-r--r-- 1 root root 2024 Oct 4 07:42 log4j.properties
-rw-r--r-- 1 root root 2025 Jul 26 21:56 log4j.properties.template
-rw-r--r-- 1 root root 7239 Oct 4 07:42 metrics.properties
-rw-r--r-- 1 root root 7239 Jul 26 21:56 metrics.properties.template
-rw-r--r-- 1 root root 865 Jul 26 21:56 slaves.template
-rw-r--r-- 1 root root 1292 Jul 26 21:56 spark-defaults.conf.template
-rwxr-xr-x 1 root root 1563 Oct 4 07:42 spark-env.sh
-rwxr-xr-x 1 root root 3861 Jul 26 21:56 spark-env.sh.template
我们进一步探索,当我们使用这种方法时,我们会获得更好的行为
WifiManager wifiManager = (WifiManager)getActivity().getApplicationContext().getSystemService (Context.WIFI_SERVICE);
//If Wifi is ON then switch it OFF
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
WifiConfiguration netConfig = new WifiConfiguration();
Random random = new Random();
int iRandomKey = random.nextInt(65536);
netConfig.SSID = "TestHotspot";
netConfig.status = WifiConfiguration.Status.ENABLED;
netConfig.priority = 40;
//hiding SSID,
netConfig.hiddenSSID = true;
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
netConfig.preSharedKey = "testpassword";
final String RingOfFire_Ssid = netConfig.SSID;
final String RingOfFire_password = netConfig.preSharedKey;
try {
Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apstatus = (Boolean) setWifiApMethod.invoke(wifiManager, netConfig, true);
Log.d("ConnectWIFI:", "apstatus" + ":" + apstatus);
Method isWifiApEnabledMethod = wifiManager.getClass().getMethod("isWifiApEnabled");
while (!(Boolean) isWifiApEnabledMethod.invoke(wifiManager)) {
Thread.sleep(500);
};
Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
netConfig = (WifiConfiguration) getWifiApConfigurationMethod.invoke(wifiManager);
Log.d("ConnectWIFI:", netConfig.SSID + ":" + netConfig.preSharedKey + ":" + "Hotspot");
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
}
由于