android 支持通过外接4G模块联网
重要办理问题:1.开机无法主动拨号
2.手动拨号成功后能获取到IP但无法联网
问题场景:
近来由于业务需求需要在多个不同平台上支持外接USB4g模块联网,开始在全志H6平台上举行很顺利,根据模块厂商提供的开发文档举行相干代码集成就可以使用了。后来在全志A40平台上无论怎么调都无法主动拨号,但是通过linux方式用pppd下令可以拨号成功拿到IP和DNS信息,但是由于没有走android体系的网络切换流程,体系网络信息不会更新,导致无法上网。末了只能采取两种方式结合来实现:即先通过linux下令方式举行拨号,然后把得到的IP和DNS信息设置到android体系并刷新整个网络链路。
移动网络拨号和网络信息更新相干流程可参考下面两篇文章:
路由配置信息的获取_connectivityservice流程图-CSDN博客
ConnectivityService框架初识-CSDN博客
这里重要记录一下怎样动态更新android体系的IP和DNS信息使之能正常联网。
先通过Linux指令:pppd call cmnet 举行拨号,得到IP和dns信息保存下来,在ip-up脚本中处置惩罚的:
#!/system/bin/sh
/system/bin/setprop "net.interfaces.defaultroute" "ppp0"
/system/bin/setprop "net.ppp0.dns1" "$DNS1"
/system/bin/setprop "net.ppp0.dns2" "$DNS2"
/system/bin/setprop "net.ppp0.local-ip" "$IPLOCAL"
/system/bin/setprop "net.ppp0.remote-ip" "$IPREMOTE"
exit 0 动态更新网络IP和dns是在ConnectivityService.java中举行,因为全部形式网络路由信息获取到后都要举行网络信息注册:registerNetworkAgent,注册成功后相干会主动举行相干信息更新,以是动态调用registerNetworkAgent()就可以了。
先看看它的构造函数,弄清楚调用需要传递哪些参数:
public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,
LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
int currentScore, NetworkMisc networkMisc) {} 下面在ConnectivityService.java自定义一个函数updateNetworkByProp,创建所需的各个参数传入registerNetworkAgent,然后在需要的时候调用即可。
private void updateNetworkByProp(){
Messenger messenger = new Messenger(mTrackerHandler);
NetworkInfo networkInfo = new NetworkInfo(0, 0,"MOBILE","LTE");
networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED,"connected","ctnet");
networkInfo.setIsAvailable(true);
LinkProperties linkProperties = new LinkProperties();
linkProperties.setInterfaceName("ppp0");
try {
linkProperties.addLinkAddress(new LinkAddress(InetAddress.getByName(SystemProperties.get("net.ppp0.local-ip")),32));
InetAddress dnsRoute = InetAddress.getByName("0.0.0.0");
linkProperties.addRoute(new RouteInfo(new LinkAddress(dnsRoute,0),dnsRoute,"ppp0"));
InetAddress dns1 = InetAddress.getByName(SystemProperties.get("net.ppp0.dns1"));
InetAddress dns2 = InetAddress.getByName(SystemProperties.get("net.ppp0.dns2"));
linkProperties.addDnsServer(dns1);
linkProperties.addDnsServer(dns2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
linkProperties.setMtu(1500);
linkProperties.setTcpBufferSizes("524288,1048576,2097152,262144,524288,1048576");
NetworkCapabilities networkCapabilities = new NetworkCapabilities();
// networkCapabilities.setNetworkSpecifier("");
networkCapabilities.setLinkUpstreamBandwidthKbps(51200);
networkCapabilities.setLinkDownstreamBandwidthKbps(102400);
networkCapabilities.addCapability(NET_CAPABILITY_INTERNET);
networkCapabilities.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
networkCapabilities.addCapability(NET_CAPABILITY_TRUSTED);
networkCapabilities.addCapability(NET_CAPABILITY_NOT_VPN);
NetworkMisc networkMisc = new NetworkMisc();
//调用注册网络信息函数 registerNetworkAgent(messenger,networkInfo,linkProperties,networkCapabilities,100,networkMisc);
} 对应的打印信息为:
registerNetworkAgent NetworkAgentInfo{ ni{, state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}
network{101}nethandle{433808132830}
lp{{InterfaceName: ppp0 LinkAddresses: Routes: DnsAddresses: Domains: null MTU: 1500 TcpBufferSizes: 524288,1048576,2097152,262144,524288,1048576}}
nc{[ Transports: CELLULAR Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN LinkUpBandwidth>=51200Kbps LinkDnBandwidth>=102400Kbps Specifier: <3>]}
Score{50}everValidated{false}lastValidated{false}created{false} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} }
各字段含义:
[*] NetworkAgentInfo: 这是一个包含网络代理信息的对象。
[*] ni: 表示网络接口信息(Network Interface Info),此中:
[*]type: 网络范例,这里是 MOBILE,表示是移动网络,具体是LTE。
[*]state: 网络连接状态,这里是 CONNECTED/CONNECTED,表示网络已经连接。
[*]reason: 连接成功的原因,这里是 connected。
[*]extra: 额外信息,这里是 ctnet,可能是运营商或网络的标识。
[*]failover: 表示是否是备用网络,这里是 false。
[*]available: 表示网络是否可用,这里是 true。
[*]roaming: 表示是否处于漫游状态,这里是 false。
[*]metered: 表示网络是否是计费网络,这里是 false。
[*] network: 表示网络的ID,这里是 101。
[*] nethandle: 网络句柄,这里是 433808132830。
[*] lp: 表示链路属性(Link Properties),此中:
[*]InterfaceName: 网络接口名称,这里是 ppp0。
[*]LinkAddresses: 网络接口的IP地址,这里是 10.5.66.235/32。
[*]Routes: 路由信息,这里有一个默认路由 0.0.0.0/0 -> 0.0.0.0 ppp0。
[*]DnsAddresses: DNS服务器地址,这里是 202.96.128.86,202.96.134.133。
[*]MTU: 最大传输单位,这里是 1500。
[*]TcpBufferSizes: TCP缓冲区大小。
[*] nc: 表示网络能力(Network Capabilities),此中:
[*]Transports: 传输范例,这里是 CELLULAR。
[*]Capabilities: 网络能力,包括 INTERNET, NOT_RESTRICTED, TRUSTED, NOT_VPN。
[*]LinkUpBandwidth 和 LinkDnBandwidth: 上行和下行带宽,这里分别是 51200Kbps 和 102400Kbps。
[*]Specifier: 网络的特定标识,这里是 <3>。
[*] Score: 网络代理的评分,这里是 50。
[*] everValidated 和 lastValidated: 表示网络是否曾经被验证过,这里都是 false。
[*] created, lingering, explicitlySelected, acceptUnvalidated, everCaptivePortalDetected, lastCaptivePortalDetected: 这些字段描述了网络代理的一些状态和行为,这里大多数都是 false。
更新成功后能得到如下打印信息:
07-29 13:41:07.89722984657 D ConnectivityService: registerNetworkAgent NetworkAgentInfo{ ni{, state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}network{101}nethandle{433808132830}lp{{InterfaceName: ppp0 LinkAddresses: Routes: DnsAddresses: Domains: null MTU: 1500 TcpBufferSizes: 524288,1048576,2097152,262144,524288,1048576}}nc{[ Transports: CELLULAR Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN LinkUpBandwidth>=51200Kbps LinkDnBandwidth>=102400Kbps Specifier: <3>]}Score{50}everValidated{false}lastValidated{false}created{false} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} }
07-29 13:41:07.89822983890 D ConnectivityService: NetworkAgentInfo EVENT_NETWORK_INFO_CHANGED, going from null to CONNECTED
07-29 13:41:07.89922983890 D ConnectivityService: Adding iface ppp0 to network 101
07-29 13:41:07.95122983890 D ConnectivityService: Setting DNS servers for network 101 to
07-29 13:41:07.97322983890 D ConnectivityService: Switching to new default network: NetworkAgentInfo{ ni{, state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}network{101}nethandle{433808132830}lp{{InterfaceName: ppp0 LinkAddresses: Routes: DnsAddresses: Domains: null MTU: 1500 TcpBufferSizes: 524288,1048576,2097152,262144,524288,1048576}}nc{[ Transports: CELLULAR Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN LinkUpBandwidth>=51200Kbps LinkDnBandwidth>=102400Kbps Specifier: <3>]}Score{50}everValidated{false}lastValidated{false}created{true} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} }
07-29 13:41:08.02118902532 V IdletimerController: runCmd(/system/bin/ip6tables -w -t raw -A idletimer_raw_PREROUTING -i ppp0 -j IDLETIMER --timeout 10 --label 0 --send_nl_msg 1) res_ipv4=0, res_ipv6=0
07-29 13:41:08.05918902532 V IdletimerController: runCmd(/system/bin/ip6tables -w -t mangle -A idletimer_mangle_POSTROUTING -o ppp0 -j IDLETIMER --timeout 10 --label 0 --send_nl_msg 1) res_ipv4=0, res_ipv6=0
07-29 13:41:08.06619532064 D alsa_pcm: pcm_open_req, capture card: 3, device: 0, req_rate: 44100
07-29 13:41:08.06622983890 D ConnectivityService: Sending CONNECTED broadcast for type 0 NetworkAgentInfo isDefaultNetwork=true
07-29 13:41:08.06619532064 E audio_hw_primary: XXX pcm_left Unable to open PCM device (cannot open device '/dev/snd/pcmC3D0c': Device or resource busy)
07-29 13:41:08.06619532064 E audio_hw_primary: XXX init_capture_pcm again
07-29 13:41:08.07040384038 I DeviceStatusMonitor: DeviceStatusMonitor$1.onReceive():51 onReceive() : Action = android.net.conn.CONNECTIVITY_CHANGE
07-29 13:41:08.07122983890 D ConnectivityService: NetworkAgentInfo validation passed
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]