将所有客户端套接字绑定到wifi接口 [英] Bind all client sockets to wifi interface

查看:85
本文介绍了将所有客户端套接字绑定到wifi接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的运动相机编写客户端应用程序.当智能手机连接到wifi和移动网络时,使用网络连接会出现问题:Android确定wifi没有连接到互联网,因此所有请求都传递到了移动网络接口.我已经实现了客户端API,该API使用预先绑定的套接字与摄像机通信:

I'm writing a client application for my action camera. There's an issue using network connections when smartphone connected to wifi and mobile network: Android determines, that wifi does not connected to internet, so all requests passed to mobile network interface. I've already implemented the client API, that uses pre-bounded sockets to communicate with camera:

public class MainActivity extends AppCompatActivity {
    private Network wifi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
        requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.requestNetwork(requestBuilder.build(), new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                wifi = network;
            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
                wifi = null;
            }
        });
    }

    Socket initSocket() {
        Socket sock = new Socket();
        if (wifi != null) {
            wifi.bindSocket(sock);
        }
        else {
            System.out.println("Warning: wifi network is null");
        }
        return sock;
    }
}

此外,我实现了一个简单的http客户端来使用.因此,任何API请求都类似于:

Also, i've implemeneted simple http-client to work with. So, any API request looks like:

SomeCommandResult executeSomeCommand() {
    Socket sock = initSocket();
    HttpPacket res = API.performHttpRequest(sock, "GET /?command=some-command");
    return (SomeCommandResult) parseXML(new String(res.body));
}

但是我在实现其他组件时遇到了一些问题,因此现在我正在寻找将所有套接字绑定到wifi接口的另一种方法(包括内部网络用法:ImageView,VideoView等).我发现了 Socket.setSocketImplFactory(...):

But i've faced some problems implementing other components, so now i'm looking for another way to bind all sockets to the wifi interface (including internal net usage: ImageView, VideoView, etc). I've discovered Socket.setSocketImplFactory(...):

    protected void onCreate(Bundle savedInstanceState) {
        ...

        Socket.setSocketImplFactory(new SocketImplFactory() {
            public SocketImpl createSocketImpl() {
                return new MySocketImpl();
            }
        });
    }

但是我不知道如何实现自己的 MySocketImpl .

But i have no idea how to implement own MySocketImpl.

我需要做的就是在实际建立连接之前拦截所有传出的套接字连接并将它们绑定到wifi iface(强制使用wifi).需要帮助来实现 MySocketImpl .也许有人对实现该目标有另一种想法?

All i need is to intercept all outgoing socket connections and bind them to the wifi iface before connection actually established (force use wifi). Need help imlementing MySocketImpl. Or maybe someone have another idea how to achieve it?

推荐答案

感谢

Thanks for ianhanniballake's answer and corresponding blog post (Connecting your App to a Wi-Fi Device), now I know, there's a ConnectivityManager.bindProcessToNetwork(Network network) method.

所以,这就是我所做的:

So, this is how I've done:

public class MyApp extends Application {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
        requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.requestNetwork(requestBuilder.build(), new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                cm.bindProcessToNetwork(network);
            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
            }
        });
    }
}

这篇关于将所有客户端套接字绑定到wifi接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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