本文基于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应用场景。
产品规格书:IDO-SBC3566-V1B 产品手册
添加以太网共享服务
在源码目录下新建以太网共享服务文件,实现系统开机启动时开启以太网共享。
packages/apps/Settings/src/com/android/settings/StartEth1ShareRunnable.java
文件内容如下:
package com.android.settings;
//以太网共享
import android.app.Service;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.content.Intent;
import android.os.IBinder;
import android.net.ConnectivityManager;
import static android.net.TetheringManager.TETHERING_ETHERNET;
import android.net.ConnectivityManager.OnStartTetheringCallback;
public class StartEth1ShareRunnable extends Service {
private static final String TAG = "StartEth1ShareRunnable";
private Context mContext;
//以太网共享
private ConnectivityManager mCm;
private OnStartTetheringCallback mStartTetheringCallback;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
mCm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mStartTetheringCallback = new OnStartTetheringCallback();
mHandler.post(StartEth1ShareRunnable);//启用以太网
}
private Handler mHandler = new Handler();
private class OnStartTetheringCallback extends ConnectivityManager.OnStartTetheringCallback {
@Override
public void onTetheringStarted() {
Log.d(TAG, "ethernet onTetheringStarted!\n");
}
@Override
public void onTetheringFailed() {
Log.e(TAG, "ethernet onTetheringFailed!\n");
}
}
//以太网共享
private Runnable StartEth1ShareRunnable = new Runnable() {
@Override
public void run() {
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服务
package com.android.settings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent service = new Intent(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/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:133 errors:0 dropped:0 overruns:0 frame:0
TX packets:158 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:17758 TX 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:104 errors:0 dropped:0 overruns:0 frame:0
TX packets:111 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:27734 TX bytes:11416
通过ping 百度地址能正常上网
console:/ # ping www.baidu.com
PING www.baidu.com (157.148.69.74) 56(84) bytes of data.
64 bytes from 157.148.69.74: icmp_seq=1 ttl=52 time=32.8 ms
64 bytes from 157.148.69.74: icmp_seq=2 ttl=52 time=24.0 ms
64 bytes from 157.148.69.74: icmp_seq=3 ttl=52 time=24.1 ms
64 bytes from 157.148.69.74: icmp_seq=4 ttl=52 time=24.4 ms
64 bytes from 157.148.69.74: icmp_seq=5 ttl=52 time=23.8 ms
64 bytes from 157.148.69.74: icmp_seq=6 ttl=52 time=24.1 ms
64 bytes from 157.148.69.74: icmp_seq=7 ttl=52 time=25.4 ms
64 bytes from 157.148.69.74: icmp_seq=8 ttl=52 time=24.7 ms
^C
--- www.baidu.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7012ms
rtt min/avg/max/mdev = 23.808/25.447/32.874/2.852 ms
另外一块主板通过网络共享获取到的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/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:286671 errors:0 dropped:0 overruns:0 frame:0
TX packets:586 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:27238966 TX bytes:64962
Interrupt:169 Base address:0x3000
通过测试可以ping通互联网网址
console:/ # ping www.baidu.com
PING www.baidu.com (157.148.69.74) 56(84) bytes of data.
64 bytes from 157.148.69.74: icmp_seq=1 ttl=51 time=37.7 ms
64 bytes from 157.148.69.74: icmp_seq=2 ttl=51 time=28.7 ms
64 bytes from 157.148.69.74: icmp_seq=3 ttl=51 time=22.9 ms
64 bytes from 157.148.69.74: icmp_seq=4 ttl=51 time=23.2 ms
64 bytes from 157.148.69.74: icmp_seq=5 ttl=51 time=23.4 ms
64 bytes from 157.148.69.74: icmp_seq=6 ttl=51 time=48.0 ms
64 bytes from 157.148.69.74: icmp_seq=7 ttl=51 time=23.0 ms
64 bytes from 157.148.69.74: icmp_seq=8 ttl=51 time=24.9 ms
64 bytes from 157.148.69.74: icmp_seq=9 ttl=51 time=69.8 ms
^C
--- www.baidu.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8012ms
rtt min/avg/max/mdev = 22.930/33.577/69.879/15.184 ms
全部0条评论
快来发表一下你的评论吧 !