@chaoscreater,
Thanks for waiting. I need double to check it up
Probably it contains all what you want. The return value to MD writes to STDOUT, so you can catch it via MD variable, and manipulate it by MD Text manipulate action.
umm... text .sh file was gone away...uploaded it again.
MediaFire is a simple to use free service that lets you put all your photos, documents, music, and video in a single place so you can access them anywhere and share them everywhere.
www.mediafire.com
Thanks for sharing that. I just have 2 questions:
1. How can I make the script run automatically on Termux app startup?
2. Can you include the MacroDroid macro as well for use with Termux?
Also, I have a suggestion. When you enable wireless debugging, it should register something in logcat. In fact, if you take a look at the Reddit link for Tasker, it uses logcat to capture the events. Based on this, we can capture the port used by wireless debugging and then store that as a variable. Then, pass this variable into Termux. It'll be a lot quicker compared to using nmap to scan.
I just did a test and it showed this in my logcat:
"adb wifi started on port 45391"
Make sure to kill any existing ADB server first before you run logcat. I was using adb tcpip 5555 on my phone and so I wasn't getting any logcat data at all.
UPDATE:
OK, I just tested and got my macro working. I can now just use logcat to capture the port used by wireless debugging! Now I just need to figure out how to pass this port to Termux....
Search "id=10632" in the store for my macro.
UPDATE 2:
OK, I've created my own solution and I think I got it working!
First of all, download Termux and Termux-Tasker plugin from F-DROID. Don't download from Play Store. If you're using a Xiaomi device, make sure that AFTER you've installed Termux and Termux-Tasker plugin, go into your MacroDroid setting -> Permission and allow the Termux-Plugin. If you don't do this, MacroDroid won't be able to see Termux-Plugin when you configure it in the Actions.
Run Termux and type adb. It should say that you don't have adb install and will give you a command to install it. I can't remember exactly what the command is, but I think it's something like "pkg install android-tools". This step is very important, because there are different ways to install adb in Termux and without getting into details, this is the one you need.
Once installed, run "adb pair" and it should give you an example of how the command should be structured. If you get an error about how "adb pair" is not a valid command, then you did not install the correct adb package.
Next, run Termux in a floating window. Now, browse to Developer Options -> Wireless Debugging and click to generate a pairing code. This pairing code will disappear and change to a new one whenever you swap to another app, which is why we need to run Termux in a floating window.
Next, simply run:
adb pair localhost
ort pair_code
where the port and pair code are from the wireless debugging. Once done, you should see a new entry added to wireless debugging.
You can then run:
adb connect localhost
ort
where the port is shown in the top in the wireless debugging menu.
Now, browse to the ~/.Termux directory:
List the contents of the directory:
You should find a "termux.properties" file. Edit this file:
nano termux.properties
Go to the very bottom and add exactly as it shows below:
Press CTRL+O to write and CTRL+X to exit. This will allow other apps to be able to issue commands to Termux (which we'll be doing using MacroDroid).
Next, while still in the same ~/.Termux directory, create a folder called Tasker:
Go into the folder:
Create a new file called connadb3.sh:
And paste the following in there:
#!/data/data/com.termux/files/usr/bin/bash
host="localhost"
WirelessDebugging_Port="$1"
adb=$PREFIX/bin/adb
echo "Connecting to" ${host} "on" ${WirelessDebugging_Port}
$adb connect ${host}:${WirelessDebugging_Port}
$adb tcpip 5555
As far as I know, you can call a Termux .sh script from another app by using the name of the script and the variable as parameter. In our case, we can call it like this:
connadb3.sh 43002 - where 43002 is just a random port for the purpose of this example.
The script will receive the port "43002" and use it in the variable "$1", which is in the 4th line of the script. The important part is the 2nd to last line, where it basically connects to the wireless debugging via:
adb connect localhost
ort
The final line is optional. I'm using it to run adb tcpip 5555. You don't need to if you don't need it. OK, so that's the setup done in Termux. Now over to MacroDroid.
Go to the Templates store and search and add the following macros:
and
The first macro runs on device boot. It will enable your wifi, wait about 10s for wifi to be connected and then run "adb_wifi_enabled = 1". This turns on the setting "Wireless Debugging" under Developer Settings. On my phone (Xiaomi), I'll get a popup asking to "Always allow on this network" and even if you tick "Always allow", you'll get this prompt on every startup. This is a known Android bug and it seems that other people on other phone brands have the same issue.
The popup can take a few seconds to show up, so here we wait a few seconds. I then use UI Interaction to tick "Always allow on this network" (this part is optional because of the bug as mentioned above, so you can disable this step if you want). The important part is that it ticks allow, so that it starts wireless debugging in the background.
Now, because Android logcat captures adbd events, we can capture exactly what port is generated by wireless debugging. This is what the 2nd macro does. The trigger is that if abd message is found, specifically the "wifi started on port" message (from wireless debugging), then the trigger will be invoked. The tricky and annoying thing here is I can't make it run AFTER the first macro has run. Obviously, we only want to start capturing logcat AFTER we have turned on wireless debugging. I have tried using the constraint "macro 1 invoked within x minutes", but this doesn't really control the order for which macro 2 is invoked.
Therefore, my current workaround is to set macro 2 to be disabled. We run macro 1 first, then at the end of it we enable macro 2. The assumption here is that at this point, macro 1 would have turned on wireless debugging and macro 2 will immediately start capturing logcat events.
Back to macro 2. We save the logcat entry into a variable, then we extract just the port number from the variable. We then pass this port into the Termux .sh script that we created earlier, which is used in the $1 variable (i.e the first parameter) in the Termux script.
You may also need to grant MacroDroid the read_logs permission via ADB. You'll get a message about it if you haven't.
You should also double check the Termux Tasker plugin action in MacroDroid. Make sure it's not complaining about your script not being found. If yes, then you didn't add the "allow-external-apps=true" in Termux properly. Go back and re-do it, or Google it. I had to Google all of this myself.
That's it! If you got it working properly, then what should happen is everytime you boot up your phone, your wifi will connect and MacroDroid will automatically turn on wireless debugging, then use logcat to check the port and pass the port to Termux, which will then run adb connect localhost
ort and finally adb tcpip 5555 (optional). No need to use nmap or anything else to scan ports, this is much more accurate and way quicker.
Of course, you can set the trigger for the logcat event to something else, such as a MacroDroid quick tile or whatever. Just make sure you know that logcat event capture runs continually (it seems that way) and so it'll slow down your phone if you misconfigure this particular trigger.
NOTEs:
- Because I don't know of a good way to click "Allow" for wireless debugging, so the MacroDroid UI interaction is what I'm using and obviously there should be a better way to do this. Sometimes, the click taps the same spot twice and so it's annoying.
- If you have previously run adb tcpip 5555 and you're trying to test this whole thing, then you won't get any logcat events. You need to kill adb server first, or just restart phone.
- If you're rooted, you can simply backup your Termux files/data from here --> /data/data/com.termux/files/home. Then, when you need to restore, just copy all files/data back to the same directory and make sure to set the permission on the files and folders. I use Root Explorer to do this and I just set permissions to 777 (yes, I know it's a bad idea but hey it works).