1

我正在尝试构建一个使用 USB RNDIS 进行网络通信的应用程序。我想获得通过 Android 手机上的 USB RNDIS 设置的默认网关 IP。但是,我编写的代码仅适用于具有 USB Tethering 的手机(手机连接正确等)。在带有 USB RNDIS 的手机上,我得到了 connectivityManager.getAllNetworks()` 返回 null。我在connectivityManager中看到了以下方法:

/**
 * Attempt to both alter the mode of USB and Tethering of USB.  A
 * utility method to deal with some of the complexity of USB - will
 * attempt to switch to Rndis and subsequently tether the resulting
 * interface on {@code true} or turn off tethering and switch off
 * Rndis on {@code false}.
 *
 * <p>This method requires the caller to hold either the
 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
 * or the ability to modify system settings as determined by
 * {@link android.provider.Settings.System#canWrite}.</p>
 *
 * @param enable a boolean - {@code true} to enable tethering
 * @return error a {@code TETHER_ERROR} value indicating success or failure type
 *
 * {@hide}
 */
public int setUsbTethering(boolean enable) {
    try {
        String pkgName = mContext.getOpPackageName();
        Log.i(TAG, "setUsbTethering caller:" + pkgName);
        return mService.setUsbTethering(enable, pkgName);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

但是由于某种原因该方法不可用(不可用)......?

我尝试过的代码如下:

 /**
     * Automatically get the IP from the device that is set as Default Gateway in the RHM. 
     * Summary:
     * ConnectivityManager.getLinkProperties() -> LinkProperties.getRoutes() -> RouteInfo.getGateway()
     */
    private String getIpDefaultGateway() {
        String webSockProtocol = "wss:/";
        String webSockPort     = ":443/";
        String uri             = "";

        ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager != null) {
//            connectivityManager.setUsbTethering(true); <------- THIS DOES NOT EXIST and not able to import something etc...
            Network[] networkAll = connectivityManager.getAllNetworks();  \\<---------returns Null on RNDIS, works ok in Tethering.
            for (Network n : networkAll){
                Log.w(TAG, "---------------------------------------------------------------");
                Log.w(TAG, "getIpDefaultGateway: Network" + n.toString());
            }

            //get the network obj corresponding to the currently active default data network.
            Network network = connectivityManager.getActiveNetwork();
            if (network != null) {
                // Get the properties of a network link that represents a connection to a network.
                // It may have multiple addresses and multiple gateways.
                LinkProperties linkProperties;
                linkProperties = connectivityManager.getLinkProperties(network);
                if (linkProperties != null) {
                    // get network configuration information that contains a gateway and InetAddress
                    // indicating the next hop to use. If this is null it indicates a directly-connected route.
                    List<RouteInfo> routeInfo = linkProperties.getRoutes();
                    RouteInfo routeForDefaultGateway;
                    // find the gateway ip
                    for (RouteInfo ri : routeInfo) {
                        if (ri.isDefaultRoute() && ri.getGateway() instanceof Inet4Address) {
                            routeForDefaultGateway = ri;
                            uri = webSockProtocol + routeForDefaultGateway.getGateway() + webSockPort;
                            Log.w(TAG, " getIpDefaultGateway: uri: " + uri, null);
                        }
                    }
                } else {
                    Log.w(TAG, " getIpDefaultGateway: No active links to represent connection to a network.");
                }
            } else {
                Log.e(TAG, " getIpDefaultGateway: null network");
            }
        } else {
            Log.e(TAG, " getIpDefaultGateway: null connectivityManager");
        }
        return uri;
    }

任何帮助将非常感激。

4

0 回答 0