Android 9以太网与WIFI共存
常规方法是的把以下注释就可以了,实际操作起来有差异:
frameworks\base\services\core\java\com\android\server
ConnectivityService.java
private void teardownUnneededNetwork(NetworkAgentInfo nai) {
if (nai.numRequestNetworkRequests() != 0) {
for (int i = 0; i < nai.numNetworkRequests(); i++) {
NetworkRequest nr = nai.requestAt(i);
// Ignore listening requests.
if (nr.isListen()) continue;
loge("Dead network still had at least " + nr);
break;
}
}
// nai.asyncChannel.disconnect();
}
private class NetworkStateTrackerHandler extends Handler {
public NetworkStateTrackerHandler(Looper looper) {
super(looper);
}
private boolean maybeHandleAsyncChannelMessage(Message msg) {
switch (msg.what) {
default:
return false;
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
handleAsyncChannelHalfConnect(msg);
break;
}
case AsyncChannel.CMD_CHANNEL_DISCONNECT: {
NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);
// if (nai != null) nai.asyncChannel.disconnect();
break;
}
case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
handleAsyncChannelDisconnected(msg);
break;
}
}
return true;
}
frameworks\base\core\java\android\net
NetworkFactory.java
private void evalRequest(NetworkRequestInfo n) {
if (VDBG) log("evalRequest");
if (n.requested == false && n.score < mScore &&
n.request.networkCapabilities.satisfiedByNetworkCapabilities(
mCapabilityFilter) && acceptRequest(n.request, n.score)) {
if (VDBG) log(" needNetworkFor");
needNetworkFor(n.request, n.score);
n.requested = true;
} else if (n.requested == true &&
(n.score > mScore || n.request.networkCapabilities.satisfiedByNetworkCapabilities(
mCapabilityFilter) == false || acceptRequest(n.request, n.score) == false)) {
if (VDBG) log(" releaseNetworkFor");
// releaseNetworkFor(n.request);
// n.requested = false;
} else {
if (VDBG) log(" done");
}
}
以上是常规操作,实现以太网与WIFI共存。
实际场景是以太网连接一个摄像头,实时传输视频。
连接摄像头传输视频正常,WIFI上网也正常。
不正常情况:以上基础上,关闭WIFI开关,再打开,视频会断几秒,看log:
07-05 08:54:10.598 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.WIFI_STATE_CHANGED flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:10.599 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.WIFI_STATE_CHANGED flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:10.622 +0000 1438 1631 E WifiP2pService: Unhandled message { when=0 what=131203 target=com.android.internal.util.StateMachine$SmHandler }
07-05 08:54:10.631 +0000 1438 1631 E WifiP2pService: Unhandled message { when=0 what=131203 target=com.android.internal.util.StateMachine$SmHandler }
07-05 08:54:11.481 +0000 923 923 W wificond: Scan is not started. Ignore abort request
07-05 08:54:11.510 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.510 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:11.539 +0000 1438 1460 I EthernetTracker: interfaceLinkStateChanged, iface: wlan0, up: false
07-05 08:54:11.541 +0000 1438 1460 I chatty : uid=1000(system) android.fg identical 2 lines
07-05 08:54:11.541 +0000 1438 1460 I EthernetTracker: interfaceLinkStateChanged, iface: wlan0, up: false
07-05 08:54:11.544 +0000 1438 1460 I EthernetTracker: interfaceLinkStateChanged, iface: wlan0, up: true
07-05 08:54:11.554 +0000 1438 1460 I chatty : uid=1000(system) android.fg identical 6 lines
07-05 08:54:11.554 +0000 1438 1460 I EthernetTracker: interfaceLinkStateChanged, iface: wlan0, up: true
07-05 08:54:11.557 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.557 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:11.558 +0000 1438 1600 D ConnectivityService: registerNetworkAgent NetworkAgentInfo{ ni{[type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: true, roaming: false]} network{106} nethandle{458672230413} lp{{InterfaceName: wlan0 LinkAddresses: [192.168.50.253/24,] Routes: [192.168.50.0/24 -> 0.0.0.0 wlan0,0.0.0.0/0 -> 192.168.50.1 wlan0,] DnsAddresses: [192.168.50.1,] UsePrivateDns: false PrivateDnsServerName: null Domains: null MTU: 0 TcpBufferSizes: 524288,1048576,2097152,262144,524288,1048576}} nc{[ Transports: WIFI Capabilities: NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED Unwanted: LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps SSID: "ASUS_5G"]} Score{20} everValidated{false} lastValidated{false} created{false} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} clat{null} }
07-05 08:54:11.559 +0000 1438 1635 D ConnectivityService: NetworkAgentInfo [WIFI () - 106] EVENT_NETWORK_INFO_CHANGED, going from null to CONNECTING
07-05 08:54:11.561 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.563 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:11.584 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.RSSI_CHANGED flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.586 +0000 1438 1635 D ConnectivityService: NetworkAgentInfo [WIFI () - 106] EVENT_NETWORK_INFO_CHANGED, going from CONNECTING to CONNECTING
07-05 08:54:11.625 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.625 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:11.639 +0000 1438 1635 D ConnectivityService: NetworkAgentInfo [WIFI () - 106] EVENT_NETWORK_INFO_CHANGED, going from CONNECTING to CONNECTED
07-05 08:54:11.640 +0000 1438 1635 W DnsManager: updatePrivateDns(106, PrivateDnsConfig{true:/[]})
07-05 08:54:11.640 +0000 1438 1635 D ConnectivityService: Setting DNS servers for network 106 to [/192.168.50.1]
07-05 08:54:11.640 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.640 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:11.640 +0000 1438 1635 D DnsManager: setDnsConfigurationForNetwork(106, [192.168.50.1], [], [1800, 25, 8, 64], , [192.168.50.1])
07-05 08:54:11.642 +0000 1438 1635 D ConnectivityService: Adding iface wlan0 to network 106
07-05 08:54:11.647 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.StartNetWorkUpdate
07-05 08:54:11.650 +0000 1438 1454 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.net.wifi.STATE_CHANGE flg=0x4000010 (has extras) } to com.qualcomm.update/.background.NetworkConnectChangedReceiver
07-05 08:54:11.664 +0000 1438 1635 D ConnectivityService: Setting DNS servers for network 106 to [/192.168.50.1]
07-05 08:54:11.665 +0000 1438 1635 D DnsManager: setDnsConfigurationForNetwork(106, [192.168.50.1], [], [1800, 25, 8, 64], , [192.168.50.1])
07-05 08:54:11.709 +0000 1438 1635 D ConnectivityService: NetworkAgentInfo [WIFI () - 106] validation passed
07-05 08:54:11.713 +0000 1438 1635 D ConnectivityService: Switching to new default network: NetworkAgentInfo{ ni{[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: true, roaming: false]} network{106} nethandle{458672230413} lp{{InterfaceName: wlan0 LinkAddresses: [192.168.50.253/24,] Routes: [fe80::/64 -> :: wlan0,192.168.50.0/24 -> 0.0.0.0 wlan0,0.0.0.0/0 -> 192.168.50.1 wlan0,] DnsAddresses: [192.168.50.1,] UsePrivateDns: false PrivateDnsServerName: null Domains: null MTU: 0 TcpBufferSizes: 524288,1048576,2097152,262144,524288,1048576}} nc{[ Transports: WIFI Capabilities: NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED Unwanted: LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps SignalStrength: -91 SSID: "ASUS_5G"]} Score{41} everValidated{true} lastValidated{true} created{true} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} clat{null} }
07-05 08:54:11.714 +0000 918 1140 E NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'INTERFACE' not found
07-05 08:54:11.714 +0000 918 1140 E NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'STATE' not found
07-05 08:54:11.714 +0000 918 1140 E NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'TIME_NS' not found
07-05 08:54:11.714 +0000 918 1140 E NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'UID' not found
07-05 08:54:11.759 +0000 1438 1635 D ConnectivityService: Lingering NetworkAgentInfo [Ethernet () - 105] for 30000ms
07-05 08:54:11.759 +0000 1438 1635 D ConnectivityService: Sending DISCONNECTED broadcast for type 9 NetworkAgentInfo [Ethernet () - 105] isDefaultNetwork=true
07-05 08:54:11.760 +0000 1438 1635 D ConnectivityService: Sending CONNECTED broadcast for type 1 NetworkAgentInfo [WIFI () - 106] isDefaultNetwork=true
07-05 08:54:11.763 +0000 1788 1788 E SystemUIService: SystemUIService NetworkChangeReceiver
07-05 08:54:11.763 +0000 1788 1788 E SystemUIService: SystemUIService Connected to WIFI mFlay:false
07-05
以上分析出,打开WIFI开关连接时,会发送断开以太网的广播,然后又将以太网连接上:
07-05 08:54:11.759 +0000 1438 1635 D ConnectivityService: Sending DISCONNECTED broadcast for type 9 NetworkAgentInfo [Ethernet () - 105] isDefaultNetwork=true
07-05 08:54:11.760 +0000 1438 1635 D ConnectivityService: Sending CONNECTED broadcast for type 1 NetworkAgentInfo [WIFI () - 106] isDefaultNetwork=true
这个应该就是视频断开一下,再连接上的原因。
解决思路,在以太网口连接时,不发送断开以太网的广播。
还是在
frameworks\base\services\core\java\com\android\server
ConnectivityService.java
public void add(int type, NetworkAgentInfo nai) {
if (!isTypeSupported(type)) {
return; // Invalid network type.
}
if (VDBG) log("Adding agent " + nai + " for legacy network type " + type);
ArrayList<NetworkAgentInfo> list = mTypeLists[type];
if (list.contains(nai)) {
return;
}
synchronized (mTypeLists) {
list.add(nai);
}
// Send a broadcast if this is the first network of its type or if it's the default.
final boolean isDefaultNetwork = isDefaultNetwork(nai);
// if ((list.size() == 1) || isDefaultNetwork){
if (((list.size() == 1) || isDefaultNetwork) && !isEthernetConnected()) {//连接WIFI时断开以太网
maybeLogBroadcast(nai, DetailedState.CONNECTED, type, isDefaultNetwork);
sendLegacyNetworkBroadcast(nai, DetailedState.CONNECTED, type);
}
}
/** Removes the given network from the specified legacy type list. */
public void remove(int type, NetworkAgentInfo nai, boolean wasDefault) {
ArrayList<NetworkAgentInfo> list = mTypeLists[type];
if (list == null || list.isEmpty()) {
return;
}
final boolean wasFirstNetwork = list.get(0).equals(nai);
synchronized (mTypeLists) {
if (!list.remove(nai)) {
return;
}
}
final DetailedState state = DetailedState.DISCONNECTED;
// if (wasFirstNetwork || wasDefault) {
if ((wasFirstNetwork || wasDefault)&& !isEthernetConnected()) {//连接WIFI时断开以太网
maybeLogBroadcast(nai, state, type, wasDefault);
sendLegacyNetworkBroadcast(nai, state, type);
}
if (!list.isEmpty() && wasFirstNetwork) {
if (DBG) log("Other network available for type " + type +
", sending connected broadcast");
final NetworkAgentInfo replacement = list.get(0);
maybeLogBroadcast(replacement, state, type, isDefaultNetwork(replacement));
sendLegacyNetworkBroadcast(replacement, state, type);
}
}
private boolean isEthernetConnected() {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (intf.getName().startsWith("eth")) {
if (intf.isUp()) {
android.util.Log.d(TAG, "isEthernetConnected:true");
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
android.util.Log.d(TAG, "isEthernetConnected:false");
return false;
}