Skip to content

Instantly share code, notes, and snippets.

@Ryiski
Forked from gnumoksha/adb-wireless-connect.sh
Created August 5, 2023 07:09
Show Gist options
  • Select an option

  • Save Ryiski/6a80de0883a4b67b2732f1f36a231a84 to your computer and use it in GitHub Desktop.

Select an option

Save Ryiski/6a80de0883a4b67b2732f1f36a231a84 to your computer and use it in GitHub Desktop.

Revisions

  1. @gnumoksha gnumoksha created this gist Oct 10, 2022.
    30 changes: 30 additions & 0 deletions adb-wireless-connect.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env bash

    # This script will try to find the adb remote debugging port in the specified
    # IP address and then tell adb to try to connect to it.
    # It is useful for connecting to an android device without opening the
    # "Wireless debugging" screen to get the IP and the random port, which is
    # cumbersome.

    for line in $(avahi-browse --terminate --resolve --parsable --no-db-lookup _adb-tls-connect._tcp); do
    if [[ $line != =* ]]; then
    continue
    fi

    IFS=';'; fields=($line); unset IFS;
    uri="${fields[7]}:${fields[8]}"

    echo "INFO: it will try to connect on $uri"
    adb_result=$(adb connect $uri)
    echo $adb_result

    # Note: adb exits with 0 even if the connection fails,
    # so I'm checking its output
    if [[ $adb_result =~ connected ]]; then
    echo "INFO: sucefully connected"
    exit 0
    fi
    done

    echo "ERROR: unable to identify the ADB port on the android device"
    exit 1