本文基于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文章
3939瀏覽量
127590 -
4G網絡
+關注
關注
1文章
143瀏覽量
26906 -
瑞芯微
+關注
關注
23文章
427瀏覽量
51407
發布評論請先 登錄
相關推薦
評論