How to extend ADB debugging via Wi-Fi?  

  RSS

Koncn Tseng
Active MemberAdmin
Joined:1 year  ago
Posts: 18
October 23, 2016 12:01 pm  

With daily debugging the X20 96Boards, especially ADB debug it via USB that you must set the board into USB Device Mode. If you are using USB mouse at the same time, you need to switch the "USB HOST SEL" frequently.

If so, we can image that you are suffering the pain of USB working mode ...

Here, let us introduce ADB debugging via Wi-Fi for you. Wi-Fi ADB makes it easy to debug/test your Android apps directly on board via WiFi TCP/IP connection. No need to use USB cable. After enabling ADB over Wi-Fi, to connect your computer to the device open the console and run. We summary 2 methods for your reference:

  • Install "Wi-Fi ADB" app directly;
  • Integrate Wi-Fi ADB with "Settings" -> "Developer Options" by your hands with source code;

 

1. Install Wi-Fi ADB app

Search and install some Wi-Fi ADB application from Google Play or other App Store.

Before installing such app, please make sure the board is ROOTED which is running eng or userdebug ROM. If you don't know what it means or your board is not rooted please don't install. You also need to grant this app su permission when being asked.

 When you turn on or off the Wi-Fi ADB, the back end of Android goes like this:

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

 

If you don't trust the 3rd party apk, and want to integrate above code into Android Settings with friendly UI, let's move forward to section 2.

 

2. Integrate Wi-Fi ADB

 During this section, our goal is to develop Wi-Fi ADB with the below UI based on Android source code.

 

2.1. Add a switch in the layout file

Allocate to the layout file "packages/apps/Settings/res/xml/development_prefs.xml", Insert the Wi-Fi ADB switch code after ADB.

 

  1. <SwitchPreference  
  2.     android:key="enable_adb"  
  3.     android:title="@string/enable_adb"  
  4.     android:summary="@string/enable_adb_summary"/>  
  5.   
  6. <!-- add start -->  
  7. <SwitchPreference  
  8.     android:key="enable_wifi_adb"  
  9.     android:title="@string/enable_wifi_adb"  
  10.     android:summary="@string/enable_wifi_adb_summary"/>  
  11. <!-- add end -->

 

 Find the localization file "packages/apps/Settings/res/values/strings.xml", Insert the following code in default English. Meanwhile, you can extend it to your prefer language.

 

  1. <!-- add start -->  
  2.    <string name="enable_wifi_adb">Wi-Fi debugging</string>  
  3.    <string name="enable_wifi_adb_openwifi">Wi-Fi is not connected, please turn Wi-Fi on and connect it</string>  
  4.    <string name="enable_wifi_adb_summary">Wi-Fi debugging mode when Wi-Fi is connected</string>  
  5.    <string name="enable_wifi_adb_connected_summary">Wi-Fi mode is on, please adb connect on your PC:\n  
  6.    adb connect <xliff:g id="ip_address">%1$s</xliff:g>:5555</string>  
  7. <!-- add end --> 

 

2.2. Write the logic part in file "packages/apps/Settings/src/com/android/settings/DevelopmentSettings.Java"

 

First, define some variate:

 

  1. //add start  
  2. private static final String ENABLE_WIFI_ADB = "enable_wifi_adb";  
  3. private static final String ADB_WIFI_ENABLED_KEY = "ADB_WIFI_ENABLED";  
  4. private static SwitchPreference mEnableWifiAdb;  
  5. //add end 

 

Second, initialize the Wi-Fi ADB switch into the onCreate() method:

 

  1. mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);  
  2. //add start   
  3. mEnableWifiAdb = findAndInitSwitchPref(ENABLE_WIFI_ADB); 
  4. //add end

 

