Use multiple network interfaces in an app

前端 未结 3 639
灰色年华
灰色年华 2021-02-19 13:04

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.

3条回答
  •  醉话见心
    2021-02-19 13:29

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

提交回复
热议问题