Gitee: You are so beautiful
background
Recently, the product introduced a network management requirement that required Ethernet on/off functionality. Here is a summary of the potholes encountered throughout the development process.
EthernetManager unavailable
There is actually an EthernetManager class that is responsible for Ethernet management under the Android.net package, but this Api is not open to us and is officially marked as hidden. Naturally this method is not applicable, so abandoned.
/**
* A class representing the IP configuration of the Ethernet network.
*
* @hide
*/
@SystemService(Context.ETHERNET_SERVICE)
public class EthernetManager {
}
Copy the code
Use the Shell
The implementation method described in this article is to use shell commands to control Ethernet on and off. Before we get to the important code, here are some of the commands we might use in adb shell.
- Enable/disable Ethernet
netcfg eth0 down/up
Copy the code
- Check the status of the Ethernet cable. 1 indicates that the Ethernet cable is inserted, and 0 indicates that the Ethernet cable is removed.
cat /sys/class/net/eth0/carrier
Copy the code
- Gets the information about the specified network adapter
Mask // Subnet mask of eth0 getProp dhcp.eth0.gateway // gateway of eth0Copy the code
- Turn wifi on/off
svc wifi enable/disable
Copy the code
Note: In the above command, eth0 is the name of the Ethernet card on my device. The name of the Ethernet card may vary from device to device. There may also be multiple network cards, depending on your own.
Run Shell commands to enable or disable Ethernet
Note: Some shell commands require root permissions. If your Android device is already root, you can send netcfg eth0 down/up commands that require root permissions. Then you can only run some commands that do not require root permission, such as getprop dhcp.eth0.mask to get the subnet mask of eht0. So one of the prerequisites for turning Ethernet on/off is that your device is already root. Otherwise, when you send a command that requires root permission, you will get an exception message as follows:
java.io.IOException Error running exec().Command: [su] Working Directory: null Environment: null
Copy the code
This section uses netcfg eth0 up as an example to describe how to enable the Ethernet. Other commands are similar. The difference is that some commands return results, while others do not.
- in
build.gradle
Add the following dependencies. This dependency package contains a lot of common Android utility classes, which is very useful.
Implementation 'com. Blankj: utilcode: 1.25.9'Copy the code
- Use the dependency package
ShellUtils
Send the following command to start the Ethernet. And in theadb shell
Performed in thenetcfg eth0 up
The effect is the same
ShellUtils.execCmd("netcfg eth0 up",true)
Copy the code
Here is the result of executing this command in the ADB shell, and the actual device does have Ethernet turned on
Is the subnet mask of connected Wifi 0.0.0.0?
The procedure for obtaining Ethernet information is described in the previous section. So I think we can talk about this pit, in fact, this pit has always existed in Android, until now the official has not been well solved, the link to this problem is as follows: Netmask of wifimager.getdhcpInfo () is always zero on Android 5.0 Find netMask on Android device
WifiManager gets DhcpInfo and the Wifi subnet mask is 0.
So how to solve it? Boy, you don’t have to think about using a Shell.
One line of code
ShellUtils.execCmd("getprop dhcp.wlan0.mask".false).successMsg
Copy the code
Again, I would like to remind you that the wireless network card of my device is called WLAN0, and the specific name is subject to your device.
conclusion
Please note ahead, this is only a pit record, nothing high knowledge. All the holes you might need to tread, I’ve done it for you. If you have a better way to achieve, you are very welcome to communicate with me.
Reference documentation
- Adb shell command common in Android
- Android Debug Bridge (adb)
- Common Android commands
- How can i enable/disable the Ethernet connection on Android?