我可以在 Android 上同时打开 Wi-Fi 和蜂窝网络接口吗?

发布于 2024-12-23 18:18:37 字数 644 浏览 5 评论 0原文

我正在开发一个将在手机上运行的应用程序 将是私人 Wi-Fi 网络上的一个站点。该电话将是一个 站,而不是接入点,并且专用 Wi-Fi 网络也不是 到互联网的路由。我的应用程序需要与 互联网上的服务器以及本地 Wi-Fi 网络上的设备, 因此它需要同时在两个网络上建立连接。 我一直在试图弄清楚如何做到这一点。

我一直在尝试讨论中描述的技术 名为“Can Android 2.X同时连接3G和Wifi数据网络?”,但是 它运作不佳。我发现,当我启用 通过呼叫蜂窝网络 ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE), 我在 Wi-Fi 网络上打开的所有套接字都已关闭。我没有 尝试过,但我怀疑同样的事情也会发生在套接字上 当我切换回 Wi-Fi 时蜂窝网络。

另一个问题是, 这些调用在全球范围内运行,改变网络设置 适用于整个手机,而不仅仅是应用程序。切换网络 像这样进行全局设置会干扰任何其他应用程序 恰好在手机上运行。即使我的应用程序退出后, 电话将继续以其上次设置的网络配置运行。

我正在寻找一种在蜂窝网络上打开连接的方法 同时使用数据和 Wi-Fi 网络,且互不干扰 手机上运行的其他应用程序。

  1. 有谁知道该怎么做 这?
  2. 有谁知道这是否可能?

I'm working on an application that will run on a phone where the phone
will be a station on a private Wi-Fi network. The phone will be a
station, not an access point, and the private Wi-Fi network does not
route to the Internet. My application needs to communicate with
servers on the Internet as well as devices on the local Wi-Fi network,
so it needs to have connections on both networks at the same time.
I've been trying to figure out how to do this.

I've been trying the technique described in the discussion on the
Google Android developers group titled "Can
Android 2.X connect to 3G and Wifi data networks simultaneously?", but
it is not working well. What I find is that, when I enable the
cellular network by calling
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE),
any sockets I have open on the Wi-Fi network are closed. I haven't
tried it, but I suspect the same thing will happen to sockets on the
cellular network when I switch back to Wi-Fi.

Another problem is that,
these calls operate on a global level, changing the network settings
for the entire phone, not just the application. Switching the network
set up globally like this will interfere with any other app that
happens to be running on the phone. Even after my application exits,
the phone continues to run with the last network configuration it set.

I'm looking for a way to have connections open on both the cellular
data and Wi-Fi networks at the same time, and without interfering with
other applications running on the phone.

  1. Does anyone know how to do
    this?
  2. Does anyone know if this is possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

陌路终见情 2024-12-30 18:18:37

这实际上很简单。您需要提交两个请求;一个用于具有 cellular 传输类型的网络,另一个请求具有 wifi 传输类型。然后,无论从这些请求返回什么网络,您都可以根据需要使用它们(例如,仅使用从 wifi 请求返回的网络来处理内部资源)。

以下是同时保持 Wi-Fi 和蜂窝网络运行的示例:

final ConnectivityManager connectivityManager = (ConnectivityManager)
  context.getSystemService(Context.CONNECTIVITY_SERVICE);

final NetworkRequest requestForWifi =
  new NetworkRequest.Builder()
  .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
  .build();

final NetworkRequest requestForCellular =
  new NetworkRequest.Builder()
  .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
  .build();

final NetworkCallback cbWifi = new NetworkCallback() {
  @Override
  void onAvailable(Network network) {
      // Triggers when this network is available so you can bind to it.
      
      // Examples of how to bind to it include 
      // (uncomment the best option for your use-case):
      
      // If you want to set connections for the entire app
      // connectivityManager.bindProcessToNetwork(network);

      // If you want to open a specific connection:
      // By socket:
      // try (Socket socket = new Socket()) {
      //     Network network = getNetwork();
      //     network.bindSocket(socket);
      // }
      // Or by URL:
      // URLConnection conn = network.openConnection(URL url);
  }
};

final NetworkCallback cbCellular = new NetworkCallback() {
  @Override
  void onAvailable(Network network) {
      // Triggers when this network is available so you can bind to it.
  }
};

connectivityManager.requestNetwork(requestForWifi, cbWifi);
connectivityManager.requestNetwork(requestForCellular, cbCellular);

