[Android Software Development] gakio12:
***Requires Root*** (unless someone can show me a way that doesn't)

Checks the cell network type, NOT DATA TYPE.

Background: I am a truck driver for three months a year during my summer break from university. I love streaming music, but I love Nexus phones (and my $30 a month truly unlimited everything from T-Mobile!). Because I am on T-Mobile, my data connection on the interstates SUCK. So, I went ahead and got a prepaid Verizon mobile hotspot with the 10GB a month plan to use while on the road. However, I go through large cities frequently, so using the hotspot all the time is not ideal. I came up with the idea to use Tasker to track which mobile network type I am connected to, and turn off wifi when I gain HSPA or LTE, or turn wifi on when I lose HSPA or LTE. Sounds simple right? WRONG! The problem is that the only state context that Tasker has is Mobile Data, and can be set to 2G, 3G, 3G-HSPA, or 4G. I thought this would be enough, however, once wifi is connected, the mobile data state becomes None. So instead, I had to figure which string I needed from a dumpsys, output that string's value to a Tasker variable, and from there I could set If / Else statements. So let's begin:

First, I had to open up a terminal emulator and type in:

su

This will ask for Superuser access, simply allow always, and accept.

Next, type in:

dumpsys | grep DUMP

This will show you the different services you can view with dumpsys! Quite a bit of info there.

Now, from the helpful people over in the Tasker groups forum, I learned that network status is in

telephony.registry

So to only show that (instead of a huge long list of all the info), you will type in:

dumpsys telephony.registry

From here you can scroll through all the states. There will be a lot of values, but the one we are looking for is

mServiceState

In this case, I see it as (will vary between phone and provider):

mServiceState=0 home T-Mobile T-Mobile 310260 HSPA CSS not supported -1 -1 RoamInd=-1 DefRoamInd=-1 EmergOnly=False

"That's cool. Now what do I do with it?"

I'll tell you! With this info, head over to Tasker and make a profile (I used a time profile to activate every two minutes). After you make your profile, name a new task (I named it Celltype). Now add an activity, select Shell, then Run Shell. We've got some cool stuff here right? Yes, we do. It is a good idea to simply type in the command:

dumpsys telephony.registry | grep mServiceState | awk '{print}'

and set your timeout (mine is 4 seconds). Check the Use Root box, then in the Store Output In field, type a new variable name (I used %STATE); don't use a variable that is already in use! Dandy! Now just hit the back button and add another action. This time, select Alert and pick one (I picked popup, so that is what I am using in this guide).

Find the Text field, and enter your variable (%STATE) for me. Now just hit the back button, then hit the Play button in the lower left hand corner. You should get a Superuser request, allow it. Then, you should get a popup with the output of mServiceState, which for me is:

0 home T-Mobile T-Mobile 310260 HSPA CSS not supported -1 -1 RoamInd=-1 DefRoamInd=-1 EmergOnly=False

If you do not get a string like that, try changing '{print}' to '{print $0}' in the Command field in your Run Shell action.

Now find your network type. For me it is HSPA. Then count the position that network type text is in the mServiceState string. This is separated by spaces, so if I count mine, HSPA is in the sixth position (your's may vary). Did you notice I had you put at the end of the dumpsys in the Run Shell action:

awk | '{print]'

Well that prints out the result to whatever you tell Tasker to. You can do a lot of cool things with the print function, but I'm only going to cover what is relevant to this guide, which is the position identifier, $. In this case, since the text I want is in the sixth position, I will change the Command field in the Run Shell action to:

dumpsys telephone.registry | grep mServiceState | awk '{print $6}'

Your's may be in a different position, say it is in position three. Then you will put '{print $3}' at the end of your Command field. Again, make sure Use Root is checked and Store Output In has your created variable typed in.

Hit the back button to bring you to the main Task screen, and hit the play button in the lower left hand corner again. This time, in your popup, you should get the network type only. In my case, I got a popup that had the text, HSPA. If all goes well, and you do get ONLY the network type, then congrats! If you didn't, go back and make sure everything is correct (variable matches across actions).

If everything is dandy at this point, you can go ahead and delete the popup action (or keep it until you are all done with everything, like I did). What I did next is add a new action, selected Task and selected If. In the If action; type your created variable into the field on the left, then choose your matching method (mine is Matches) and type what you want it to match (or not match, etc) in the field on the right (for me, I typed HSDPA/HSPA/HSPA+/HSUPA/LTE, the / meaning OR).

After that, you can add whatever sub actions you want executed if your If statement is met. Once you have those done, you can add an Else action and any sub actions you want executed if your If statement is NOT met!

I have attached my XML from Tasker (just unzip, then import the XML), so you can go ahead and check out my whole project, break it apart and turn it into your own, whatever. If you share it, just please mention me. Or don't :crying:

P.S. If I missed anything, let me know! However, I AM a truck driver right now, I don't have very much time, but I will try to help if anyone needs it.