在没有Internet连接的情况下使用WiFi [英] Using a WiFi without Internet Connection

查看:362
本文介绍了在没有Internet连接的情况下使用WiFi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在连接到具有自己的WiFi网络的设备. 在android 6及更高版本中,即使没有互联网连接,系统也会在几秒钟后询问我是否要连接到该网络. 只有批准该消息后,我才能连接到我的设备. 我尝试以编程方式连接到网络,而不是强迫用户每次都手动进行设置并进行连接.我使用以下代码连接到设备网络:

In my app I am connecting to a device that has it's own WiFi network. In android 6 and above the system asks me after a few seconds if I want to connect to this network even though there is no internet connection. Only after approving that message I can connect to my device. I tried connecting to the network programmatically and not force the user to go to his settings and connect manually every time. I used the following code to connect to the devices network:

private void connectToWiFi(WifiManager wifiManager, String wifiName) {

    WifiConfiguration configuration = new WifiConfiguration();
    configuration.SSID = "\"" + wifiName + "\"";
    configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wifiManager.addNetwork(configuration);
    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration i : list) {
        if (i.SSID != null && i.SSID.equals("\"" + wifiName + "\"")) {
            wifiManager.disconnect();
            wifiManager.enableNetwork(i.networkId, true);
            wifiManager.reconnect();
            break;
        }
    }
}

,并尝试强制该应用使用WiFi连接,而不是我正在使用的蜂窝数据:

and also trying to force the app to use the WiFi connection and not the cellular data I am using :

NetworkRequest.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new NetworkRequest.Builder();
        builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

        connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                String ssid = wifiManager.getConnectionInfo().getSSID();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    connectivityManager.bindProcessToNetwork(null);
                    if (ssid.equals("\"" + Prefs.getWifiName(PUFMainActivity.this) + "\"")) {
                        connectivityManager.bindProcessToNetwork(network);
                        connectivityManager.unregisterNetworkCallback(this);
                    }
                }
            }
        });

    }

尽管只要蜂窝数据处于活动状态,设备就不会出现连接状态.如果我禁用蜂窝数据,那么它可以正常工作. 我需要知道是否有一种方法可以以编程方式完成我想做的事情,而无需告诉用户禁用其蜂窝数据.

Although as long as the Cellular data is active the device doesn't apear to be connected. If I disable the Cellular data then it works fine. I need to know if there is a way to do what I want programmatically without telling the user to disable his Cellular data.

谢谢

推荐答案

尽管只要蜂窝数据处于活动状态,设备就不会激活 会被连接.如果我禁用蜂窝数据,那么它将起作用 很好.

Although as long as the Cellular data is active the device doesn't apear to be connected. If I disable the Cellular data then it works fine.

我正面临着类似的问题.默认解决方案对我不起作用,因此我使用了Network类中的bindSocket方法,显然它可以正常工作.我不知道此解决方案是否适用于您的问题,但这是一些代码:

I was facing a similar problem. The default solution didn't work for me so I used the bindSocket method from Network class, and apparently it is working just fine. I don't know if this solution applies to your problem, but here's some code:

ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = connectivityManager.getAllNetworks();
for(Network network : networks){
   NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);

   if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
       wifiNetwork = network; // Grabbing the Network object for later usage
   }
}

并在套接字之后:

if(android.os.Build.VERSION.SDK_INT >= 22) {
   wifiNetwork.bindSocket(socket);
}

更新: 您也可以使用NetworkCallback来捕获Network对象.

UPDATE: You can use a NetworkCallback to grab the Network object as well.

NetworkRequest.Builder builder;
builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
   @Override
   public void onAvailable(Network network) {
      wifiNetwork = network;
   }
});

这篇关于在没有Internet连接的情况下使用WiFi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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