Android的:如何使用WiFi网络的特定主机,但有使用手机的移动网络一切 [英] Android: How to use a Wifi network for specific hosts but have phone use Mobile network for everything else

查看:537
本文介绍了Android的:如何使用WiFi网络的特定主机,但有使用手机的移动网络一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发移动应用(iOS和Android)来控制设备通过Wi-Fi。该设备创建一个无线网络(SoftAP),但不提供访问互联网。

I am developing a mobile application (iOS and Android) to control a device over Wi-Fi. The device creates a wireless network (SoftAP) but does not provide access to the internet.

在iOS上,我可以连接到该设备,并要求其IP地址的(192.168.70.1)的,但所有其他请求回落到移动网络。这允许同时连接到该设备通过Wi-Fi手机,以保持互联网连接。

On iOS, I can connect to the device and make requests to its IP address (192.168.70.1) but all other requests fall back to the mobile network. This allows the phone to maintain internet connectivity while connected to the device over Wi-Fi.

在Android上,如果我连接到该设备的无线网络,上网请求不会回落到移动连接,他们只是失败。

On Android, if I connect to the device wireless network, internet requests do not fall back to the mobile connection, they just fail.

在我的Andr​​oid应用程序,我可以使用<一个href="http://developer.android.com/reference/android/net/ConnectivityManager.html#requestRouteToHost(int,%20int)"><$c$c>ConnectivityManager.requestRouteToHost强制从我的应用程序请求使用所述移动网络。但是,其他应用程序发出的请求仍然使用该设备的WiFi网络和失败。

In my Android app, I can use ConnectivityManager.requestRouteToHost to force requests from my app to use the mobile network. However, requests made by other apps still use the device wifi network and fail.

我也尝试过使用 ConnectivityManager 更改网络preference有:

I have also tried to use the ConnectivityManager to change the network preference with :

ConnectivityManager.setNetwork preference(ConnectivityManager.TYPE_MOBILE)

ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE)

这会导致手机使用移动网络的所有应用程序的所有请求。 Wi-Fi已被禁用。看来,在$ P $ Android的无线网络连接pvious版本仍然可以使用,即使它不是preferred网络,但是这似乎并没有在奇巧的工作。它可能关系到Android的4.2的变化:<一href="https://$c$c.google.com/p/android/issues/detail?id=73509">https://$c$c.google.com/p/android/issues/detail?id=73509 ConnectivityManager自4.2眼泪就下来了网络,不是网络preference

This causes the phone to use the mobile network for all requests from all applications. Wi-Fi is disabled. It seems that in previous versions of Android Wi-Fi could still be used even if it is not the "preferred" network but this does not seem to work in KitKat. It is possibly related to a change in Android 4.2: https://code.google.com/p/android/issues/detail?id=73509 "ConnectivityManager since 4.2 tears down networks that are not the NetworkPreference"

有没有一个解决方案,允许Android应用程序使用的无线网络连接特定的IP地址和移动网络的一切?也许这可以通过NDK做?

Is there a solution that allows an Android app to use Wi-Fi for a specific IP address and the mobile network for everything else? Maybe this can be done via the NDK?

推荐答案

其实你可以,但只因为棒棒堂(API 21)。

Actually you can, but only since Lollipop (API 21).

从Android API 21文档:

From the Android API 21 Documentation:

    Android 5.0 provides new multi-networking APIs that let your app dynamically scan for available
networks with specific capabilities, and establish a connection to them. This functionality is useful
when your app requires a specialized network, such as an SUPL, MMS, or carrier-billing network, or if
you want to send data using a particular type of transport protocol.

所以,你可以使用<一个href="https://developer.android.com/reference/android/net/NetworkRequest.Builder.html">NetworkRequest.Builder类来创建一个 NetworkRequest 发送超过一定的要求<一href="https://developer.android.com/reference/android/net/NetworkRequest.Builder.html#addTransportType(int)">Transport键入。

So you can use the NetworkRequest.Builder class to create a NetworkRequest that sends the request over a certain Transport Type.

使用以下功能:

NetworkRequest.Builder addTransportType(int transportType)

要设置传输类型(<一href="https://developer.android.com/reference/android/net/NetworkCapabilities.html#TRANSPORT_WIFI">WIFI, <一href="https://developer.android.com/reference/android/net/NetworkCapabilities.html#TRANSPORT_ETHERNET">ETHERNET或<一href="https://developer.android.com/reference/android/net/NetworkCapabilities.html#TRANSPORT_CELLULAR">CELLULAR例如)为您的要求。

To set transport types (WIFI, ETHERNET or CELLULAR for example) for your requests.

这篇关于Android的:如何使用WiFi网络的特定主机,但有使用手机的移动网络一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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