只要有针对特定网络类型(即 Wi-Fi 或蜂窝网络)的请求,ConnectivityService(服务< code>ConnectivityManager 依赖)将保持这些网络可用(如果可用)。因此,使用上述模式但调整 NetworkRequest 对象以满足您的需求,您可以保持任意数量的网络运行。

This is actually pretty simple. You need to file two requests; one for a network with the cellular transport type and an additional request with the wifi transport type. Then, with whatever networks are returned from those requests, you can use them as needed (e.g. only do work on the internal resources with the network returned from the wifi request).

Here is an example to keep both Wi-Fi and Cellular up at the same time:

final ConnectivityManager connectivityManager = (ConnectivityManager)
  context.getSystemService(Context.CONNECTIVITY_SERVICE);

final NetworkRequest requestForWifi =
  new NetworkRequest.Builder()
  .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
  .build();

final NetworkRequest requestForCellular =
  new NetworkRequest.Builder()
  .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
  .build();

final NetworkCallback cbWifi = new NetworkCallback() {
  @Override
  void onAvailable(Network network) {
      // Triggers when this network is available so you can bind to it.
      
      // Examples of how to bind to it include 
      // (uncomment the best option for your use-case):
      
      // If you want to set connections for the entire app
      // connectivityManager.bindProcessToNetwork(network);

      // If you want to open a specific connection:
      // By socket:
      // try (Socket socket = new Socket()) {
      //     Network network = getNetwork();
      //     network.bindSocket(socket);
      // }
      // Or by URL:
      // URLConnection conn = network.openConnection(URL url);
  }
};

final NetworkCallback cbCellular = new NetworkCallback() {
  @Override
  void onAvailable(Network network) {
      // Triggers when this network is available so you can bind to it.
  }
};

connectivityManager.requestNetwork(requestForWifi, cbWifi);
connectivityManager.requestNetwork(requestForCellular, cbCellular);

As long as there are requests for a particular network type (i.e. Wi-Fi or Cellular), ConnectivityService (service that ConnectivityManager relies on) will keep those networks up if available. Therefore using the above pattern but tweaking the NetworkRequest object to meet your needs, you can keep any number of networks up.

且行且努力 2024-12-30 18:18:37

此线程 Android:强制通过无线电发送数据与WiFi提到了解决该问题的两种可能的方法。

  1. 每当您希望应用程序使用特定连接时,请设置网络首选项:

    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
    
  2. 启用高优先级移动数据连接:

    connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
    

对于第二种方法,特别说明它适用于 Android 2.2,不知道这是否也适用于实际版本。然而,据我所知,enableHIPRI或多或少是一个隐藏的网络设置,所以如果可能的话我更喜欢第一种方法。

This thread Android: Force data to be sent over radio vs WiFi mentions two possible approaches to the problem.

  1. Set the network preference whenever you want your app to use a specific connection:

    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
    
  2. Enable high priority mobile data connection:

    connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
    

For the second approach it is specifically stated it works with Android 2.2, no idea if this works in actual versions as well. However as far as I found out, enableHIPRI is more or less a hidden network setting, so I would prefer the first method if possible.

海的爱人是光 2024-12-30 18:18:37

如果您无法使用 API 调用来实现它,并且您愿意亲自参与较低级别的工作,那么一些 Linux 知识可能会有所帮助。
基本上,您要做的就是启动两个接口并在 3g 接口上设置默认路由。
您必须使用具有 root 权限的系统命令来执行此类任务。
关闭套接字的原因可能是由于 API 调用而导致接口再次下降和上升。

If you can't make it using API calls and if you are willing to get your hands dirty with the lower level, some linux knowledge may help.
Basically what you have to do is to bring up both interfaces and have the default route set on the 3g interface.
You will have to use system commands with root privileges for this kind of task.
The reason for the close sockets is probably the interface that goes down and up again because of the API call.

梦境 2024-12-30 18:18:37

如果没有 root 访问权限,该应用程序无法产生太大影响。

使用 Android API,您最多只能打开 WiFi,希望设备切换到它,然后关闭 WiFi,使设备切换到 3G(如果有,APN 是否正确等)。

其他任何东西都不能保证有效。例如,设置首选连接类型并不能保证设备将切换到该类型。

通常的行为是,一旦 WiFi 可用,设备就会同时打开 3G 和 WiFi 一小会儿(3-5 秒),然后关闭 3G。一旦用户或您的应用程序关闭 WiFi,并且设备尝试连接到互联网,它就会在不久后打开 3G。

