enable-port5555.ps1 1.4 KB

12345678910111213141516171819202122
  1. $ErrorActionPreference = 'SilentlyContinue'
  2. $TCPIP_PORT = 5555
  3. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  4. $root = (Get-Item (Join-Path $scriptDir '..\..')).FullName
  5. $adb = Join-Path $root 'lib\scrcpy-adb\adb.exe'
  6. if (-not (Test-Path $adb)) { Write-Host '[ERROR] ADB not found'; exit 1 }
  7. $lines = & $adb devices 2>$null
  8. $devices = $lines | ForEach-Object { $t = $_.Trim(); if ($t -match '^(\S+)\s+device') { $Matches[1] } } | Where-Object { $_ }
  9. $dev = $devices | Select-Object -First 1
  10. if (-not $dev) { Write-Host 'No devices found. Please connect a device via USB.'; exit 1 }
  11. if ($devices.Count -gt 1) { Write-Host "Multiple devices. Using: $dev" }
  12. & $adb -s $dev shell 'settings put global adb_wifi_enabled 1' 2>$null | Out-Null
  13. $wireless = (& $adb -s $dev shell 'settings get global adb_wifi_enabled' 2>$null).Trim()
  14. if ($wireless -eq '1') { Write-Host 'Wireless debugging enabled.' } else { Write-Host 'Wireless setting may not be supported. Enable it in Settings > Developer options.' }
  15. $wlanIp = (& $adb -s $dev shell 'getprop dhcp.wlan0.ipaddress' 2>$null).Trim()
  16. if ($wlanIp -notmatch '^\d+\.\d+\.\d+\.\d+$') {
  17. $out = & $adb -s $dev shell 'ip -4 addr show wlan0' 2>$null
  18. if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $wlanIp = $Matches[1] }
  19. }
  20. $tcpipOut = & $adb -s $dev tcpip $TCPIP_PORT 2>$null
  21. Write-Host $tcpipOut
  22. if ($wlanIp) { Write-Host "Wireless: adb connect ${wlanIp}:${TCPIP_PORT}" }