簡體   English   中英

有沒有辦法在 ChromeOS 的 Android 應用程序中檢測 USB 設備?

[英]Is there a way to detect USB devices in an Android application on ChromeOS?

設備必須處於USB 主機模式才能使用UsbManager類。 但是,當使用packagemanager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)檢查應用程序是否處於 USB 主機模式時,它總是返回 false。 顯然,USB Host 是 Chromebook 上不受支持的功能 還有另一種方法可以檢測 Chromebook 上的 Android 何時插入 USB 嗎?

我們正在從 Chrome 應用程序過渡到 Android,該文檔說使用“臨時*私有 USB 主機 API”代替chrome.usb功能。 但是鏈接已損壞,我找不到更多信息。

我也嘗試過USB Web API但沒有結果(該應用程序包含在 webview 中,所以認為它可能有效)。

解決方案是使用StorageVolumestate屬性。 MOUNTEDStorageMediaState值將指示已安裝的 USB 驅動器。 您還需要使用附加的StorageVolume的各種屬性來過濾掉非 USB 驅動器。

fun getStorageMediaStateFromVolume(volume: StorageVolume?): StorageMediaState {
    val volumeState = volume?.state
    return if (volume == null || volumeState == null) StorageMediaState.UNKNOWN
    else StorageMediaState.values().find { it.stringValue == volumeState } ?: StorageMediaState.UNKNOWN
}
enum class StorageMediaState(val stringValue: String, val isDriveInserted: Boolean) {
    // Unknown storage state, such as when a path isn't backed by known storage media.
    // This is the value when no drive is inserted.
    UNKNOWN(Environment.MEDIA_UNKNOWN, false),

    // Media is present but not mounted. Usually this state occurs after an ejection has completed
    // but the user has not yet removed the drive.
    UNMOUNTED(Environment.MEDIA_UNMOUNTED, true),

    // Media is present and being disk-checked.
    CHECKING(Environment.MEDIA_CHECKING, true),

    // Media is present but is blank or is using an unsupported filesystem.
    NOFS(Environment.MEDIA_NOFS, true),

    // Media is present and mounted at its mount point with read/write access.
    MOUNTED(Environment.MEDIA_MOUNTED, true),

    // Media is present and mounted at its mount point with read-only access.
    MOUNTED_READ_ONLY(Environment.MEDIA_MOUNTED_READ_ONLY, true),

    // Media is present not mounted, and shared via USB mass storage.
    SHARED(Environment.MEDIA_SHARED, true),

    // Media is present but cannot be mounted, typically because the media's file system is corrupted.
    UNMOUNTABLE(Environment.MEDIA_UNMOUNTABLE, true),

    // Media is in the process of being ejected.
    EJECTING(Environment.MEDIA_EJECTING, true),

    // Media is not present.
    REMOVED(Environment.MEDIA_REMOVED, false),

    // Media was removed before it was unmounted.
    BAD_REMOVAL(Environment.MEDIA_BAD_REMOVAL, false)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM