在一个应用程序中使用多个网络接口 [英] Use multiple network interfaces in an app

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

问题描述

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

在具有两个网络接口设置的手机上的一个android应用程序中,是否可以为某些http请求使用一个网络接口而为其他HTTP请求使用另一个网络接口进行指定?是否需要通过路由表而不是Java来完成此操作?

我正在使用的电话是根植的Nexus 6p.

更新:

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

我可以绑定(usb界面)并在一个应用程序中使用Wi-Fi,因此使我相信可以使用以太网和Wi-Fi.

Update2 :

经过更多测试后,似乎是权限问题.因为当我对网络执行ping操作时,设备无需先在终端中运行su即可通过以太网连接,所以该网络不存在.但是,当我运行su然后ping时,我可以ping通网络.因此,似乎我的应用需要在访问以太网之前获得超级用户许可.我已经授予它超级用户访问权限,但没有任何改变.我读到仅运行su不足以满足该这里,这样我就可以将我的应用程序设置为系统应用程序(认为这可能会授予应用程序eth0访问权限).我只是将我的应用程序移至/system/app,然后重新启动.但是,这似乎并没有赋予该应用程序更多的特权(因此无法解决问题),或者除了将应用程序系统复制到/system/app之外,还需要其他一些东西来构成该应用程序系统.

更新4 :

因此,我能够在没有root用户权限的情况下在每个应用程序上使用以太网!看来它只能在DHCP上运行,而不喜欢我正在使用的静态连接.它可以在启用Wi-Fi的情况下工作,但是,启用以太网后,我无法联系Wi-Fi网络上的任何设备.有没有解决的办法?与设置两个默认网关有关吗?

解决方案

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

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

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);
        }
    }
}

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.

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?

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

Update:

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.

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.

Update2:

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.

Update 3:

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.

Update 4:

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?

解决方案

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);
        }
    }
}

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

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