我正在尝试建立一个应用程序,将使用usb rndis进行网络通信。我想得到默认的网关ip是通过usb rndis的安卓手机上设置。然而,我写的代码只适用于有usb连接的手机(手机连接正确等等)。在使用usb rndis的手机上,我获取空connectivitymanager.getallnetworks()`返回空值。我在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;
}
任何帮助都将不胜感激。
暂无答案!
目前还没有任何答案,快来回答吧!