本修改基于RK3588 Android12SDK,调试功能为双网口,网口2在设置里面不能获取到IP地址,根据排查是因为在EthernetSettings.java中,onCreat中这一段写死了eth0,导致后续eth1获取不到IP。
String[] ifaces = mEthManager.getAvailableInterfaces();
if (ifaces.length > 0) {
mIfaceName = ifaces[0];//"eth0";
}
根据排查到的问题,我们要动态的识别到网口插入,并且正确的切换到对应的网口,所以我们可以根据sys下的节点sys/class/net/eth0(eth1)/carrier判断是eth0或eth1插入,再讲插入的网口赋值给mIfaceName。
代码如下:
private static final int ETHERNET_NUMBER = 2;
private String get_mIfaceName() {
String[] ifaces = mEthManager.getAvailableInterfaces();
if (ifaces.length > 0) {
for(int i=0;i<ETHERNET_NUMBER;i++){
int carrier = getEthernetCarrierState(ifaces[i]);
Log.d(TAG, "cgl ============= ifaces = "+ifaces[i]+" carrier = " +carrier);
if(carrier == 1)
mIfaceName = ifaces[i];
}
}
return mIfaceName;
}
我是在onCreateDialog和BroadcastReceiver中添加mIfaceName的判断,代码如下:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
log("Action " + action);
+ mIfaceName = get_mIfaceName();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
Log.v(TAG, "===" + info.toString());
if (null != info && ConnectivityManager.TYPE_ETHERNET == info.getType()) {
long currentTime = System.currentTimeMillis();
int delayTime = 0;
if (currentTime - mChangeTime < 1000) {
delayTime = 2000;
}
if (NetworkInfo.State.CONNECTED == info.getState()) {
handleEtherStateChange(ETHERNET_STATE.ETHER_STATE_CONNECTED, delayTime);
} else if (NetworkInfo.State.DISCONNECTED == info.getState()) {
handleEtherStateChange(ETHERNET_STATE.ETHER_STATE_DISCONNECTED, delayTime);
}
}
}
}
/*.
-------------------------------------------------------------------------------------------------------------------------------
.*/
public Dialog onCreateDialog(int dialogId) {
log("onCreateDialog " + dialogId);
+ mIfaceName = get_mIfaceName();
switch (dialogId) {
case SHOW_RENAME_DIALOG:
mDialog = new ethernet_static_ip_dialog(getActivity(), false, this, mGetStaticIpInfo, mIfaceName);
return mDialog;
}
return super.onCreateDialog(dialogId);
}