ToB企服应用市场:ToB评测及商务社交产业平台

标题: android 支持通过外接4G模块联网 [打印本页]

作者: 南七星之家    时间: 2024-8-29 07:36
标题: 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脚本中处置惩罚的:
  1. #!/system/bin/sh
  2. /system/bin/setprop "net.interfaces.defaultroute" "ppp0"
  3. /system/bin/setprop "net.ppp0.dns1" "$DNS1"
  4. /system/bin/setprop "net.ppp0.dns2" "$DNS2"
  5. /system/bin/setprop "net.ppp0.local-ip" "$IPLOCAL"
  6. /system/bin/setprop "net.ppp0.remote-ip" "$IPREMOTE"
  7. exit 0
复制代码
动态更新网络IP和dns是在ConnectivityService.java中举行,因为全部形式网络路由信息获取到后都要举行网络信息注册:registerNetworkAgent,注册成功后相干会主动举行相干信息更新,以是动态调用registerNetworkAgent()就可以了。
先看看它的构造函数,弄清楚调用需要传递哪些参数:
  1. public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,
  2.             LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
  3.             int currentScore, NetworkMisc networkMisc) {}
复制代码
下面在ConnectivityService.java自定义一个函数updateNetworkByProp,创建所需的各个参数传入registerNetworkAgent,然后在需要的时候调用即可。
  1.     private void updateNetworkByProp(){
  2.         Messenger messenger = new Messenger(mTrackerHandler);
  3.         NetworkInfo networkInfo = new NetworkInfo(0, 0,"MOBILE","LTE");
  4.         networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED,"connected","ctnet");
  5.         networkInfo.setIsAvailable(true);
  6.         LinkProperties linkProperties = new LinkProperties();
  7.         linkProperties.setInterfaceName("ppp0");
  8.         try {
  9.             linkProperties.addLinkAddress(new LinkAddress(InetAddress.getByName(SystemProperties.get("net.ppp0.local-ip")),32));
  10.             InetAddress dnsRoute = InetAddress.getByName("0.0.0.0");
  11.             linkProperties.addRoute(new RouteInfo(new LinkAddress(dnsRoute,0),dnsRoute,"ppp0"));
  12.             InetAddress dns1 = InetAddress.getByName(SystemProperties.get("net.ppp0.dns1"));
  13.             InetAddress dns2 = InetAddress.getByName(SystemProperties.get("net.ppp0.dns2"));
  14.             linkProperties.addDnsServer(dns1);
  15.             linkProperties.addDnsServer(dns2);
  16.         } catch (UnknownHostException e) {
  17.             e.printStackTrace();
  18.         }
  19.         linkProperties.setMtu(1500);
  20.         linkProperties.setTcpBufferSizes("524288,1048576,2097152,262144,524288,1048576");
  21.         NetworkCapabilities networkCapabilities = new NetworkCapabilities();
  22.        // networkCapabilities.setNetworkSpecifier("");
  23.         networkCapabilities.setLinkUpstreamBandwidthKbps(51200);
  24.         networkCapabilities.setLinkDownstreamBandwidthKbps(102400);
  25.         networkCapabilities.addCapability(NET_CAPABILITY_INTERNET);
  26.         networkCapabilities.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
  27.         networkCapabilities.addCapability(NET_CAPABILITY_TRUSTED);
  28.         networkCapabilities.addCapability(NET_CAPABILITY_NOT_VPN);
  29.         NetworkMisc networkMisc = new NetworkMisc();
  30. //调用注册网络信息函数        registerNetworkAgent(messenger,networkInfo,linkProperties,networkCapabilities,100,networkMisc);
  31.     }
复制代码
对应的打印信息为:
  1. registerNetworkAgent NetworkAgentInfo{ ni{[type: MOBILE[LTE], state: CONNECTED/CONNECTED, reason: connected, extra: ctnet, failover: false, available: true, roaming: false, metered: false]}  
  2.         network{101}  nethandle{433808132830}  
  3.         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}}
  4.          nc{[ Transports: CELLULAR Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN LinkUpBandwidth>=51200Kbps LinkDnBandwidth>=102400Kbps Specifier: <3>]}
  5.           Score{50}  everValidated{false}  lastValidated{false}  created{false} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} }
  6.         
复制代码
各字段含义:
更新成功后能得到如下打印信息:
  1. 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} }
  2. 07-29 13:41:07.898  2298  3890 D ConnectivityService: NetworkAgentInfo [MOBILE (LTE) - 101] EVENT_NETWORK_INFO_CHANGED, going from null to CONNECTED
  3. 07-29 13:41:07.899  2298  3890 D ConnectivityService: Adding iface ppp0 to network 101
  4. 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]
  5. 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} }
  6. 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
  7. 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
  8. 07-29 13:41:08.066  1953  2064 D alsa_pcm: pcm_open_req, capture card: 3, device: 0, req_rate: 44100
  9. 07-29 13:41:08.066  2298  3890 D ConnectivityService: Sending CONNECTED broadcast for type 0 NetworkAgentInfo [MOBILE (LTE) - 101] isDefaultNetwork=true
  10. 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)
  11. 07-29 13:41:08.066  1953  2064 E audio_hw_primary: XXX init_capture_pcm again
  12. 07-29 13:41:08.070  4038  4038 I DeviceStatusMonitor: DeviceStatusMonitor$1.onReceive():51 onReceive() : Action = android.net.conn.CONNECTIVITY_CHANGE
  13. 07-29 13:41:08.071  2298  3890 D ConnectivityService: NetworkAgentInfo [MOBILE (LTE) - 101] validation passed
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4