Then, setup the according logic code into onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) method:

 

  1.        if (preference == mEnableAdb) {  
  2.          if (mEnableAdb.isChecked()) {  
  3.             mDialogClicked = false;  
  4.             if (mAdbDialog != null) dismissDialogs();  
  5.             mAdbDialog = new AlertDialog.Builder(getActivity()).setMessage(  
  6.                     getActivity().getResources().getString(R.string.adb_warning_message))  
  7.                     .setTitle(R.string.adb_warning_title)  
  8.                     .setPositiveButton(android.R.string.yes, this)  
  9.                     .setNegativeButton(android.R.string.no, this)  
  10.                     .show();  
  11.             mAdbDialog.setOnDismissListener(this);  
  12.         } else {  
  13.             Settings.Global.putInt(getActivity().getContentResolver(),  
  14.                     Settings.Global.ADB_ENABLED, 0);  
  15.             mVerifyAppsOverUsb.setEnabled(false);  
  16.             mVerifyAppsOverUsb.setChecked(false);  
  17.             // Modify here, The "Developer options" status is opened.  
  18.             onPreferenceTreeClick(null, mVerifyAppsOverUsb);  
  19.             updateBugreportOptions();  
  20.         }  
  21.     }   
  22.     //add start  
  23.     else if (preference == mEnableWifiAdb) {  
  24.         if (mEnableWifiAdb.isChecked()) {  
  25.             Settings.Global.putInt(getActivity().getContentResolver(),ADB_WIFI_ENABLED_KEY, 1);  
  26.             android.os.SystemProperties.set("sys.connect.adb.wifi","1");  
  27.               
  28.             WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);    
  29.             WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
  30.             int ipAddress = wifiInfo.getIpAddress();     
  31.             String ipAddressString = (ipAddress & 0xFF ) + "." +((ipAddress >> 8 ) & 0xFF) + "." +         
  32.                     ((ipAddress >> 16 ) & 0xFF) + "." +( ipAddress >> 24 & 0xFF) ;  
  33.             Log.i(TAG, "ipAddress="+ipAddress);  
  34.             Log.i(TAG, "ipAddressString="+ipAddressString);  
  35.             if ("0.0.0.0".equals(ipAddressString)) {  
  36.                 mEnableWifiAdb.setSummary(getResources().getString(R.string.enable_wifi_adb_openwifi));  
  37. }else{  
  38.     mEnableWifiAdb.setSummary(getResources().  
  39.             getString(R.string.enable_wifi_adb_connected_summary,ipAddressString));  
  40. }  
  41.         } else {  
  42.             Settings.Global.putInt(getActivity().getContentResolver(),ADB_WIFI_ENABLED_KEY, 0);  
  43.             android.os.SystemProperties.set("sys.connect.adb.wifi","0");  
  44.             mEnableWifiAdb.setSummary(getResources().getString(R.string.enable_wifi_adb_summary));    
  45.         }  
  46.     }  
  47.     //add end

 

Then, add the according logic code into onResume() method:

 

  1. //add start  
  2. IntentFilter filter = new IntentFilter();  
  3. filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);  
  4. filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);  
  5. filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
  6. getActivity().registerReceiver(wifiBroadcastReceiver, filter);  
  7.   
  8. boolean isAdbWifiChecked = Settings.Global.getInt(getActivity().getContentResolver(),ADB_WIFI_ENABLED_KEY, 0) != 0;  
  9.    mEnableWifiAdb.setChecked(isAdbWifiChecked);  
  10.    Log.i(TAG, "isAdbWifiChecked:" + isAdbWifiChecked);    
  11. //add end

 

Then, add the according logic code into updateAllOptions() method:

 

  1. /// Modify here, Lock and unlock screen, the "USB debugging" is unchecked.  
  2.         boolean isChecked = (mAdbDialog != null && mAdbDialog.isShowing()) ? true :  
  3.                     (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) != 0);  
  4.         updateSwitchPreference(mEnableAdb, isChecked);  
  5.          
  6.         //add start  
  7.         boolean isAdbWifiChecked = Settings.Global.getInt(cr,ADB_WIFI_ENABLED_KEY, 0) != 0;  
  8.         updateSwitchPreference(mEnableWifiAdb, isAdbWifiChecked);  
  9.         //add end

 

