如何仅通过wifi强制执行互联网连接? [英] How can I enforce internet connection only through wifi?

查看:248
本文介绍了如何仅通过wifi强制执行互联网连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个连接Wifi的设备.我们需要通过wifi连接到设备进行安装.连接后,应用程序通过ip发送请求.

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.

这在android 9之前可以正常工作.但是当我使用android 9时遇到一些问题.因为当我连接到设备时,我收到如下错误消息:wi-fi没有互联网访问权限.并且请求继续针对移动数据.因此,应用无法与设备通话.

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?

推荐答案

我想这与权限有关,因为API level 27您需要

android.permission.ACCESS_FINE_LOCATION
或者
android.permission.ACCESS_COARSE_LOCATION
允许.

对于Android Pie,您需要CHANGE_WIFI_STATE.
试试这个

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

这篇关于如何仅通过wifi强制执行互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