USB debugging on physical device within a virtual machine development environment
Asked Answered
C

2

7

I have a development environment set up in a Hyper-V virtual machine (Windows 10). Using Visual Studio 2019 as my IDE with Xamarin (a monodroid project). My physical device is unable to "passthrough" due to decisions made by the Hyper-V designers, from what I've read. I have also tried to use workarounds, unfortunately, the device does not show up (even in Device Manager) in the Guest machine.

I was unable to see the device (within the IDE) initially, however, after some research and work, I've managed to get the device showing. I used this to "trick" adb running on the Guest to see the device (which is connected to my physical machine). Host machine runs the script with IP 192.168.1.13 on port 6037 (adb daemon runs on 5037, which is default). Guest machine runs the script, with remotehost set to the Host IP, with the same port. I believe this configuration is fine, according to the documentation.

Unfortunately, even though I am able to deploy to my device, the debugger is unable to connect, and I receive Cannot start debugging: Cannot connect to 127.0.0.1:29300: No connection could be made because the target machine actively refused it 127.0.0.1:29301 Couldn't connect debugger. You can see more details in Xamarin Diagnostic output and the full exception on logs.

From what I have gathered, the debugger is trying to connect to localhost, and since the IDE is running within the Guest, the physical device will not be at localhost:29301. I assume if I could get the IDE to connect to 192.168.0.13:29301 (Host IP) instead, it would find the device, given that's where it is connected.

When I run adb devices, both the Host and Guest show the physical device, and after attempting to run the debugger, running adb forward --list, the forward rules are as such:

V4QBB18807150883 tcp:29300 tcp:29300
V4QBB18807150883 tcp:29301 tcp:29301

I can also see in Xamarin Diagnostics that setprop "debug.mono.extra" "debug=127.0.0.1:29302:29303,timeout=1585047190,loglevel=0,server=y" is executed. I assume manipulating that would give me some joy, but, I don't know how.

I'm not too familiar with adb and networking, so anyone with more experience in the matter could shed some light on my situation.

How can I get the debugger (within the Guest) to connect to my physical device?

PS: I've searched a lot before asking here. I know this exists: How can I connect to Android with ADB over TCP? and even after trying multiple answers on that page, I was unable to solve my problem.

Cappello answered 24/3, 2020 at 11:1 Comment(2)
https://mcmap.net/q/410129/-how-can-i-deploy-and-execute-an-application-on-a-device-connected-to-a-remote-system/1778421Toback
@AlexP. Thanks for your comment. I believe the adbportforward.jar utility essentially does as your answer suggests internally anyway. Please correct me if I'm wrong in assuming thatCappello
C
17

After considerable amount of research, I came across this. Instead of searching for a way to set up adb port forwarding, I tried to see if there was a way to set up a USB passthrough via Hyper-V.

If using RDP is an option (directly connecting through "Hyper-V Manager" also works):

On host machine

  1. Press Win+R type gpedit.msc and hit enter
  1. Go to Computer configuration > Administrative templates > Windows Components > Remote Desktop Services > Remote Desktop Connection Client > RemoteFX USB Device Redirection
  1. Select the only GPO there and Enable it and change the access rights to "Administrators and Users"

On VM:

  1. Press Win+R type gpedit.msc and hit enter
  1. Go to Computer configuration > Administrative templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Device and Resource Redirection
  1. Select GPO "Do not allow supported Plug and Play device redirection" and disable it

Reboot both machines...

You may need to force update Group Policy, as per this comment:

I had to force update group policy (gpupdate) for it to take effect. Just shutting down and restarting both host and vm wasn't enough. Thanks

Eventually, after carrying out those steps (I ran gpupdate and restarted both machines), I had an extra option of listed devices, "Other supported RemoteFX USB devices", within that was my physical device. After checking the tick box, the VM detected the device without the need to port forward.

This can potentially cause security risks allowing a passthrough like this, however, my VM is not public and only accessible via my Host. I would appreciate if someone in security could comment on the possible security risks just to inform myself and potential others that come across this post.

Cappello answered 25/3, 2020 at 9:12 Comment(2)
exactly as stated by user959631. Do the group policy updates and then gpupdate. Then connect with the connection options for the remotefx usb. For directly connecting via hyper-v you might want to do a windows-r command "vmconnect <hostname> <vmname> /edit"Turgent
This solution works even if you connect via RDP to remote PC.Bainmarie
Z
0

This is an old post but for anyone stumbling upon it, I found an alternative way to connect a physical device to a Hyper-V VM. I was able to get this working on a Hyper-V VM running Ubuntu 20.04. There was no way I could edit the group policy so instead I found a way to connect my VM to the same wifi as my device. Once connected, I could use Android Studio to pair it that way.

I followed the guidance here: https://superuser.com/questions/1403016/how-do-i-setup-a-working-wifi-internet-connection-on-windows-10-hyper-v

  1. Open Hyper-V Manager
  2. Click "Virtual Switch Manager"
  3. Click "New virtual network switch"
  4. Name: NAT, Connection type: "External network", Select the correct network adapter. Tick "Allow management operating system to share this network adapter.
  5. Right click Ubuntu, select "Settings"
  6. In Network Adapter, Virtual switch, select "NAT"
  7. Go to "Control Panel > Network and Internet > Network Connections" in Windows 10
  8. Right click the network adapter that is in use. For me, it is "Ethernet 2"
  9. "Properties > Sharing", tick "Allow other network users to connect through this computer's Internet connection" and select "vEthernet (NAT)" in the drop down.
  10. Tick "Allow other network users to control or disable the shared Internet connection", then OK.
  11. Start Ubuntu virtual machine and test!

Once I followed those steps, Android Studio was able to pair with my device over the wifi.

Zincography answered 7/9, 2022 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.