本文基于IDO-SBC3566-V1B Android11系统实现开机后以太网自动共享4G网络功能
IDO-SBC3566基于瑞芯微RK3566研发的一款高性能低功耗的智能主板,采用四核A55,主频高达1.8GHz,专为个人移动互联网设备和AIOT设备而设计,内置了多种功能强大的嵌入式硬件引擎,为高端应用提供了优异的性能,支持几乎全格式的4K编解码,内置3DGPU,能够完全兼容OpenGL ES1.1/2.0/3.2、OpenCL2.0和Vulkan1.0.特殊的MMU2D硬件引擎,此外,凭借其强大的兼容性,可以轻松地转换基于TensorFlow/MXNet/PyTorch/Caffe等一系列框架的网络模型。
支持Android,Linux,OpenHarmony,麒麟OS等多种操作系统,可广泛应用于平板、教育平板、带屏音箱、词典笔、云终端、视频会议系统等安卓/Linux类应用方案,以及其他带屏消费类、或轻量级AI应用场景。
添加以太网共享服务
在源码目录下新建以太网共享服务文件,实现系统开机启动时开启以太网共享。
packages/apps/Settings/src/com/android/settings/StartEth1ShareRunnable.java
文件内容如下:
packagecom.android.settings;
//以太网共享
importandroid.app.Service;
importandroid.content.Context;
importandroid.os.Handler;
importandroid.util.Log;
importandroid.content.Intent;
importandroid.os.IBinder;
importandroid.net.ConnectivityManager;
importstaticandroid.net.TetheringManager.TETHERING_ETHERNET;
importandroid.net.ConnectivityManager.OnStartTetheringCallback;
publicclassStartEth1ShareRunnableextendsService{
privatestaticfinalString TAG = "StartEth1ShareRunnable";
privateContext mContext;
//以太网共享
privateConnectivityManager mCm;
privateOnStartTetheringCallback mStartTetheringCallback;
@Override
publicIBinder onBind(Intent arg0){
// TODO Auto-generated method stub
returnnull;
}
@Override
publicvoidonCreate(){
super.onCreate();
mCm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mStartTetheringCallback = newOnStartTetheringCallback();
mHandler.post(StartEth1ShareRunnable);//启用以太网
}
privateHandler mHandler = newHandler();
privateclassOnStartTetheringCallbackextendsConnectivityManager.OnStartTetheringCallback{
@Override
publicvoidonTetheringStarted(){
Log.d(TAG, "ethernet onTetheringStarted!\n");
}
@Override
publicvoidonTetheringFailed(){
Log.e(TAG, "ethernet onTetheringFailed!\n");
}
}
//以太网共享
privateRunnable StartEth1ShareRunnable = newRunnable() {
@Override
publicvoidrun(){
try{
if(mCm != null)
mCm.startTethering(TETHERING_ETHERNET, true, mStartTetheringCallback, mHandler);//开启以太网共享
} catch(Exception e) {
Log.e(TAG, "ethernet startTethering error!\n");
e.printStackTrace();
mHandler.postDelayed(StartEth1ShareRunnable, 10000);//延迟10秒后重新执行
}
}
};
}
在packages/apps/Settings/src/com/android/settings/ 添加一个广播 MyBootReceiver.java 用于接收启动完成事件,在收到广播后调用StartEth1ShareRunnable服务
packagecom.android.settings;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
publicclassMyBootReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Context context, Intent intent){
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent service = newIntent(context, StartEth1ShareRunnable.class);
context.startService(service);
}
}
}
功能验证
SBC3566主板插入可使用的EC20 4G模块以及SIM卡,如下图所示:
从SBC3566主板网口接一根网线到另外一块主板网口上,并给两个主板供电。
在SBC3566系统查看网口及4G获取的IP
共享的网口网络IP:192.168.5.14G网卡IP:10.47.102.124
console:/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:ed:83:f3:50:c4 Driver rk_gmac-dwmac
inet addr:192.168.5.1 Bcast:192.168.5.255 Mask:255.255.255.0
inet6 addr: fe80::7a78:d959:9bff:28cd/64Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:133errors:0dropped:0overruns:0frame:0
TX packets:158errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RX bytes:17758TX bytes:37405
Interrupt:38
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.47.102.124 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:104errors:0dropped:0overruns:0frame:0
TX packets:111errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:3
RX bytes:27734TX bytes:11416
通过ping 百度地址能正常上网
console:/ # ping www.baidu.com
PING www.baidu.com (157.148.69.74) 56(84) bytes ofdata.
64bytes from157.148.69.74: icmp_seq=1ttl=52time=32.8ms
64bytes from157.148.69.74: icmp_seq=2ttl=52time=24.0ms
64bytes from157.148.69.74: icmp_seq=3ttl=52time=24.1ms
64bytes from157.148.69.74: icmp_seq=4ttl=52time=24.4ms
64bytes from157.148.69.74: icmp_seq=5ttl=52time=23.8ms
64bytes from157.148.69.74: icmp_seq=6ttl=52time=24.1ms
64bytes from157.148.69.74: icmp_seq=7ttl=52time=25.4ms
64bytes from157.148.69.74: icmp_seq=8ttl=52time=24.7ms
^C
--- www.baidu.com ping statistics ---
8packets transmitted, 8received, 0% packet loss, time 7012ms
rtt min/avg/max/mdev = 23.808/25.447/32.874/2.852ms
另外一块主板通过网络共享获取到的IP 为192.168.5.X网段
console:/ # ifconfig
eth1 Link encap:Ethernet HWaddr 3a:1d:f7:26:49:0a Driver r8168
inet addr:192.168.5.199 Bcast:192.168.5.255 Mask:255.255.255.0
inet6 addr: fe80::4a59:7a31:cc45:1b53/64Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:286671errors:0dropped:0overruns:0frame:0
TX packets:586errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RX bytes:27238966TX bytes:64962
Interrupt:169Base address:0x3000
通过测试可以ping通互联网网址
console:/ # ping www.baidu.com
PING www.baidu.com (157.148.69.74) 56(84) bytes ofdata.
64bytes from157.148.69.74: icmp_seq=1ttl=51time=37.7ms
64bytes from157.148.69.74: icmp_seq=2ttl=51time=28.7ms
64bytes from157.148.69.74: icmp_seq=3ttl=51time=22.9ms
64bytes from157.148.69.74: icmp_seq=4ttl=51time=23.2ms
64bytes from157.148.69.74: icmp_seq=5ttl=51time=23.4ms
64bytes from157.148.69.74: icmp_seq=6ttl=51time=48.0ms
64bytes from157.148.69.74: icmp_seq=7ttl=51time=23.0ms
64bytes from157.148.69.74: icmp_seq=8ttl=51time=24.9ms
64bytes from157.148.69.74: icmp_seq=9ttl=51time=69.8ms
^C
--- www.baidu.com ping statistics ---
9packets transmitted, 9received, 0% packet loss, time 8012ms
rtt min/avg/max/mdev = 22.930/33.577/69.879/15.184ms
-
Android
+关注
关注
12文章
3943浏览量
127840 -
4G网络
+关注
关注
1文章
143浏览量
26981 -
瑞芯微
+关注
关注
24文章
432浏览量
51505
发布评论请先 登录
相关推荐
评论