Changing the preferred network mode in Android has always been a hassle. The backend to the function changed many times over the years and has become more and more complicated.
Right now there seems to be no universal way to change the settings in an automated way without using the GUI. Every phone model / ROM / provider combination results in different code numbers to access Androids interface to control the baseband modem.
But if you are up for the challenge, here is the way how to do it for your device.
You need to find these values:
– Numeric Code to write to your phones modem device
– Numeric Code to read from your phones modem device
– Numeric Codes for every preferred network setting you want to use (2G,3G,4G, etc.)
OK, lets go!
Download firmware.jar from your phone:
adb pull /system/framework/framework.jar
Unpack firmware.jar:
unzip ./firmware.jar
Search files in firmware.jar for the needed service code to write to the modem interface on your phone:
> grep -r "TRANSACTION_setAllowedNetworkTypesForReason" . ./f/sources/com/android/internal/telephony/ITelephony.java: static final int TRANSACTION_setAllowedNetworkTypesForReason = 108;
Code was found in „ITelephony.java“ on my device (Pixel 6a). Code is 108
Search files in firmware.jar for the needed service code to read from the modem interface on your phone:
> grep -r "TRANSACTION_getAllowedNetworkTypesForReason" . ./f/sources/com/android/internal/telephony/ITelephony.java: static final int TRANSACTION_getAllowedNetworkTypesForReason = 107;
Code was found in „ITelephony.java“ on my device (Pixel 6a). Code is 107
Now we have the needed codes to read from (107) and write to (108) the modem interface.
Next thing to do is go through all the available settings in the GUI.
I will set my phone to 2G only for this example. (Settings -> Network -> Simcard -> Cogwheel -> Preferred Network mode). You have to repeat this step for all the other settings too (3G,4G, etc.)
After setting the network mode to 2G read it’s code from Android:
adb shell #Code 107 has been identified in the last step. It can be different on your device service call phone 107 Result: Parcel( 00000000 00008003 00000000 '............')
Now we got a Hex value for the phones 2G-setting (00008003)
We need to convert that to decimal, because the interface command expects that as input.
You can use this website for easy conversion: https://www.rapidtables.com/convert/number/hex-to-decimal.html
(HEX) 00008003 converts to (DEC) 32771
Cool. We now have everything we need.
Last thing to do is to try it out.
Open up the settings GUI to see if it works. The prefered network entry in the GUI should change instantly to 2G after using the following command.
Use the code for writing you identified above (108 in my case) and the code for 2G from the last step (32771 in my case):
adb shell service call phone 108 i32 0 i32 0 i64 "32771" Result: Parcel( 00000000 00000001 '........')
Yay! That worked. Nice. The device switched to 2G mode.
Repeat this for all the network type settings your device offers to get the other HEX-codes. Convert them to DEC and use them.
Use the commands to switch network modes on the fly with automation apps like Tasker or Macrodroid.
Thank you to Dev777 who brought me onto the right track in this forum thread:
https://www.macrodroidforum.com/index.php?threads/change-preferred-network-mode.2130/