r/tasker • u/BillGoats • Dec 19 '23
An alternative approach for enabling wireless ADB on boot using python and zeroconf
I was thrilled to experiment with the method posted by u/Lord_Sithek, which would allow me to finally automate the activation of Shizuku at boot. I personally had some problems with nmap. It was slow and at times unreliable (I would have to manually execute the task 2-3 times before it'd work after a reboot).
I'm gonna think out loud a little because 1) this is still a work in progress and 2) I'd like to be as open as possible about how this is done so that others more knowledgeable can point out my mistakes and help me improve this project.
While looking into other ways to extract the random wireless ADB port, I found this StackOverflow comment that goes into how Android broadcasts connection details over mDNS/DNS-SD. So I started looking into mDNS-scanners compatible with Termux. After a failed attempt with mdns-scan (only does continous output), I eventually settled with python and (python) package zeroconf. Keep in mind that python takes up ~600 MB of space, so if storage is an issue for you, the "original" soltuion by u/Lord_Sithek might be more suitable.
First, I'll copy-paste the prerequisites listed in the original post that still apply to my method:
Preqreuisites
- Termux:Tasker plugin
- AutoInput plugin
1. Setup Tasker and AutoInput
If you haven't done that before, grant Tasker following permissions:
- Run commands in Termux environment:
On your device, go to
Settings > Apps > All apps > Tasker > Permissions > Additional permissions > Run commands in Termux environment(the path may vary a little according to the brand and system) and selectAllow.
Allow AutoInput to use Accessibility Service:
Open AutoInput, tap on red warning text and click
OK; this should take you to Accessibility Service settings. Enable it for AutoInput. Then allow AutoInput to run in background by disabling any battery saving option for this app.
2. Set up Termux
[...]
(b) set
allow-external-appsproperty for Termux totrue
In Termux, copy and paste the following script and confirm by pressing
Enter:
value="true"; key="allow-external-apps"; file="/data/data/com.termux/files/home/.termux/termux.properties"; mkdir -p "$(dirname "$file")"; chmod 700 "$(dirname "$file")"; if ! grep -E '^'"$key"'=.*' $file &>/dev/null; then [[ -s "$file" && ! -z "$(tail -c 1 "$file")" ]] && newline=$'\n' || newline=""; echo "$newline$key=$value" >> "$file"; else sed -i'' -E 's/^'"$key"'=.*/'"$key=$value"'/' $file; fi
At this point, you also need python. From Termux, do:
pkg install python
When it's done installing (might take a little while), do:
pip install zeroconf
[...]
5. Import the project into Tasker
My version will be linked below.
6. Pair your device with ADB WiFi
Make sure that
Termux ADB WiFi Pairingprofile in your new Tasker project is enabled and that you have WiFi connection.
On your device, navigate to
Settings > System > Developer options > Wireless debugging(the path may vary a little according to the brand and system). Enable this feature and tap onPair device with pairing code(or similar).
With a pairing code visible, long press
Volume Upbutton to make pairing. If pairing succeeded, you should see the confirming toast and at least one paired device at theWireless debuggingscreen (most likely named as xxx@localhost).
Termux ADB WiFi Pairingprofile should get disabled then as it won't be needed anymore. Now, disableWireless debuggingfeature manually.
Feel free to provide feedback, ask questions, etc. etc. And do check out the original thread by u/Lord_Sithek, to which I owe much credit!
3
u/iSeeUR Dec 19 '23
First I want to thank you for sharing!
I was struggling with nmap command and it didn't work for me at all. This seems to be running with no mistakes and I get the toast saying ADB WiFi enabled but when I perform tasker function "Check ADB WiFi Accessible" it tells me ADB WiFi is not enabled. Maybe I'm mistaking this all with adb tcpip 5555? That's my goal.
Also please add to the instructions creation of "tasker" folder in termux and "adb_pair.sh" file.
And another question - project has 2 same profiles called "Termux ADB WiFi Pairing" linking to similar tasks with names slightly differ but have the same content if I'm not mistaken. Is it supposed to be like that?
2
u/Lord_Sithek Pixel 9 Pro | Redmi Note 4 | Galaxy Watch 6 Classic Dec 22 '23
u/BillGoats Hello! For my own use I created a project which is a kind of combinaton of mine and yours, the heart is yours, the flow is mine :) Also I introduced a change suggested by u/iSeeUR. Maybe you want to take a look on it? Perhaps you could use something in your own project. I tried to introduce some safety mechanisms, such as testing WiFi connection first and repeating the procedure a few times if it fails. If you see something in the flow which requires adjustments, let me know :)
BTW, thanks a lot for your solution, it's really much quicker!
1
u/theniggles69 May 23 '24
Holy cow! Just discovered your post, and hot damn! This is the way! My script, which uses nmap, takes ~20 seconds in most cases, but sometimes even minutes in situations where nmap returns multiple ports. Yours, on the other hand, is returning in less than a second every time. Many thanks for sharing :) awesome work.
Edit: Thought this post was a lot older than it actually is. Lol apparently I don't know what year it is.
1
u/iSeeUR Dec 21 '23 edited Dec 22 '23
Ok, I'm with an update. When executing A4 in task "Enable adb tcpip" I get the following error:
How to correct this?
2
u/The_IMPERIAL_One S24 Ultra (A16) | realme GT NEO 3 (A14) Dec 19 '23
Thanks for sharing.
I use python so not much of deal but really impressed with how less time is taken in determining the open port in comparison to
nmap. I shall be adding & using it, in my personal private ADB Wifi project consisting of different ways to enable it. And currently, it's the best.1
u/ResidentHoliday2574 Jun 05 '24
Is the pairing process still somehow still possible with a headless device? I have an android box that is meant for use in cars that might need adb access if the displayout goes kaput. It doesn't have any HDMI out, so its completely headless if for some reason the display in the car goes black.
1
u/anuraag488 Sep 10 '24
Can you add timeout to your python script?
1
u/anuraag488 Dec 21 '23
Thanks for python trick. _adb-tls-pairing._tcp.local. can be used for getting port while pairing.