问题
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