从 Android 2.3 开始,您将无法再禁用/启用 3G。一种用于破坏/恢复 APN 设置以启用/禁用 3G,但从 4.0 开始,您无法以编程方式更改 APN 设置。

Without root access the app cannot influence much.

With Android API what you can do at most is just turn on WiFi in hope that the device will switch to it and turn WiFi off to make the device switch to 3G (if it's there, the APN is correct etc.).

Anything else is not guaranteed to work. E.g. setting preferred connectivity type doesn't guarantee that the device will switch to that type.

The usual behavior is that as soon as WiFi becomes available, the device will have both 3G and WiFi on for a short while (3-5 secs) and then turn off 3G. As soon as WiFi is turned off by the user or your app, and the device attempts to connect to the Internet, it will turn on 3G after a short while.

Starting with Android 2.3 you can't event disable/enable 3G anymore. One used to spoil/restore APN settings to enable/disable 3G, but starting with 4.0 you can't change APN settings programmatically.

难如初 2024-12-30 18:18:37

我认为一次只能提供一项服务。您可以使用 WiFi,也可以使用蜂窝 3G 数据。两者不能同时工作。

I think only one service is possible at a time. Either you can use WiFi or you can use use Cellular 3G Data. Both can't work simultaneously at a time.

爱情眠于流年 2024-12-30 18:18:37

3G和Wifi数据网络不能同时连接,但是如果运营商支持并且android框架也为运营商改变,3G和Wifi可以同时连接。
现在有些运营商已经具备此功能,但有些运营商还没有。

The 3G and Wifi data networks can not connect at the same time, but 3G and Wifi can be connected simultaneously if the operator support and the android framework also change for the operator.
Now some operators already have this feature but some can not.

晚雾 2024-12-30 18:18:37

目前在 Android 应用程序中无法同时使用两个网络,但您可以在 PC 中执行此操作。
连接一个来自 WIFI 路由器的连接,一个连接来自 LAN 或 USB 互联网棒的连接。
所以创建APP并在PC上使用。如果您不知道如何在 PC 上运行 Android,请谷歌一下。

Using both network at a time is not possible in Android App at present but you can do this in PC.
Connect one from your WIFI router and one from LAN or USB Internet Stick.
So create APp and use on PC. If you do not know how to run Android on PC than google it.

仙女山的月亮 2024-12-30 18:18:37

您可能想研究一下临时 WiFi 网络。
即使 droid 已经使用 wifi 上网(但不使用 ip 堆栈),它也会通过 wifi 连接到设备。

不要将 wifi (IEEE 802.11x) 连接与互联网 (IP) 连接混淆。
IP 可以在 wifi、移动、以太网、DSL、拨号或信鸽上运行,但一次只能运行在一个网络上。

有关临时 WiFi 网络的更多信息,请查看以下链接。

https://code.google.com/p/android-wifi-tether/

请注意,许多运营商禁止多播(共享)其 IP 连接。好的。

You might want to look into ad-hoc wifi networks.
It connects to devices over wifi even if the droid is already using wifi for internet (does not use the ip stack, though).

Dont confuse wifi (IEEE 802.11x) connections with internet (IP) connections.
IP can run on wifi, mobile, ethernet, DSL, dailup or homing pigeons, but only one network at a time.

For more information on ad-hoc wifi networks, have a look at the following link.

https://code.google.com/p/android-wifi-tether/

Please note that many carriers forbid multicasting (sharing) their IP connections. Nice.

一刻暧昧 2024-12-30 18:18:37

如果我没记错的话,如果 Wi-Fi 和 3G/4G 都可用,则 Wi-Fi 优先。

只有当Wi-Fi不可用时,才会切换到3G/4G。当然,前提是两者同时打开。

您可以考虑使用面向互联网的 VPN,而不是使用私人 Wi-Fi,从而限制您的选择,这样设备将从 3G/4G 连接到 VPN,然后如果您担心使用私人 Wi-Fi 的安全性,则随后访问数据。菲。

Android 内置 VPN 客户端,也有第三方客户端可用。

If I am not mistaken, if both Wi-Fi and 3G/4G is available, Wi-Fi will take the precedence.

Only when Wi-Fi is not available, it will switch to 3G/4G. Of course this provided both are switched on at the same time.

Instead of using a private Wi-Fi thus limiting your choice you may consider having a internet facing VPN, so that the device will connect to the VPN from 3G/4G and than subsequently access the data if security is your concern for using private Wi-Fi.

And VPN clients are inbuilt in Android and there are also third party clients available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文