How can I enforce internet connection only through wifi?

给你一囗甜甜゛ 提交于 2019-12-13 10:33:07

问题


We have a Wifi-connected device. We need to connect to the device via wifi for installation. Once connected, the application sends the request on a ip.

This works smoothly before android 9. But I'm having some problems when I do with android 9. Because when i connect to device i getting error message like this: wi-fi has no internet acces. And requests continue on mobile data. So app can't talk with device.

Can i doing something about this problem?


回答1:


I guess this is related to permissions since API level 27 you need either

android.permission.ACCESS_FINE_LOCATION
or
android.permission.ACCESS_COARSE_LOCATION
permission.

You need to CHANGE_WIFI_STATE for Android Pie.
Try this

  ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected()) {
            WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            wifiInfo.getSSID();
            String name = networkInfo.getExtraInfo();
            String ssid = "\"" + wifiInfo.getSSID() + "\"";
}

You can study about these changes in detail. Refer Here



来源:https://stackoverflow.com/questions/55397068/how-can-i-enforce-internet-connection-only-through-wifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!