在应用程序中使用多个网络接口

Use multiple network interfaces in an app(在应用程序中使用多个网络接口)

本文介绍了在应用程序中使用多个网络接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个通过 wifi 触发 Sony qx 智能手机可连接相机的应用程序.但是,我需要通过另一个本地网络实时传输手机上的图像.由于 wifi 卡用于 qx 连接,我需要能够通过 USB 使用以太网来从手机传输图像.Http 请求将用于触发摄像头并将图像发送到手机上.

I wrote an app that is triggering a Sony qx smartphone attachable camera over wifi. However I need to transfer the images off the phone over another local network in real time. Since the wifi card is being used for qx connection I need to be able to use ethernet over usb for transferring images off the phone. Http requests will be used to trigger the camera and send the images off the phone.

是否可以在具有两个网络接口设置的手机上的一个 android 应用程序中指定某些 http 请求使用一个网络接口而其他人使用另一个网络接口?这需要通过路由表而不是java来完成吗?

Is it possible in one android app on a phone with two network interfaces setup to specify for certain http requests to use one network interface and for others to use another network interface ? Does this need to be done through routing tables, not java?

我使用的手机是有根的 nexus 6p.

The phone I'm using is a rooted nexus 6p.

更新:

目前,我能够让以太网适配器与设备 (Nexus 6P) 配合使用.该设备通过以太网连接到本地网络.当 Wi-Fi 接口关闭时,我可以 ping 设备通过以太网连接到的本地网络上的所有设备.但是,我无法访问该网络上任何设备(我知道它们正在运行)的 Web 服务器(不使用 DNS),即通过浏览器应用程序访问 Http.nexus 6p 通过 Ubiquiti Station 通过以太网连接到网络.这似乎是一个路由问题.

Currently, I was able to get an Ethernet adapter working with the device (Nexus 6P). The device is connected to a local network over Ethernet. When the Wi-Fi interface is off, I can ping all devices on the local network the device is connected to over Ethernet. However, I am unable to access the web servers (Not using DNS) of any of the devices on that network (which I know they are running), i.e. Http via a browser app. The nexus 6p is connected to the network over Ethernet via a Ubiquiti Station. This seems to be a routing issue.

我可以在一个应用程序中连接(USB 接口)和使用 Wi-Fi,这让我相信可以使用以太网和 Wi-Fi.

I can tether(usb interface) and use Wi-Fi in one app, so that leads me to believe it is possible to use Ethernet and Wi-Fi.

更新2:

经过更多测试,似乎是权限问题.因为当我 ping 网络时,设备通过以太网连接到没有首先在终端中运行 su 网络不存在.但是,当我运行 su 然后 ping 时,我可以 ping 网络.因此,我的应用似乎需要在访问以太网之前获得超级用户许可.我已授予它超级用户访问权限,但没有任何改变.我从这个 su 是不够的-access-in-android">发布.这是因为 su 只是产生了一个死掉的根 shell.这也解释了为什么我无法通过浏览器应用程序访问该网络上的任何 Web 服务器.是否可以在进行 HTTP 调用时授予我的应用程序访问以太网接口的权限,例如授予 HttpURLConnection 根访问权限,如果这有意义(运行 su 不起作用)?似乎肯定有一个解决方案,因为 HttpURLConnection 可以通过 USB 网络共享接口(Nexus 6P 称之为 rndis0)进行调用.

After more testing, it seems to be that it is a permissions issue. Since when I ping the network the device is connected to over Ethernet without first running su in the terminal the network doesn't exist. However, when I run su then ping, I can ping the network. Thus it seems my app needs to get superuser permission before accessing Ethernet. I've granted it superuser access, but nothing has changed. I read that simply running su isn't enough from one of the comments in this post. This is because su just spawns a root shell that dies. This also explains why I couldn't access any of the web servers on this network via a browser app. Is it possible to grant my app access to the Ethernet interface when making HTTP calls like give HttpURLConnection root access, if that makes any sense (running su doesn't work)? There seems to definitely be a solution since HttpURLConnection can make calls over the USB tethering interface (Nexus 6P calls it rndis0) fine.

更新 3:

我在网上找到 here ,我可以让我的应用程序成为系统应用程序(认为这可能会授予应用程序 eth0 访问权限).我只是将我的应用程序移动到 /system/app 然后重新启动.但是,这似乎并没有赋予应用程序更多的权限(因此不能解决问题),或者除了将其复制到 /system/app 之外,还需要其他一些东西来制作应用程序系统.

I found online here , that I can make my app a System app (thought this might grant the app eth0 access). I just moved my app to /system/app and then rebooted. However, this didn't seem to give the app anymore privileges (thus not solving the problem) , or there is something else required to make the app system than just copying it to /system/app.

更新 4:

所以我能够在没有 root 权限的情况下让以太网在每个应用程序上运行!似乎它只能通过 DHCP 工作并且不喜欢我正在使用的静态连接.它适用于启用 Wi-Fi,但是,启用以太网后,我无法联系 Wi-Fi 网络上的任何设备.有没有解决的办法?和设置两个默认网关有关系吗?

So I was able to get Ethernet working on every app without root permissions! It seemed to be that it only works over DHCP and does not like static connections, which I was using. It works with Wi-Fi enabled, however, I cannot contact any of the devices on the Wi-Fi network when Ethernet is enabled. Is there a way around this? Does it have to do with setting two default gateways?

推荐答案

由于您是在 Nexus 6P 中编程,您可以尝试使用 ConnectivityManager 中添加的新 API 来选择以太网作为您的首选您的进程的网络连接.

Since you were programming in Nexus 6P, you can try to use the new API added in ConnectivityManager to select the ethernet as your preferred network connection for your process.

由于我无法构建像您这样的类似环境,我不确定它是否有效.这只是一个建议的解决方案,完全没有经过测试和验证.

Since I can't build the similar environment like yours, I am not sure if it works. It's just a suggested solution, totally not tested and verified.

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Network etherNetwork = null;
for (Network network : connectivityManager.getAllNetworks()) {
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
    if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
        etherNetwork = network;
    }
}
Network boundNetwork = connectivityManager.getBoundNetworkForProcess();
if (boundNetwork != null) {
    NetworkInfo boundNetworkInfo = connectivityManager.getNetworkInfo(boundNetwork);
    if (boundNetworkInfo.getType() != ConnectivityManager.TYPE_ETHERNET) {
        if (etherNetwork != null) {
            connectivityManager.bindProcessToNetwork(etherNetwork);
        }
    }
}

这篇关于在应用程序中使用多个网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在应用程序中使用多个网络接口

基础教程推荐