Hello,Dean,
By default, we can't set static IP for ethernet on android platform. But android supplies SDK for user to realize it. we can add "Ethernet parameters setting" item in "Settings" menu, then load NetwokUtils and EthnetNative to set neccessary parameter.
For more detailed information, you can look into the corresponding documnets and get more usages on your purpose.
Regards,
Weidong
Hi Weidong,
we can add "Ethernet parameters setting" item in "Settings" menu, then load NetwokUtils and EthnetNative to set neccessary parameter.
Do you mean that we don't need to modify framework, and we just need to add a option in "Settings" or using other APKs to load NetwokUtils and EthnetNative?
BRs.
Dean
Hello,Dean,
I checked API in networkutils:
private native static boolean configureNative(
String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
I think if you load it in your setting program, static ip should be configured. I got a section of code from internet, but i didn't test, you can test it if it is OK:
ConnectivityManager mService = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EthernetManager mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);
if(mEthManager.getState() == EthernetManager.ETHERNET_STATE_ENABLED) {
EthernetDevInfo mInterfaceInfo = mEthManager.getSavedConfig();
String mIp;
String mMask;
String mGw;
String mDns;
mIp = "192.168.0.118";
mMask = "255.255.255.0";
mGw = "192.168.0.1";
mDns = "192.168.0.1";
mInterfaceInfo.setConnectMode(EthernetDevInfo.ETHERNET_CONN_MODE_MANUAL);
mInterfaceInfo.setIpAddress(mIp);
mInterfaceInfo.setNetMask(mMask);
mInterfaceInfo.setDnsAddr(mDns);
mInterfaceInfo.setGateWay(mGw);
try{
mEthManager.updateDevInfo(mInterfaceInfo);
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(this, "Ethernet state disabled!", 5000).show();
}
Regards,
Weidong
Hi Weidong,
Thanks for your suggestion.
I didn't test this yet, but Android source code seems to use networkutils.configureInteface() instead of networkutils.native(), right?
According to your sample, you use updateDevInfo().
Main flow is EthernetManager.updateDevInfo() -> EthernetService.updateDevInfo() -> EthernetStateTracker.resetInterface() -> networkutils.configureInterface().
But there is no these classes before doing ethernet patch. Can I use networkutils.configureInterface() directly?