Home > Back-end >  Android Emulator connect external storage Device while running
Android Emulator connect external storage Device while running

Time:09-15

I have an App that needs to detect, if a USB Storage device is getting mounted. Can i simulate the mounting of an USB Storage Device while the Emulator is already running? This way i can debug the behavior of my app.

As i know, for registering the mounting and unmounting of the USB Storage Device i can use the StorageVolumeCallback(). What do i have to do to write a simple .txt File to that attached USB Storage Device?

Im having trubble to create a StorageManager inside my ViewModel because i do not have access to the Context.

Im thankful for any Tipp related to USB Storage Management at all.

Info: API Version: 31 IDE: Android Studio Language: Kotlin

CodePudding user response:

There isn't a .NET library I'm aware of which can do this. However, please refer to this post where the brilliant answer shows how to do this interfacing with the Win 32 API.

How do I disable a system device programmatically?

You'll need a combination of this, and a WMI query to find an attached PnP device of type storage. As a clue:

using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_USBHub"))
        {
            collection = searcher.Get();
        }

Change Win32_USBHub to the correct class if this isn't giving you what you're looking for.

EDIT: Be warned. If you're disabling storage devices, make sure they're not in use. That's what the "Safely remove USB" option is for in Windows.

CodePudding user response:

Another option, if you don't need to emulate this in code, use a real USB storage device inserted in the system and use PowerShell to get, disable, and enable the device.

The Cmdlets you'll need are:

Get-PnpDevice Disable-PnpDevice Enable-PnpDevice

  • Related