参考博客:rk3568-OTG模式切换_rk3568 打开usb共享网络功能-优快云博客
原来的设计有缺陷,没有保存效果,每次开机重新恢复
1.添加环境变量persist.sys.usb=1
diff --git a/device/rockchip/rk356x/rk3568_r/rk3568_r.mk b/device/rockchip/rk356x/rk3568_r/rk3568_r.mk
index 0fad1e23d8..4edb005f87 100644
--- a/device/rockchip/rk356x/rk3568_r/rk3568_r.mk
+++ b/device/rockchip/rk356x/rk3568_r/rk3568_r.mk
@@ -45,3 +45,4 @@ PRODUCT_PROPERTY_OVERRIDES += persist.wifi.sleep.delay.ms=0
PRODUCT_PROPERTY_OVERRIDES += persist.bt.power.down=true
PRODUCT_PROPERTY_OVERRIDES += vendor.hwc.device.primary=DSI
PRODUCT_PROPERTY_OVERRIDES += vendor.hwc.device.extend=eDP,LVDS
+PRODUCT_PROPERTY_OVERRIDES += persist.sys.usb=1
可以进系统后通过getprop看到值
2.修改

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="usb_mode"
android:defaultValue="false"
android:title="@string/usb_host_device_manager"/>
</PreferenceScreen>
diff --git a/packages/apps/Settings/res/xml/usb_host_device.xml b/packages/apps/Settings/res/xml/usb_host_device.xml
index 23e78005f9..11fbaddcd9 100755
--- a/packages/apps/Settings/res/xml/usb_host_device.xml
+++ b/packages/apps/Settings/res/xml/usb_host_device.xml
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <ListPreference
- android:entries="@array/usb_function_location"
- android:entryValues="@array/usb_function_location_values"
+ <CheckBoxPreference
android:key="usb_mode"
- android:persistent="true"
+ android:defaultValue="false"
android:title="@string/usb_host_device_manager"/>
3.修改

