Android system transplantation and debugging -------) How to add an adb wifi wireless debugging function [Developer options]-[Wifi debugging]
First understand how to set the function of adb wifi wireless debugging, as shown below.
1. Open the adb tcp connection port on the mobile phone
: / $ setprop service.adb.tcp.port 5555
: / $ stop adbd
: / $ start adbd
Among them setprop is used to set system properties, here does not require root permissions, su. It can be set through the adb shell or through the Android terminal installed on the mobile phone.
2. Setting up and using the computer
Connect to adb, where phone_ipaddress and portnumber refer to the phone's IP and the listening port number set before (such as 5555)
adb connect phone_ipaddress: portnumber
Direct adb shell or adb -s device shell to connect to the device
If you want to disconnect as follows:
adb disconnect phone_ipaddress: port
But every time this kind of setting is very cumbersome, so make a switch directly in the setting to do the settings on the mobile phone side, as shown below:
1. Turn on the switch and connect to Wifi
2. Turn on the switch, Wifi is not connected
3. The switch is not turned on
In this way, adb wifi debugging can be performed directly, and the mobile terminal does not need to use USB debugging to set the modified attributes every time, so that adb wifi debugging can be activated.
================================================== ================================================== ==
Let's talk about the implementation ideas in detail:
Step 1: Make a switch similar to [USB debugging] [Wifi debugging] in [Settings]-> [Developer options]
The second step: [Wifi debugging] switch on activates the monitor 5555 port, used for adb wifi debugging, [Wifi debugging] switch off does not monitor the 5555 port, so adb wifi debugging is not possible
Step 3: When the [Wifi debugging] switch is turned on and off in the second step, call the corresponding interface to set
Step 4: Write the interface to be called in the third step
1. Add the switch to the layout first
First find the layout file, packages / apps / Settings / res / xml / development_prefs.xml, find the corresponding code of the [USB debugging] switch, and then add a [Wifi debugging] switch, the code is as follows:
<SwitchPreference android: key = "enable_adb" android: title = "@ string / enable_adb" android: summary = "@ string / enable_adb_summary" /> <!-Added by ouyang start-> <SwitchPreference android: key = "enable_wifi_adb "android: title =" @ string / enable_wifi_adb "android: summary =" @ string / enable_wifi_adb_summary "/> <!-added by ouyang end-> In the above code, the first code is the code of the [USB debugging] switch , The second is the code of the [Wifi debugging] switch I added, and then why the string does the corresponding international operation, the code is as follows:
Added in packages / apps / Settings / res / values-zh-rCN / strings.xml:
<!-added by ouyang start 2015-12-17-> <string name = "enable_wifi_adb"> Wifi debugging </ string> <string name = "enable_wifi_adb_openwifi"> Wifi is not connected, please connect to wifi </ string> < string name = "enable_wifi_adb_summary"> Enable debug mode after connecting to Wifi </ string> <string name = "enable_wifi_adb_connected_summary"> Wifi debugging is turned on, you can enter the following command to debug on the computer: / n adb connect <xliff: g id = "ip_address">% 1 $ s </ xliff: g>: 5555 </ string> <!-added by ouyang end 2015-12-17->
Added in packages / apps / Settings / res / values / strings.xml:
<!-added by ouyang start 2015-12-17-> <string name = "enable_wifi_adb"> Wifi debugging </ string> <string name = "enable_wifi_adb_openwifi"> Wifi is not connected, please turn wifi on and connect it </ string> <string name = "enable_wifi_adb_summary"> Debug mode when Wifi is connected </ string> <string name = "enable_wifi_adb_connected_summary"> adb wifi is on, from your computer run: / n adb connect <xliff: g id = "ip_address">% 1 $ s </ xliff: g>: 5555 </ string> <!-added by ouyang end 2015-12-17->
2. In the packages / apps / Settings / src / com / android / settings / DevelopmentSettings.java file, perform the related logic processing on the [Wifi debugging] switch that I just added.
First define several variables of the switch, as follows:
// add by ouyang 2015-12-17 start
private static final String ENABLE_WIFI_ADB = "enable_wifi_adb";
private static final String ADB_WIFI_ENABLED_KEY = "ADB_WIFI_ENABLED";
private static SwitchPreference mEnableWifiAdb;
// add by ouyang 2015-12-17 end
Then initialize the [Wifi debugging] switch in the onCreate () method as follows:
mEnableAdb = findAndInitSwitchPref (ENABLE_ADB);
// add by ouyang 2015-12-17
mEnableWifiAdb = findAndInitSwitchPref (ENABLE_WIFI_ADB);
Then in the onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference) method, do logic processing on the opening and closing operations of the [Wifi debugging] switch.
if (preference == mEnableAdb) {
if (mEnableAdb.isChecked ()) {
mDialogClicked = false;
if (mAdbDialog! = null) dismissDialogs ();
mAdbDialog = new AlertDialog.Builder (getActivity ()). setMessage (
getActivity (). getResources (). getString (R.string.adb_warning_message))
.setTitle (R.string.adb_warning_title)
.setPositiveButton (android.R.string.yes, this)
.setNegativeButton (android.R.string.no, this)
.show ();
mAdbDialog.setOnDismissListener (this);
} else {
Settings.Global.putInt (getActivity (). GetContentResolver (),
Settings.Global.ADB_ENABLED, 0);
mVerifyAppsOverUsb.setEnabled (false);
mVerifyAppsOverUsb.setChecked (false);
/// M: ALPS01256802, The "Developer options" status is opened.
onPreferenceTreeClick (null, mVerifyAppsOverUsb);
updateBugreportOptions ();
}
}
// add by ouyang 2015-12-17 start
else if (preference == mEnableWifiAdb) {
if (mEnableWifiAdb.isChecked ()) {
Settings.Global.putInt (getActivity (). GetContentResolver (), ADB_WIFI_ENABLED_KEY, 1);
android.os.SystemProperties.set ("sys.connect.adb.wifi", "1");
The
WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo ();
int ipAddress = wifiInfo.getIpAddress ();
String ipAddressString = (ipAddress & 0xFF) + "." + ((IpAddress >> 8) & 0xFF) + "." +
((ipAddress >> 16) & 0xFF) + "."
+ (ipAddress >> 24 & 0xFF);
Log.i (TAG, "ipAddress =" + ipAddress);
Log.i (TAG, "ipAddressString =" + ipAddressString);
if ("0.0.0.0" .equals (ipAddressString)) {
mEnableWifiAdb.setSummary (getResources (). getString (R.string.enable_wifi_adb_openwifi));
} else {
mEnableWifiAdb.setSummary (getResources ().
getString (R.string.enable_wifi_adb_connected_summary, ipAddressString));
}
} else {
Settings.Global.putInt (getActivity (). GetContentResolver (), ADB_WIFI_ENABLED_KEY, 0);
android.os.SystemProperties.set ("sys.connect.adb.wifi", "0");
mEnableWifiAdb.setSummary (getResources (). getString (R.string.enable_wifi_adb_summary));
}
}
// add by ouyang 2015-12-17 end
The above code is mainly,
When the [Wifi Debug] switch is turned on, save a value that originally indicated that the [Wifi Debug] switch was turned on and off is 1, and then call the interface android.os.SystemProperties.set ("sys.connect.adb.wifi", "1"); If wifi is turned on at this time, the corresponding adb wifi command is displayed, such as when the ip address is 192.168.107.201, "Wifi debugging is turned on, you can enter the following command to debug on the computer: adb connect 192.168 .107.201: 5555 "; if wifi is on, it will prompt" Wifi is not connected, please connect wifi "
When the [Wifi Debugging] switch is turned off, save a value originally indicating that the [Wifi Debugging] switch is turned on and off is 0, and then call the interface android.os.SystemProperties.set ("sys.connect.adb.wifi", "0");
The state value of ADB_WIFI_ENABLED_KEY, when entering the Activity, if the [Wifi debugging] switch was turned on last time, then the next time you come in, it will show that the [Wifi debugging] switch is on, otherwise it is off. Let's talk about the relevant code of the status value ADB_WIFI_ENABLED_KEY, as follows:
In the onResume () method, add the following code:
// add by ouyang 2015-12-17 start
IntentFilter filter = new IntentFilter ();
filter.addAction (WifiManager.NETWORK_STATE_CHANGED_ACTION);
filter.addAction (WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION);
getActivity (). registerReceiver (wifiBroadcastReceiver, filter);
The
boolean isAdbWifiChecked = Settings.Global.getInt (getActivity (). getContentResolver (), ADB_WIFI_ENABLED_KEY, 0)! = 0;
mEnableWifiAdb.setChecked (isAdbWifiChecked);
Log.i (TAG, "isAdbWifiChecked:" + isAdbWifiChecked);
// add by ouyang 2015-12-17 end
The code means that when you enter the Activity, you will dynamically register a broadcast to monitor the change of the Wifi status, and then dynamically display the IP address obtained by the Android phone. Then according to the state value of ADB_WIFI_ENABLED_KEY to display the on and off state of the [Wifi debugging] switch.
In the updateAllOptions () method, we also have to do the processing, the code is as follows:
/// M: CR ALPS00244115. Lock and unlock screen, the "USB debugging" is unchecked.
boolean isChecked = (mAdbDialog! = null && mAdbDialog.isShowing ())? true:
(Settings.Global.getInt (cr, Settings.Global.ADB_ENABLED, 0)! = 0);
updateSwitchPreference (mEnableAdb, isChecked);
// add by ouyang 2015-12-17 start
boolean isAdbWifiChecked = Settings.Global.getInt (cr, ADB_WIFI_ENABLED_KEY, 0)! = 0;
updateSwitchPreference (mEnableWifiAdb, isAdbWifiChecked);
// add by ouyang 2015-12-17 end
/// M: update usb preference again
mExt.customUSBPreference (mEnableAdb);
// add by ouyang 2015-12-17
mExt.customUSBPreference (mEnableWifiAdb);
Let's talk about the above dynamically registered broadcast, the code is as follows:
BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver () {
@Override
public void onReceive (Context context, Intent intent) {
String TAG = "wifiBroadcastReceiver";
boolean isAdbWifiChecked = mEnableWifiAdb.isChecked ();
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService (Context.CONNECTIVITY_SERVICE);
NetworkInfo net = connectivityManager.getActiveNetworkInfo ();
if (net == null) {
Log.i (TAG, "No net type");
if (isAdbWifiChecked) {
mEnableWifiAdb.setSummary (getResources ()
.getString (R.string.enable_wifi_adb_openwifi));
}
} else {
Log.i (TAG, "Net Type:" + net.getTypeName ());
}
State wifi = connectivityManager.getNetworkInfo (ConnectivityManager.TYPE_WIFI) .getState ();
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
Log.i (TAG, "wifi connected");
WifiManager wifiManager = (WifiManager) context.getSystemService (Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo ();
int ipAddress = wifiInfo.getIpAddress ();
String ipAddressString = (ipAddress & 0xFF) + "." + ((IpAddress >> 8) & 0xFF) + "."
+ ((ipAddress >> 16) & 0xFF) + "." + (ipAddress >> 24 & 0xFF);
if (isAdbWifiChecked) {
mEnableWifiAdb.setSummary (
getResources (). getString (
R.string.enable_wifi_adb_connected_summary, ipAddressString));
}
Log.i (TAG, getResources (). GetString (
R.string.enable_wifi_adb_connected_summary, ipAddressString));
} else if (wifi == State.DISCONNECTED || wifi == State.DISCONNECTING) {
Log.i (TAG, "wifi disconnected");
if (isAdbWifiChecked) {
mEnableWifiAdb.setSummary (
getResources (). getString (R.string.enable_wifi_adb_openwifi));
}
Log.i (TAG, getResources (). GetString (
R.string.enable_wifi_adb_connected_summary));
}
}
};
The above code is mainly used to indicate the Wifi connection status of the Android phone when the [Wifi debugging] switch is turned on. If wifi is not connected, it will prompt "Wifi is not connected, please connect wifi", if Wifi is connected, it will be dynamic The IP address obtained from the DHCP server will be displayed. For example, when the IP address is 192.168.107.201, it will display, "Wifi debugging is turned on. On the computer side, you can enter the following command for debugging: adb connect 192.168.107.201:5555"
====================================================================== ================================
Ok, in the packages / apps / Settings /, that is, the settings APP level code is written, let's talk about how to implement the two interfaces called.
android.os.SystemProperties.set ("sys.connect.adb.wifi", "1");
android.os.SystemProperties.set ("sys.connect.adb.wifi", "0");
First find the frameworks / base / core / java / android / os / SystemProperties.java file, find the set (String key, String val) method, the code is as follows, and find that the native_set (key, val) method is called.
/ **
* Set the value for the given key.
* @throws IllegalArgumentException if the key exceeds 32 characters
* @throws IllegalArgumentException if the value exceeds 92 characters
* /
public static void set (String key, String val) {
if (key.length ()> PROP_NAME_MAX) {
throw new IllegalArgumentException ("key.length>" + PROP_NAME_MAX);
}
if (val! = null && val.length ()> PROP_VALUE_MAX) {
throw new IllegalArgumentException ("val.length>" +
PROP_VALUE_MAX);
}
native_set (key, val);
}
The native_set (key, val) method is defined as follows:
private static native void native_set (String key, String def);
This interface class registers the corresponding cpp interface android_os_SystemProperties.cpp in the initial running environment. The actual operation calls the interface corresponding to the cpp file through JNI:
frameworks / base / core / jni / AndroidRuntime.cpp
namespace android {
extern int register_android_os_SystemProperties (JNIEnv * env);
}
frameworks / base / core / jni / android_os_SystemProperties.cpp
static void SystemProperties_set (JNIEnv * env, jobject clazz,
jstring keyJ, jstring valJ)
{
int err;
const char * key;
const char * val;
if (keyJ == NULL) {
jniThrowNullPointerException (env, "key must not be null.");
return;
}
key = env-> GetStringUTFChars (keyJ, NULL);
if (valJ == NULL) {
val = ""; / * NULL pointer not allowed here * /
} else {
val = env-> GetStringUTFChars (valJ, NULL);
}
err = property_set (key, val);
env-> ReleaseStringUTFChars (keyJ, key);
if (valJ! = NULL) {
env-> ReleaseStringUTFChars (valJ, val);
}
if (err <0) {
ALOGE ("setproperty key =% s value =% s err =% d \ n", key, val, err);
jniThrowException (env, "java / lang / RuntimeException",
"failed to set system property");
}
}
Ok, for the analysis of the android.os.SystemProperties.set () method, you can refer to
http://blog.csdn.net/ameyume/article/details/8056492
http://www.blogjava.net/anymobile/articles/301989.html
Write the implementation of the following two interfaces directly below
android.os.SystemProperties.set ("sys.connect.adb.wifi", "1");
android.os.SystemProperties.set ("sys.connect.adb.wifi", "0");
Add the following code to the device / lentek / lentk6735_66t_l1 / E580 / init.rc file, where E580 is the name of the project.
#added by ouyang start connect adb with wifi
on property: sys.connect.adb.wifi = 1
setprop service.adb.tcp.port 5555
stop adbd
start adbd
on property: sys.connect.adb.wifi = 0
setprop service.adb.tcp.port ""
stop adbd
start adbd
#added by ouyang end connect adb with wifi
The above code is when calling
android.os.SystemProperties.set ("sys.connect.adb.wifi", "1"); and sys.connect.adb.wifiandroid.os.SystemProperties.set ("sys.connect.adb.wifi", "0 "); The specific implementation of the interface,
That is, when the "sys.connect.adb.wifi" property is set to 1, monitor port 5555,
When the "sys.connect.adb.wifi" property is set to 0, no port is monitored
================================================== ==================================
Okay, let's test how the function works again. Turn on wifi and turn on the [Wifi debugging] switch again, as shown below:
It can be seen that the IP address of the Android phone is 192.168.107.168 at this time, so the following test on the PC side to see if it can be debugged wirelessly with adb wifi, use the command adb connect 192.168.107.168 to connect, adb disconnect 192.168.107.168 to disconnect, such As shown:
C: \ Documents and Settings \ Administrator> adb connect 192.168.107.168
connected to 192.168.107.168:5555
C: \ Documents and Settings \ Administrator> adb shell
shell @ lentk6735_66t_l1: / $ ll
drwxr-xr-x root root 2015-12-21 10:15 acct
drwxrwx --- system cache 2015-12-17 19:01 cache
lrwxrwxrwx root root 1970-01-01 08:00 charger-> / sbin / healthd
dr-x ------ root root 2015-12-21 10:15 config
drwxr-xr-x root root 2015-12-21 10:15 custom
lrwxrwxrwx root root 2015-12-21 10:15 d-> / sys / kernel / debug
drwxrwx--x system system 2015-12-21 10:16 data
-rw-r--r-- root root 385 1970-01-01 08:00 default.prop
drwxr-xr-x root root 2015-12-21 10:15 dev
-rw-r--r-- root root 127 1970-01-01 08:00 enableswap.sh
lrwxrwxrwx root root 2015-12-21 10:15 etc-> / system / etc
-rw-r--r-- root root 1851 1970-01-01 08:00 factory_init.project.rc
-rw-r--r-- root root 18861 1970-01-01 08:00 factory_init.rc
-rw-r--r-- root root 31413 1970-01-01 08:00 file_contexts
-rw-r ----- root root 1980 1970-01-01 08:00 fstab.mt6735
-rwxr-x --- root root 543928 1970-01-01 08:00 init
-rwxr-x --- root root 605 1970-01-01 08:00 init.aee.rc
-rwxr-x --- root root 2065 1970-01-01 08:00 init.c2k.rc
-rwxr-x --- root root 1071 1970-01-01 08:00 init.environ.rc
-rwxr-x --- root root 3548 1970-01-01 08:00 init.modem.rc
-rwxr-x --- root root 45152 1970-01-01 08:00 init.mt6735.rc
-rwxr-x --- root root 32013 1970-01-01 08:00 init.mt6735.usb.rc
-rwxr-x --- root root 963 1970-01-01 08:00 init.no_ssd.rc
-rwxr-x --- root root 4411 1970-01-01 08:00 init.project.rc
-rwxr-x --- root root 22787 1970-01-01 08:00 init.rc
-rwxr-x --- root root 972 1970-01-01 08:00 init.recovery.mt6735.rc
-rwxr-x --- root root 2288 1970-01-01 08:00 init.trace.rc
-rwxr-x --- root root 3885 1970-01-01 08:00 init.usb.rc
-rwxr-x --- root root 583 1970-01-01 08:00 init.xlog.rc
-rwxr-x --- root root 301 1970-01-01 08:00 init.zygote32.rc
-rwxr-x --- root root 531 1970-01-01 08:00 init.zygote64_32.rc
-rw-r--r-- root root 1004 1970-01-01 08:00 meta_init.c2k.rc
-rw-r--r-- root root 1062 1970-01-01 08:00 meta_init.modem.rc
-rw-r--r-- root root 1655 1970-01-01 08:00 meta_init.project.rc
-rw-r--r-- root root 14464 1970-01-01 08:00 meta_init.rc
drwxrwxr-x root system 2015-12-21 10:15 mnt
drwxrws --- root system 2015-12-17 18:59 nvdata
drwxrwx--x system system 2015-12-21 10:15 persist
dr-xr-xr-x root root 1970-01-01 08:00 proc
-rw-r--r-- root root 9326 1970-01-01 08:00 property_contexts
drwxrwx --- system system 2015-12-17 18:59 protect_f
drwxrwx --- system system 2015-12-17 18:59 protect_s
drwx ------ root root 2015-12-05 18:05 root
drwxr-x --- root root 1970-01-01 08:00 sbin
lrwxrwxrwx root root 2015-12-21 10:15 sdcard-> / storage / sdcard0
-rw-r--r-- root root 471 1970-01-01 08:00 seapp_contexts
-rw-r--r-- root root 80 1970-01-01 08:00 selinux_version
-rw-r--r-- root root 258377 1970-01-01 08:00 sepolicy
-rw-r--r-- root root 11419 1970-01-01 08:00 service_contexts
drwxr-x--x root sdcard_r 2015-12-21 10:15 storage
dr-xr-xr-x root root 2015-12-21 10:15 sys
drwxr-xr-x root root 1970-01-01 08:00 system
-rw-r--r-- root root 8642 1970-01-01 08:00 ueventd.rc
lrwxrwxrwx root root 2015-12-21 10:15 vendor-> / system / vendor
shell @ lentk6735_66t_l1: / $ exit
C: \ Documents and Settings \ Administrator> adb disconnect 192.168.107.168
It is found that the adb wifi wireless debugging can be performed normally. Turn off the [Wifi debugging] switch [Wifi debugging] switch, and then connect, you will not be able to connect. As follows:
C: \ Documents and Settings \ Administrator> adb connect 192.168.107.168
unable to connect to 192.168.107.168:5555
C: \ Documents and Settings \ Administrator>