Skip to content

Instantly share code, notes, and snippets.

@Bad-At-Usernames
Created April 27, 2022 14:06
Show Gist options
  • Save Bad-At-Usernames/684784f42cbb69e22688a21173ec263d to your computer and use it in GitHub Desktop.
Save Bad-At-Usernames/684784f42cbb69e22688a21173ec263d to your computer and use it in GitHub Desktop.
# This script was made by @Daniel Kinau on the ALVR discord server.
import os
import time
import win32com.client
# INPUT YOUR USB NAME HERE
usb_name = ""
wmi = win32com.client.GetObject("winmgmts:")
adb_started = False
port_forwarded = False
def usb_connected() -> bool:
for usb in wmi.InstancesOf("Win32_USBHub"):
if usb_name in usb.DeviceID:
return True
return False
os.system('adb.exe devices')
while True:
time.sleep(1)
if usb_connected():
if not port_forwarded:
time.sleep(5)
os.system('adb.exe forward tcp:9943 tcp:9943')
os.system('adb.exe forward tcp:9944 tcp:9944')
port_forwarded = True
print("Forwarded device!")
else:
port_forwarded = False
@Interpause
Copy link

Interpause commented Sep 13, 2023

Equivalent for Linux:

#!/bin/env python3
import os
from time import sleep

# Input device path here
usb_path = "/dev/android1"

def usb_connected() -> bool:
    return os.path.exists(usb_path)

os.system("adb devices")

port_forwarded = False
while True:
    sleep(1)
    if usb_connected():
        if not port_forwarded:
            sleep(5)
            os.system("adb forward tcp:9943 tcp:9943")
            os.system("adb forward tcp:9944 tcp:9944")
            port_forwarded = True
            print("Forwarded device!")
    else:
        port_forwarded = False
        print("Device disconnected!")

@Woccz
Copy link

Woccz commented Sep 17, 2023

For anyone on Linux who wants a plug-and-play experience I suggest moving adb to /usr/local/bin/, then creating vr-android-device-bridge.service in etc/systemd/system/

[Unit]
Description=VR Android Device Bridge Service

[Service]   
Type=oneshot
ExecStart=/bin/bash -c "sleep 5;adb devices;adb forward tcp:9943 tcp:9943;adb forward tcp:9944 tcp:9944"

and 98-alvr-usb.rules in /etc/udev/rules.d/ with the idVendor and idProduct for your VR headset

ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="<YOUR idVendor>", ATTRS{idProduct}=="<YOUR idProduct>", RUN+="/bin/sh -c 'systemctl start vr-android-device-bridge.service'"

You can find the vendor and product ids using lsusb in the form ... ID <idVendor>:<idProduct> ... for your headset.

To debug you can check journalctl -u vr-android-device-bridge

@l33tlinuxh4x0r
Copy link

l33tlinuxh4x0r commented Oct 21, 2023

I updated the Windows script to automatically launch ALVR once adb is successfully forwarded. It also kills adb when ALVR is closed. Also you don't need your USB device name anymore... It is just plug and play now.

import os
import time
import subprocess

ALVR_started = False
port_forwarded = False

def usb_connected() -> bool:
    devs = os.popen('adb devices').read().split("\n")[0:-2]
    if devs[-1] != "List of devices attached":  
        return True
    return False

while True:
    time.sleep(1)
    if ALVR_started and ALVR_command not in str(subprocess.check_output(task_mgr)):
        ALVR_started = False
        print("ALVR closed, quitting.")
        os.system('adb kill-server')
        quit()
    if usb_connected():
        if not port_forwarded:
            time.sleep(5)
            os.system('adb forward tcp:9943 tcp:9943')
            os.system('adb forward tcp:9944 tcp:9944')
            port_forwarded = True
            print("Forwarded device!")
        if not ALVR_started and port_forwarded:
            if os.name == "nt":
                ALVR_command = "ALVR Dashboard.exe"
                task_mgr = "tasklist"
                subprocess.Popen("C:/Program Files/ALVR/ALVR Dashboard.exe", stderr=subprocess.DEVNULL)
            else: 
                ALVR_command = "AppRun"
                task_mgr = "ps"
                subprocess.Popen("./ALVR-x86_64.AppImage", stderr=subprocess.DEVNULL)
            ALVR_started = True
    else:
        port_forwarded = False
        print("No device(s) found.")
        

Add a non steam game to the steam library and select your python.exe. then set launch options to the directory/filename that you save this script to. Now you can launch ALVR from steam and everything will be setup for you!

EDIT: I made the script cross platform now just run it on Windows with ALVR installed in default directory or Linux with ALVR appimage in same directory as this script and it should work. Note adb must be in PATH.

New Version for latest ALVR builds as there isn't an appimage version anymore.

import os
import time
import subprocess

ALVR_started = False
port_forwarded = False

def usb_connected() -> bool:
    devs = os.popen('adb devices').read().split("\n")[0:-2]
    if devs[-1] != "List of devices attached":  
        return True
    return False

while True:
    time.sleep(1)
    if ALVR_started and ALVR_command not in str(subprocess.check_output(task_mgr)):
        ALVR_started = False
        print("ALVR closed, quitting.")
        os.system('adb kill-server')
        quit()
    if usb_connected():
        if not port_forwarded:
            time.sleep(5)
            os.system('adb forward tcp:9943 tcp:9943')
            os.system('adb forward tcp:9944 tcp:9944')
            port_forwarded = True
            print("Forwarded device!")
        if not ALVR_started and port_forwarded:
            if os.name == "nt":
                ALVR_command = "ALVR Dashboard.exe"
                task_mgr = "tasklist"
                subprocess.Popen("C:/Program Files/ALVR/ALVR Dashboard.exe", stderr=subprocess.DEVNULL)
            else: 
                ALVR_command = "alvr_dashboard"
                task_mgr = "ps"
                subprocess.Popen("./alvr_dashboard", stderr=subprocess.DEVNULL)
            ALVR_started = True
    else:
        port_forwarded = False
        print("No device(s) found.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment