android 支持通过外接4G模块联网

avatar
作者
筋斗云
阅读量:0

主要解决问题:

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{[type: MOBILE[LTE], state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}           network{101}  nethandle{433808132830}           lp{{InterfaceName: ppp0 LinkAddresses: [10.5.66.235/32,]  Routes: [0.0.0.0/0 -> 0.0.0.0 ppp0,] DnsAddresses: [202.96.128.86,202.96.134.133,] 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} }         

各字段含义:

  1. NetworkAgentInfo: 这是一个包含网络代理信息的对象。

  2. ni: 表示网络接口信息(Network Interface Info),其中:

    • type: 网络类型,这里是 MOBILE[LTE],表示是移动网络,具体是LTE。
    • state: 网络连接状态,这里是 CONNECTED/CONNECTED,表示网络已经连接。
    • reason: 连接成功的原因,这里是 connected
    • extra: 额外信息,这里是 ctnet,可能是运营商或网络的标识。
    • failover: 表示是否是备用网络,这里是 false
    • available: 表示网络是否可用,这里是 true
    • roaming: 表示是否处于漫游状态,这里是 false
    • metered: 表示网络是否是计费网络,这里是 false
  3. network: 表示网络的ID,这里是 101

  4. nethandle: 网络句柄,这里是 433808132830

  5. 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缓冲区大小。
  6. nc: 表示网络能力(Network Capabilities),其中:

    • Transports: 传输类型,这里是 CELLULAR
    • Capabilities: 网络能力,包括 INTERNETNOT_RESTRICTEDTRUSTEDNOT_VPN
    • LinkUpBandwidth 和 LinkDnBandwidth: 上行和下行带宽,这里分别是 51200Kbps 和 102400Kbps
    • Specifier: 网络的特定标识,这里是 <3>
  7. Score: 网络代理的评分,这里是 50

  8. everValidatedlastValidated: 表示网络是否曾经被验证过,这里都是 false

  9. created, lingering, explicitlySelected, acceptUnvalidated, everCaptivePortalDetected, lastCaptivePortalDetected: 这些字段描述了网络代理的一些状态和行为,这里大多数都是 false

更新成功后能得到如下打印信息:

07-29 13:41:07.897  2298  4657 D ConnectivityService: registerNetworkAgent NetworkAgentInfo{ ni{[type: MOBILE[LTE], state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}  network{101}  nethandle{433808132830}  lp{{InterfaceName: ppp0 LinkAddresses: [10.5.66.235/32,]  Routes: [0.0.0.0/0 -> 0.0.0.0 ppp0,] DnsAddresses: [202.96.128.86,202.96.134.133,] 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.898  2298  3890 D ConnectivityService: NetworkAgentInfo [MOBILE (LTE) - 101] EVENT_NETWORK_INFO_CHANGED, going from null to CONNECTED 07-29 13:41:07.899  2298  3890 D ConnectivityService: Adding iface ppp0 to network 101 07-29 13:41:07.951  2298  3890 D ConnectivityService: Setting DNS servers for network 101 to [/202.96.128.86, /202.96.134.133] 07-29 13:41:07.973  2298  3890 D ConnectivityService: Switching to new default network: NetworkAgentInfo{ ni{[type: MOBILE[LTE], state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}  network{101}  nethandle{433808132830}  lp{{InterfaceName: ppp0 LinkAddresses: [10.5.66.235/32,]  Routes: [0.0.0.0/0 -> 0.0.0.0 ppp0,] DnsAddresses: [202.96.128.86,202.96.134.133,] 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.021  1890  2532 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.059  1890  2532 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.066  1953  2064 D alsa_pcm: pcm_open_req, capture card: 3, device: 0, req_rate: 44100 07-29 13:41:08.066  2298  3890 D ConnectivityService: Sending CONNECTED broadcast for type 0 NetworkAgentInfo [MOBILE (LTE) - 101] isDefaultNetwork=true 07-29 13:41:08.066  1953  2064 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.066  1953  2064 E audio_hw_primary: XXX init_capture_pcm again 07-29 13:41:08.070  4038  4038 I DeviceStatusMonitor: DeviceStatusMonitor$1.onReceive():51 onReceive() : Action = android.net.conn.CONNECTIVITY_CHANGE 07-29 13:41:08.071  2298  3890 D ConnectivityService: NetworkAgentInfo [MOBILE (LTE) - 101] validation passed

    广告一刻

    为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!