adbutils
adbutils copied to clipboard
Random adb ports in Android 11+
In Android 11 it appears to choose a random ADB port (from what I've seen between 30000 and 50000)... what's the best way to find the port number from python?
This might not be all devices, but it's what I've observed on Pixel 3 and 2.
from ENV? for example env:ANDROID_ADB_SERVER_PORT
Hi, I'm not looking for the port of the adb server, but the adb port of the client device. Android 10 and below I believe always used 5555, but now it is random.
My adb server is Windows 10 but don't think that matters.
I believe the random port can be between 30000 and 60000, but not 100% sure.
Maybe a multi threaded port scan function could be added?
I solved this by making a multi threaded port scanner and attempting to connect. This may not work right out of the box (I tried to copy the relevant parts out of my script)
import socket
from adbutils import adb
def myfunction(clientIP):
global scannerIP
scannerIP = clientIP
adbRanges = [ [5555, 5585], [30000 , 50000] ] #https://www.reddit.com/r/tasker/comments/jbzeg5/adb_wifi_and_android_11_wireless_debugging/
for adbRange in adbRanges:
adbThreadedScan(adbRange)
adbAddress = adbConnect(clientIP, adbPortsFound)
if adbAddress != None:
device = adb.device(serial=adbAddress)
break
def adbConnect(clientIP, adbPortsFound):
for adbPort in adbPortsFound:
adbAddress = clientIP + ':' + str(adbPort)
adbConnectOutput = adb.connect(adbAddress)
message = adbConnectOutput.split(clientIP + ':' + str(adbPort), 1)[0].strip()
if message == 'connected to' or message == 'already connected to':
logging.info('adb connected on port: %s' % (adbPort))
return adbAddress
def adbThreadedScan(adbRange):
from threading import Thread
from queue import Queue
# number of threads, imported from dictionary of preferences
rangeThreads = adbRange[-1] - adbRange[0]
prefThreads = Prefs['int_PortScanThreads']
if rangeThreads < prefThreads:
N_THREADS = rangeThreads
else:
N_THREADS = prefThreads
# thread queue
global q
q = Queue()
global adbPortsFound
adbPortsFound = []
for t in range(N_THREADS):
try:
#for each thread, start it
t = Thread(target=port_scan_thread)
#when we set daemon to true, that thread will end when the main thread ends
t.daemon = True
#start the daemon thread
t.start()
except RuntimeError as e:
break
for port in range(adbRange[0], adbRange[-1]):
if (port % 2) != 0: #if port is an odd number
#for each port, put that port into the queue
#to start scanning
q.put(port)
q.join() #wait for all ports to finish being scanned
def port_scan(host, port): #determine whether `host` has the `port` open
try:
# creates a new socket
s = socket.socket()
# tries to connect to host using that port
s.connect((host, port))
# make timeout if you want it a little faster ( less accuracy )
# s.settimeout(0.2)
except:
# cannot connect, port is closed
pass
else:
# the connection was established, port is open!
adbPortsFound.append(port)
finally:
s.close()
def port_scan_thread():
while True:
# get the port number from the queue
port = q.get()
# scan that port number
port_scan(scannerIP, port)
# tells the queue that the scanning for that port
# is done
q.task_done()
I will keep this open, but if the dev would like to close it that's fine. It would be nice to see the functionality added to this library.
@ReenigneArcher https://github.com/openatx/adbutils/invitations
I'm still struggling to find a good solution to this, as port scanning is slow. Honestly I do not understand why Android decided to go with the random ports and complicated pairing process.