如何在运行时更改热点名称和密码android 8.0及更高版本的设备device

时间:2019-07-17 12:25:10

标签: android wifimanager hotspot wificonfiguration personal-hotspot

我想在用户启动热点时以编程方式更改热点名称和密码。此示例代码在7.0设备以下可以正常工作,但在8.0及更高版本中则无法工作。有人可以帮助我摆脱困境吗?

            try {
                    Toast.makeText(context, "Wifi config", Toast.LENGTH_SHORT).show();
                    WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
                    Method getConfigMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
                    WifiConfiguration wifiConfig = (WifiConfiguration) getConfigMethod.invoke(wifiManager);
                    wifiConfig.SSID = "NOTFORYOU";
                    wifiConfig.status= WifiConfiguration.Status.DISABLED;
                    wifiConfig.preSharedKey=Calendar.getInstance().getTimeInMillis()+"NOTF0RyOu"+Calendar.getInstance().getTimeInMillis();
                    Method setConfigMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
                    setConfigMethod.invoke(wifiManager, wifiConfig);
                }
                catch (Exception e) {
                    e.printStackTrace();                       
                }

0 个答案:

没有答案