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.
- <SwitchPreference
- android:key="enable_adb"
- android:title="@string/enable_adb"
- android:summary="@string/enable_adb_summary"/>
- <!-- add start -->
- <SwitchPreference
- android:key="enable_wifi_adb"
- android:title="@string/enable_wifi_adb"
- android:summary="@string/enable_wifi_adb_summary"/>
- <!-- 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.
- <!-- add start -->
- <string name="enable_wifi_adb">Wi-Fi debugging</string>
- <string name="enable_wifi_adb_openwifi">Wi-Fi is not connected, please turn Wi-Fi on and connect it</string>
- <string name="enable_wifi_adb_summary">Wi-Fi debugging mode when Wi-Fi is connected</string>
- <string name="enable_wifi_adb_connected_summary">Wi-Fi mode is on, please adb connect on your PC:\n
- adb connect <xliff:g id="ip_address">%1$s</xliff:g>:5555</string>
- <!-- add end -->
2.2. Write the logic part in file "packages/apps/Settings/src/com/android/settings/DevelopmentSettings.Java"
First, define some variate:
- //add 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 end
Second, initialize the Wi-Fi ADB switch into the onCreate() method:
- mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);
- //add start
- mEnableWifiAdb = findAndInitSwitchPref(ENABLE_WIFI_ADB);
- //add end
Then, setup the according logic code into onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) method:
- 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);
- // Modify here, The "Developer options" status is opened.
- onPreferenceTreeClick(null, mVerifyAppsOverUsb);
- updateBugreportOptions();
- }
- }
- //add 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 end
Then, add the according logic code into onResume() method:
- //add 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 end
Then, add the according logic code into updateAllOptions() method:
- /// Modify here, 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 start
- boolean isAdbWifiChecked = Settings.Global.getInt(cr,ADB_WIFI_ENABLED_KEY, 0) != 0;
- updateSwitchPreference(mEnableWifiAdb, isAdbWifiChecked);
- //add end
Finally, is the broadcast register code:
- 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());
- }
- NetworkInfo.State wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
- if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
- Log.i(TAG, "Wi-Fi 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 == NetworkInfo.State.DISCONNECTED || wifi == NetworkInfo.State.DISCONNECTING) {
- Log.i(TAG, "Wi-Fi 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));
- }
- }
- };
2.3. Implement the system interface
Allocate the "init.rc" file in Android source code or target root image files, add the below code:
- #add start
- 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
- #add end
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.)
Working