Android屏幕锁定时禁用USB主机端口

时间:2014-08-13 21:28:13

标签: android usb

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

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

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

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

2 个答案:

答案 0 :(得分:0)

感谢Chris Stratton小费。使用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主机来测试它。当屏幕被锁定时,鼠标没有按照我的意愿关闭。

答案 1 :(得分:0)

我遇到的另一个选项,我没试过。您可以调整控制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