0

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

Edit: So i do not necessarily need to have a external usb drive mounted at startup. If its possible with adb it would be great if i could just forward a usb Pendrive to the running emulator when i need it. Something like adb connectUsbDevice -deviceid=****,vendorid=***

snux_cs
  • 63
  • 7

2 Answers2

0

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.

Architect Jamie
  • 1,621
  • 4
  • 18
0

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

Architect Jamie
  • 1,621
  • 4
  • 18
  • Im using Kubuntu. Yes, i want to test it with a real USB Stick plugged into my Laptop. The testcase is following: 1. I start the Emulator and install the App 2. The App runs in the Emulator 3. I plug in a real USB Storage Stick in my real Laptop 4. The App need to get the "new" USB external Storage Device 5. Writes Data to the external USB Storage – snux_cs Sep 15 '22 at 09:10
  • OK. That was unclear and I assumed you were using Windows. Please edit your question and include OS and programming language. Also see https://snapcraft.io/install/powershell/kubuntu. You can install Linux PowerShell. – Architect Jamie Sep 15 '22 at 09:22