package com.android.settings;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.preference.SwitchPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import android.provider.Settings;
import android.content.res.Resources;
import android.os.SystemProperties;
import androidx.preference.CheckBoxPreference;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.util.Log;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import java.io.File;
import java.io.FileOutputStream;
public class UsbHostDeviceSetting extends SettingsPreferenceFragment implements OnPreferenceChangeListener {
/**
* Called when the activity is first created.
*/
private static final String KEY_USB_MODE_LOCATION = "usb_mode";
private static final String UsbDeviceHostCtlPatch = "/sys/devices/platform/fe8a0000.usb2-phy/otg_mode";
private static final String TAG = "UsbHostDeviceDebug";
public boolean dbg = true;
private int UsbHostState = 1;
private int UsbDeviceState = 2;
private CheckBoxPreference mUsbMode;
private SharedPreferences mSharedPreference;
private SharedPreferences.Editor mEdit;
private SettingsApplication mScreenshot;
private Context mContext;
private Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usb_host_device);
mContext = getActivity();
mUsbMode = (CheckBoxPreference ) findPreference(KEY_USB_MODE_LOCATION);
mUsbMode.setOnPreferenceChangeListener(this);
init_sys_usb();
}
private void init_sys_usb() {
if(SystemProperties.get("persist.sys.usb","1").equals("2")) {
mUsbMode.setChecked(true) ;
Log.d("longmin","1111\n");
}
else{
mUsbMode.setChecked(false) ;
Log.d("longmin","2222\n");
}
}
private static void delayMs(int ms){
exe_normal_cmd("sync");
try
{
Thread.sleep(ms);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if(preference==mUsbMode){
boolean show = mUsbMode.isChecked();
Log.d("liz","show====="+show);
if(!show) { // host
SystemProperties.set("persist.sys.usb","1");
set_usb( UsbDeviceHostCtlPatch,"1");
}
else{
//device
SystemProperties.set("persist.sys.usb","2");
set_usb( UsbDeviceHostCtlPatch,"2");
}
exe_normal_cmd("sync");
try
{
Thread.sleep(250);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
return super.onPreferenceTreeClick(preference);
}
public void set_usb(String proc,String val){
try {
FileOutputStream fops = new FileOutputStream(proc);
fops.write(val.getBytes());
fops.flush();
fops.close();
} catch (FileNotFoundException e) {
Log.d("err", "found error");
} catch (IOException e) {
Log.d("err", "IO error");
}
}
private static void exe_normal_cmd(String cmd){
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getMetricsCategory() {
// TODO Auto-generated method stub
return 5;
}
}
4.修改/packages/apps/Settings/AndroidManifest.xml
index 479d4bb17f..9f0c939b58 100644
--- a/packages/apps/Settings/AndroidManifest.xml
+++ b/packages/apps/Settings/AndroidManifest.xml
@@ -3442,7 +3442,14 @@
android:icon="@drawable/ic_settings_usb"
android:parentActivityName="Settings">
- <intent-filter android:priority="-2">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <action android:name="android.intent.action.BOOT_COMPLETED"/>
+ <category android:name="android.intent.category.DEFAULT" />
+ <category android:name="android.intent.category.VOICE_LAUNCH" />
+ <category android:name="com.android.settings.SHORTCUT" />
+ </intent-filter>
+ <intent-filter android:priority="6">
<action android:name="com.android.settings.action.SETTINGS" />
</intent-filter>
最后效果如图

=================================================================
由于重启不会切换模式,所以加入了一个开机广播
修改AndroidManifest.xml
index 479d4bb17f..9f0c939b58 100644
--- a/packages/apps/Settings/AndroidManifest.xml
+++ b/packages/apps/Settings/AndroidManifest.xml
d.settings.category"
@@ -3508,6 +3515,15 @@
</intent-filter>/>
</receiver>
+ <receiver
+ android:name="com.android.settings.display.BootReceiver"
+ android:enabled="true"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="android.intent.action.BOOT_COMPLETED"/>
+ </intent-filter>/>
+ </receiver>
+
添加BootReceiver.java
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.display;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.SystemProperties;
import android.util.Log;
import android.os.Build;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* RK3568 Android 11 开机广播接收器
*/
public class BootReceiver extends BroadcastReceiver {
// 声明需要监听的广播 Action
private static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
private static final String UsbDeviceHostCtlPatch = "/sys/devices/platform/fe8a0000.usb2-phy/otg_mode";
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
String action = intent.getAction();
// 校验是否是目标广播(适配 RK 平台可能发送的多种开机相关广播)
if (ACTION_BOOT_COMPLETED.equals(action)) {
//todo
Log.i("BootReceiver","接收到开机广播");
init_sys_usb();
}
}
private void init_sys_usb() {
if(SystemProperties.get("persist.sys.usb","1").equals("2")) {
Log.d("longmin","2222111\n");
set_usb( UsbDeviceHostCtlPatch,"2");
}
else{
Log.d("longmin","1111222\n");
set_usb( UsbDeviceHostCtlPatch,"1");
}
}
public void set_usb(String proc,String val){
try {
FileOutputStream fops = new FileOutputStream(proc);
fops.write(val.getBytes());
fops.flush();
fops.close();
} catch (FileNotFoundException e) {
Log.d("err", "found error");
} catch (IOException e) {
Log.d("err", "IO error");
}
}
}
然后开机后可以看到logcat打印接收到开机广播
======================================================================
由于不喜欢那个控件,换了一个控件
commit f86986dac69c026c4fc2d48ea9fc6f29f942c519 (HEAD -> master)
Author: longmin <longmin@xxxxx.cn>
Date: Mon Nov 24 15:15:44 2025 +0800
update adb mode for SwitchPreference
diff --git a/packages/apps/Settings/res/values-zh-rCN/strings.xml b/packages/apps/Settings/res/values-zh-rCN/strings.xml
index ad9f061234..d3f6750857 100644
--- a/packages/apps/Settings/res/values-zh-rCN/strings.xml
+++ b/packages/apps/Settings/res/values-zh-rCN/strings.xml
@@ -4967,6 +4967,10 @@
<!-- add by longmin for config usb host or device mode -->
<string name="usb_host_device_manager">USB 模式</string>
+ <string name="usb_otg_mode_switch_summary">打开:Host模式;关闭:Device模式</string>
+ <!-- end, add by haha -->
+
+ </resources>
<!-- end -->
<!--screen shot-->
diff --git a/packages/apps/Settings/res/values/strings.xml b/packages/apps/Settings/res/values/strings.xml
index 954857f2be..1f23422fde 100644
--- a/packages/apps/Settings/res/values/strings.xml
+++ b/packages/apps/Settings/res/values/strings.xml
@@ -12244,6 +12244,7 @@
<string name="later">s later capture</string>
<!-- add by longmin for config usb host or device mode -->
<string name="usb_host_device_manager">Usb Mode</string>
+ <string name="usb_otg_mode_switch_summary">Open:Host mode;Close:Device mode</string>
<!-- end -->
<!--hdmi settings-->
diff --git a/packages/apps/Settings/res/xml/usb_host_device.xml b/packages/apps/Settings/res/xml/usb_host_device.xml
index 11fbaddcd9..f99a05ab60 100755
--- a/packages/apps/Settings/res/xml/usb_host_device.xml
+++ b/packages/apps/Settings/res/xml/usb_host_device.xml
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <CheckBoxPreference
+ <SwitchPreference
android:key="usb_mode"
- android:defaultValue="false"
+ android:defaultValue="false"
+ android:summary="@string/usb_otg_mode_switch_summary"
android:title="@string/usb_host_device_manager"/>
</PreferenceScreen>
diff --git a/packages/apps/Settings/src/com/android/settings/UsbHostDeviceSetting.java b/packages/apps/Settings/src/com/android/settings/UsbHostDeviceSetting.java
index 4a03c0abae..da992bf45e 100755
--- a/packages/apps/Settings/src/com/android/settings/UsbHostDeviceSetting.java
+++ b/packages/apps/Settings/src/com/android/settings/UsbHostDeviceSetting.java
@@ -38,7 +38,7 @@ public class UsbHostDeviceSetting extends SettingsPreferenceFragment implements
private int UsbHostState = 1;
private int UsbDeviceState = 2;
- private CheckBoxPreference mUsbMode;
+ private SwitchPreference mUsbMode;
private SharedPreferences mSharedPreference;
private SharedPreferences.Editor mEdit;
@@ -54,7 +54,7 @@ public class UsbHostDeviceSetting extends SettingsPreferenceFragment implements
mContext = getActivity();
- mUsbMode = (CheckBoxPreference ) findPreference(KEY_USB_MODE_LOCATION);
+ mUsbMode = (SwitchPreference ) findPreference(KEY_USB_MODE_LOCATION);
mUsbMode.setOnPreferenceChangeListener(this);
init_sys_usb();


2607

被折叠的 条评论
为什么被折叠?



