博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】...
阅读量:6805 次
发布时间:2019-06-26

本文共 16673 字,大约阅读时间需要 55 分钟。

首先弄懂怎么设置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

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

三、未打开开关

这样就可以直接进行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调试】开关,代码如下:

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

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

Wifi调试
Wifi没连接,请连接wifi
连接Wifi后启用调试模式
Wifi调试已打开,在电脑端你可以输入以下命令进行调试:\n adb connect
%1$s
:5555
packages/apps/Settings/res/values/strings.xml 中加入:

Wifi debugging
Wifi is not connected,please turn wifi on and connect it
Debug mode when Wifi is connected
adb wifi is on,from your computer run:\n adb connect
%1$s
:5555

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 wifion property:sys.connect.adb.wifi=1	setprop service.adb.tcp.port 5555	stop adbd	start adbdon 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手机的IP地址是192.168.107.168,因此下面在PC端测试看是否可以用adb wifi无线调试,使用命令adb connect 192.168.107.168连接,adb disconnect 192.168.107.168断开连接,如下图所示:

C:\Documents and Settings\Administrator>adb connect 192.168.107.168connected to 192.168.107.168:5555C:\Documents and Settings\Administrator>adb shellshell@lentk6735_66t_l1:/ $ lldrwxr-xr-x root     root              2015-12-21 10:15 acctdrwxrwx--- system   cache             2015-12-17 19:01 cachelrwxrwxrwx root     root              1970-01-01 08:00 charger -> /sbin/healthddr-x------ root     root              2015-12-21 10:15 configdrwxr-xr-x root     root              2015-12-21 10:15 customlrwxrwxrwx root     root              2015-12-21 10:15 d -> /sys/kernel/debugdrwxrwx--x system   system            2015-12-21 10:16 data-rw-r--r-- root     root          385 1970-01-01 08:00 default.propdrwxr-xr-x root     root              2015-12-21 10:15 dev-rw-r--r-- root     root          127 1970-01-01 08:00 enableswap.shlrwxrwxrwx 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.rcdrwxrwxr-x root     system            2015-12-21 10:15 mntdrwxrws--- root     system            2015-12-17 18:59 nvdatadrwxrwx--x system   system            2015-12-21 10:15 persistdr-xr-xr-x root     root              1970-01-01 08:00 proc-rw-r--r-- root     root         9326 1970-01-01 08:00 property_contextsdrwxrwx--- system   system            2015-12-17 18:59 protect_fdrwxrwx--- system   system            2015-12-17 18:59 protect_sdrwx------ root     root              2015-12-05 18:05 rootdrwxr-x--- root     root              1970-01-01 08:00 sbinlrwxrwxrwx 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_contextsdrwxr-x--x root     sdcard_r          2015-12-21 10:15 storagedr-xr-xr-x root     root              2015-12-21 10:15 sysdrwxr-xr-x root     root              1970-01-01 08:00 system-rw-r--r-- root     root         8642 1970-01-01 08:00 ueventd.rclrwxrwxrwx root     root              2015-12-21 10:15 vendor -> /system/vendorshell@lentk6735_66t_l1:/ $ exitC:\Documents and Settings\Administrator>adb disconnect 192.168.107.168
发现可以正常的进行adb wifi 无线调试了。将
【Wifi调试】开关【Wifi调试】
开关关闭,再去连接的话,就会无法连接了。如下所示:

C:\Documents and Settings\Administrator>adb connect 192.168.107.168unable to connect to 192.168.107.168:5555C:\Documents and Settings\Administrator>

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

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

  转载请保留原文地址

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

你可能感兴趣的文章
网络工程师真的也需要好好学习linux系统
查看>>
Entangle 2.0 “Sodium”正式发布
查看>>
Redis之父表示ARM服务器没戏!
查看>>
java调用cmd命令并捕获执行结果字符串的代码
查看>>
Eclipse中android sdk升级ADT版本过低解决办法
查看>>
MDT 2013 从入门到精通之无法分析或处理pass[specialize]文件
查看>>
桌面支持--512-Rear chassis fan not detected
查看>>
Django 开源相册组件介绍 django-photologue
查看>>
IntelliJ IDEA 14 创建Web项目
查看>>
Redis server命令
查看>>
PeerConnection
查看>>
关于ext-js 中的自定义校验
查看>>
服务端response对象属性和方法
查看>>
护眼色
查看>>
Linux下安装JDK
查看>>
axis2报错:The following error occurred during schema generation: null
查看>>
JS this指向详解
查看>>
自动布局
查看>>
【云计算的1024种玩法】手把手教你如何编译升级 OpenResty
查看>>
Mac Appium环境安装
查看>>