开启行动数据时,透过wifi传送资料(没有网际网路) [英] Send data through wifi (no internet) when mobile data on

查看:245
本文介绍了开启行动数据时,透过wifi传送资料(没有网际网路)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发和开发通过wifi(由设备生成)连接到硬件设备并通过套接字连接向其发送数据的应用程序. 问题在于,当激活移动数据(3G/4G)时,android会尝试通过它发送数据,而不是通过设备生成的wifi发送数据,因为wifi没有互联网连接. 我当时在考虑使用 ConnectivityManager#setNetworkPreference (),但在api 21中已弃用.

I'm developing and application that connects to a hardware device through wifi (generated by the device) and send data to it through a socket connection. The problem is that when mobile data (3G/4G) is activated android tries to send the data through it instead of sending it through the wifi generated by the device, because because the wifi has no internet connection. I was thinking of using ConnectivityManager#setNetworkPreference() but it has been deprecated in api 21.

如何设置它使用设备生成的wifi而不是移动数据接口发送数据?

How can I set it to send data using the wifi generated by the device instead of the mobile data interface?

推荐答案

经过大量研究和时间尝试以了解所有内容后,我发现在OS 5.0之前的版本(Lollipop)中这是不可能的一次只能保持一个网络接口,而应用无法对此进行控制.

After a lot of research and time trying to understand it all I found out that this isn't possible in versions previous to Android 5.0 (Lollipop), the OS only keeps one network interface up at a time and the apps don't have control over this.

因此,要在大于或等于棒棒糖的版本上执行此操作,请执行以下操作:

So to do this on versions greater or equal to the Lollipop I did the following:

  1. 在进行套接字连接之前,请检查android是否大于或等于Lollipop,以防不是您只是做通常要做的事情;
  2. 如果版本等于或大于Lollipop,则需要执行以下操作:

  1. Before doing your socket connection check if android greater or equal to Lollipop, in case it isn't you just do whatever you have to do normally;
  2. In case the version is equal or greater to Lollipop you need to do something like this:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    final ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder builder;
    builder = new NetworkRequest.Builder();
    //set the transport type do WIFI
    builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    manager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                manager.bindProcessToNetwork(network);
            } else {
                //This method was deprecated in API level 23
                ConnectivityManager.setProcessDefaultNetwork(network);
            }
            try {
                //do a callback or something else to alert your code that it's ok to send the message through socket now
            } catch (Exception e) {
                e.printStackTrace();
            }
            manager.unregisterNetworkCallback(this);
        }
    });
}

  • 完成后,您应该停止与此绑定:

  • After you finish you should stop binding the process with this:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        ConnectivityManager manager = (ConnectivityManager) InovePlugApplication.getContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        manager.bindProcessToNetwork(null);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ConnectivityManager.setProcessDefaultNetwork(null);
    }
    

  • 此答案帮助我理解了 https://stackoverflow.com/a/29837637/2550932 的用法.

    This answer helped me unsderstand how to do it https://stackoverflow.com/a/29837637/2550932.

    这篇关于开启行动数据时,透过wifi传送资料(没有网际网路)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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