【问题标题】:USB Host port disabled when Android screen is lockedAndroid 屏幕锁定时 USB 主机端口禁用
【发布时间】:2014-08-13 21:28:13
【问题描述】:

我正在开发一个通过 Android 设备 USB 主机端口与嵌入式设备通信的应用程序。我注意到当屏幕被锁定时 USB 主机端口被禁用并且没有通信发生。

如何防止 USB 主机端口关闭,以便在屏幕锁定时进行通信?

-------------   USB Host            ---------------
|  Android  |  <------------------> |   Device    |
-------------                       ---------------

注意:如有必要,我可以在 Android 系统上拥有 root 访问权限。

【问题讨论】:

  • 你确定它是绑定到屏幕开/关而不是唤醒状态吗?你可能想看看唤醒锁或类似的东西。
  • 唤醒状态保持开启可以锁屏吗?
  • 我相信唤醒锁有各种形式的保留,因此您应该能够获得一种在关闭屏幕的同时保持 CPU 唤醒的功能。但我们还不知道这是问题所在。屏幕熄灭多久后USB会出现故障?
  • 我相信PARTIAL_WAKE_LOCK唤醒锁定级别:确保 CPU 正在运行;屏幕和键盘背光将被允许熄灭。)我就是这样看着。我会试试这个。 developer.android.com/reference/android/os/…
  • 例如,将鼠标插入 USB Host 端口将在屏幕锁定后 1 秒内关闭鼠标。

标签: android usb


【解决方案1】:

感谢克里斯·斯特拉顿的提示。使用PARTIAL_WAKE_LOCK 可以关闭屏幕,但 CPU 仍在运行。这适合我的应用。

我创建了一个快速应用来测试这个:

public class MainActivity extends Activity {

    PowerManager pm;
    PowerManager.WakeLock wl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (wl != null) {
            wl.acquire();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (wl != null) {
            wl.release();
        }
    }

我通过将鼠标连接到 USB 主机对其进行了测试。当屏幕被锁定时,鼠标没有按我的意愿关闭。

【讨论】:

    【解决方案2】:

    我遇到的另一个我没有尝试过的选项。您可以调整控制 USB 设备电源管理的系统资源。为此,您可能需要 root 访问权限。

        Changing the default idle-delay time
        ------------------------------------
    The default autosuspend idle-delay time (in seconds) is controlled by
    a module parameter in usbcore.  You can specify the value when usbcore
    is loaded.  For example, to set it to 5 seconds instead of 2 you would
    do:
    
        modprobe usbcore autosuspend=5
    
    Equivalently, you could add to a configuration file in /etc/modprobe.d
    a line saying:
    
        options usbcore autosuspend=5
    
    Some distributions load the usbcore module very early during the boot
    process, by means of a program or script running from an initramfs
    image.  To alter the parameter value you would have to rebuild that
    image.
    
    If usbcore is compiled into the kernel rather than built as a loadable
    module, you can add
    
        usbcore.autosuspend=5
    
    to the kernel's boot command line.
    
    Finally, the parameter value can be changed while the system is
    running.  If you do:
    
        echo 5 >/sys/module/usbcore/parameters/autosuspend
    
    then each new USB device will have its autosuspend idle-delay
    initialized to 5.  (The idle-delay values for already existing devices
    will not be affected.)
    
    Setting the initial default idle-delay to -1 will prevent any
    autosuspend of any USB device.  This has the benefit of allowing you
    then to enable autosuspend for selected devices.
    

    来源: https://android.googlesource.com/kernel/common/+/android-3.10/Documentation/usb/power-management.txt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多