Finally, is the broadcast register code:

 

  1. BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver() {  
  2.         @Override  
  3.         public void onReceive(Context context, Intent intent) {  
  4.             String TAG = "wifiBroadcastReceiver";  
  5.             boolean isAdbWifiChecked = mEnableWifiAdb.isChecked();  
  6.             ConnectivityManager connectivityManager = (ConnectivityManager) context  
  7.                     .getSystemService(Context.CONNECTIVITY_SERVICE);  
  8.             NetworkInfo net = connectivityManager.getActiveNetworkInfo();  
  9.             if (net == null) {  
  10.                 Log.i(TAG, "No net type");  
  11.                 if (isAdbWifiChecked) {  
  12.                     mEnableWifiAdb.setSummary(getResources()  
  13.                             .getString(R.string.enable_wifi_adb_openwifi));  
  14.                 }  
  15.             } else {  
  16.                 Log.i(TAG, "Net Type:" + net.getTypeName());  
  17.             }  
  18.             NetworkInfo.State wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
  19.             if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {  
  20.                 Log.i(TAG, "Wi-Fi connected");  
  21.                 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
  22.                 WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
  23.                 int ipAddress = wifiInfo.getIpAddress();  
  24.                 String ipAddressString = (ipAddress & 0xFF) + "." + ((ipAddress >> 8) & 0xFF) + "."  
  25.                         + ((ipAddress >> 16) & 0xFF) + "." + (ipAddress >> 24 & 0xFF);  
  26.                 if (isAdbWifiChecked) {  
  27.                     mEnableWifiAdb.setSummary(  
  28.                             getResources().getString(  
  29.                                     R.string.enable_wifi_adb_connected_summary, ipAddressString));  
  30.                 }  
  31.                 Log.i(TAG, getResources().getString(  
  32.                         R.string.enable_wifi_adb_connected_summary, ipAddressString));  
  33.             } else if (wifi == NetworkInfo.State.DISCONNECTED || wifi == NetworkInfo.State.DISCONNECTING) {  
  34.                 Log.i(TAG, "Wi-Fi disconnected");  
  35.                 if (isAdbWifiChecked) {  
  36.                     mEnableWifiAdb.setSummary(  
  37.                             getResources().getString(R.string.enable_wifi_adb_openwifi));  
  38.                 }  
  39.                 Log.i(TAG, getResources().getString(  
  40.                         R.string.enable_wifi_adb_connected_summary));  
  41.             }  
  42.   
  43.         }  
  44.     }; 

 

2.3. Implement the system interface

 

Allocate the "init.rc" file in Android source code or target root image files, add the below code:

  1. #add start
  2. on property:sys.connect.adb.wifi=1  
  3.     setprop service.adb.tcp.port 5555  
  4.     stop adbd  
  5.     start adbd  
  6. on property:sys.connect.adb.wifi=0  
  7.     setprop service.adb.tcp.port ""  
  8.     stop adbd  
  9.     start adbd  
  10. #add end

 

Edited: 12 months  ago

ReplyQuote
dividiti
New Member
Joined:12 months  ago
Posts: 4
December 31, 2016 6:01 pm  

I think I have found a simpler method. When connected via USB, simply set the "persist.adb.tcp.port" property to the desired port number e.g. 3456:

$ adb shell ifconfig wlan0 | grep "inet addr"
$ adb shell persist.adb.tcp.port 3456
$ adb tcpip 3456
$ adb connect <ip address>:3456

The port persists on reboot. (In fact, you can reboot straight after setting the property.)

Edited: 10 months  ago

ReplyQuote
Koncn Tseng
Active MemberAdmin
Joined:1 year  ago
Posts: 18
January 4, 2017 9:49 am  

Yes, you are right. There are lots of solution to enable Wi-Fi ADB debugging.


ReplyQuote
  
Working

Please Login or Register