[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】

首先弄懂怎么设置adb wifi无线调试的功能,如下所示。

1. 手机端开启adb tcp连接端口

:/$setprop service.adb.tcp.port 5555 :/$stop adbd :/$start adbd

其中setprop是用来设置系统属性的,这里不需要root权限,su即可。可通过adb shell设置,亦可通过手机上安装的Android terminal设置。

2. 电脑端的设置和使用

连接adb,其中phone_ipaddress和portnumber是指手机的ip和前面设置的监听端口号(如5555)

adb connect phone_ipaddress:portnumber

直接adb shell或adb -s 设备shell连接设备

如若要断开则如下:

adb disconnect phone_ipaddress:port

    

      但是每次这样的去设置很繁琐,所以直接在设置中做一个开关就把手机端的设置做好,如下所示:

一、打开开关,并连接Wifi

[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】_第1张图片

二、打开开关,没有连接Wifi

[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】_第2张图片

三、未打开开关

[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】_第3张图片


这样就可以直接进行adb wifi调试了,手机端就不用每次都先用USB调试去设置修改的属性,才能激活adb wifi调试。



======================================================================================================


下面来具体说说实现的思路:

第一步:在【设置】-->【开发者选项】中做一个类似于【USB调试】的开关【Wifi调试】

第二步:【Wifi调试】开关打开就激活监听5555端口,用来进行adb wifi调试,【Wifi调试】开关关闭就不监听5555端口,这样就不能进行adb wifi调试

第三步:在第二步中打开关闭【Wifi调试】开关的时候,调用相应的接口进行设置

第四步:将第三步中要调用的接口写好


1、先在布局中加入该开关

首先找到布局文件,packages/apps/Settings/res/xml/development_prefs.xml,找到【USB调试】开关的相应代码,然后再里面加一个【Wifi调试】开关,代码如下:

        <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 -->
上面代码中,第一个代码是 【USB调试】开关的代码,第二个是 我加的【Wifi调试】开关的代码,然后为什么的字符串做相应的国际化操作,代码如下:

packages/apps/Settings/res/values-zh-rCN/strings.xml 中加入:

<!-- added by ouyang start 2015-12-17 -->
    <string name="enable_wifi_adb">Wifi调试</string>
    <string name="enable_wifi_adb_openwifi">Wifi没连接,请连接wifi</string>
    <string name="enable_wifi_adb_summary">连接Wifi后启用调试模式</string>
    <string name="enable_wifi_adb_connected_summary">Wifi调试已打开,在电脑端你可以输入以下命令进行调试:\n
         			adb connect <xliff:g id="ip_address">%1$s</xliff:g>:5555</string>
    <!-- added by ouyang end 2015-12-17 -->

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、在packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java文件中,对刚才加入的【Wifi调试】开关进行相关的逻辑处理。

首先先定义开关的几个变量,如下:

    //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

然后再onCreate()方法中初始化【Wifi调试】开关,如下:

   mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);
   //add by ouyang 2015-12-17  
   mEnableWifiAdb = findAndInitSwitchPref(ENABLE_WIFI_ADB);
然后在onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)方法中对 【Wifi调试】开关的打开和关闭操作做逻辑处理。

           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");
            	
            	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

上面的代码主要是,

       当【Wifi调试】开关打开的时候,保存一个原来表示该【Wifi调试】开关打开关闭的状态的值为1,然后调用接口android.os.SystemProperties.set("sys.connect.adb.wifi","1");如果wifi这个时候打开就显示相应的adb wifi命令,如ip地址为192.168.107.201的时候就显示,"Wifi调试已打开,在电脑端你可以输入以下命令进行调试:adb connect 192.168.107.201:5555";如果wifi为打开,就提示“Wifi没连接,请连接wifi

       当【Wifi调试】开关关闭的时候,保存一个原来表示该【Wifi调试】开关打开关闭的状态的值为0,然后调用接口android.os.SystemProperties.set("sys.connect.adb.wifi","0");


        ADB_WIFI_ENABLED_KEY这个状态值,在进入该Activity的时候,如果上次打开了【Wifi调试】开关,那么下次进来的时候就要显示【Wifi调试】开关是打开状态,否则是关闭状态。下面说说ADB_WIFI_ENABLED_KEY这个状态值的相关代码,如下:

在onResume()方法中,加入下面代码:

        //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);
    	
    	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 
代码的意思是,进入该Activity就动态的注册一个广播,用来监听Wifi的状态改变的,然后来动态的显示Android手机获取的IP地址。然后根据 ADB_WIFI_ENABLED_KEY这个状态值来显示 【Wifi调试】开关的打开关闭状态。


在updateAllOptions()方法中,也要去做下处理,代码如下:

/// 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);

下面来说说上面动态注册的广播,代码如下:

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));
			}

		}
	};

上面的代码主要是当 【Wifi调试】开关打开的时候,来用动态表示Android手机的Wifi连接状态,如果wifi未连接,就提示“Wifi没连接,请连接wifi”,如果Wifi连接了,就动态的将从DHCP服务器获取的IP地址显示出来,如ip地址为 192.168.107.201的时候就显示, "Wifi调试已打开,在电脑端你可以输入以下命令进行调试:adb connect 192.168.107.201:5555"

======================================================================================================

好吧,在packages/apps/Settings/,即Settings APP层面的代码写完了,下面来说说调用的两个接口怎么实现。

android.os.SystemProperties.set("sys.connect.adb.wifi", "1");

android.os.SystemProperties.set("sys.connect.adb.wifi", "0");


首先找到frameworks/base/core/java/android/os/SystemProperties.java文件,找到set(String key, String val)方法,代码如下,发现调用了native_set(key, val)方法。

   /**
     * 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);
    }

native_set(key, val)方法定义如下:

private static native void native_set(String key, String def);

   该接口类在初始化运行环境中注册对应的cpp接口android_os_SystemProperties.cpp,实际操作通过JNI调用的是cpp文件对应的接口:

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");
    }
}

好了,关于该android.os.SystemProperties.set()方法的分析,可以参考

http://blog.csdn.net/ameyume/article/details/8056492

http://www.blogjava.net/anymobile/articles/301989.html


下面直接写下面两个接口的实现

android.os.SystemProperties.set("sys.connect.adb.wifi", "1");

android.os.SystemProperties.set("sys.connect.adb.wifi", "0");


在device/lentek/lentk6735_66t_l1/E580/init.rc文件中加入下面的代码,其中E580是项目的名称。

#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
上面代码就是当调用

android.os.SystemProperties.set("sys.connect.adb.wifi", "1");和sys.connect.adb.wifiandroid.os.SystemProperties.set("sys.connect.adb.wifi", "0");接口的时候的具体实现,

即"sys.connect.adb.wifi"属性设置为1的时候,监听5555端口,

   "sys.connect.adb.wifi"属性设置为0的时候,不监听任何端口


====================================================================================

好了,下面来再一次测一测该功能怎么样,重新打开wifi,重新打开【Wifi调试】开关,显示如下:

[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】_第4张图片

可以看出来,此时Android手机的IP地址是192.168.107.168,因此下面在PC端测试看是否可以用adb wifi无线调试,使用命令adb connect 192.168.107.168连接,adb disconnect 192.168.107.168断开连接,如下图所示:

[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】_第5张图片

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

发现可以正常的进行adb wifi 无线调试了。将 【Wifi调试】开关【Wifi调试】 开关关闭,再去连接的话,就会无法连接了。如下所示:

[置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】_第6张图片

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>



         ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================


你可能感兴趣的:([置顶] Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】)