Android通过Wifi来调试你的应用

来源:互联网 发布:游龙网络有出什么游戏 编辑:程序博客网 时间:2024/06/07 01:35

转自:http://stormzhang.com/android/2014/08/27/adb-over-wifi/

在Android中调试我们经常要用一根USB数据线连接到手机和电脑,一方面麻烦不说,手机一直连着电脑充电时间长了对手机也是一种伤害,另一方面如果哪一天忘记带USB数据线就很悲催了。今天就来教大家一种通过wifi来连接手机调试的方法,瞬间高大上有木有?而且不需要root,以后你就可以隔空给你公司的测试妹子安装/卸载apk了。

连接方法

方法很简单,具体步骤如下:

  • 1.先确保你手机和电脑运行在同一wifi局域网内

  • 2.由于是通过adb来进行连接的,所以确保你配置了环境变量

  • 3.第一次的时候需要用手机USB连接到你的电脑,之后运行下面国外大牛写的shell脚本连接成功就可以把你的USB数据线拔掉了,然后你的电脑就可以通过wifi调试你的应用了。

# adbwifi.sh是脚本的文件名sh adbwifi.sh
  • 4.以上脚本在mac或者Linux肯定是ok的,windows上需要安装一些如msysgit或者Cygwin才可运行以上Linux shell

最后提醒:实际测试一系列手机都ok,唯独测试了手上的两部小米手机连接不上,悲剧。。

脚本内容

我在GitHub Gist上也创建了该文件,点这里adbwifi.sh

下面是shell内容:

#!/bin/bash   #Modify this with your IP rangeMY_IP_RANGE="192\.168\.1"#You usually wouldn't have to modify thisPORT_BASE=5555#List the devices on the screen for your viewing pleasureadb devicesecho#Find USB devices only (no emulators, genymotion or connected devicesdeclare -a deviceArray=(`adb devices -l | grep -v emulator | grep -v vbox | grep -v "${MY_IP_RANGE}" | grep " device " | awk '{print $1}'`)  echo "found ${#deviceArray[@]} device(s)"echofor index in ${!deviceArray[*]}doecho "finding IP address for device ${deviceArray[index]}"IP_ADDRESS=$(adb -s ${deviceArray[index]} shell ifconfig wlan0 | awk '{print $3}')echo "IP address found : $IP_ADDRESS "echo "Connecting..."adb -s ${deviceArray[index]} tcpip $(($PORT_BASE + $index))adb -s ${deviceArray[index]} connect "$IP_ADDRESS:$(($PORT_BASE + $index))"echoechodoneadb devices -l#exit
以下是windows版本的批处理,也可以实现了。 :neutral: 

@echo off &setlocal enabledelayedexpansion
::set your port
set PORT_BASE=5555
::list the device
adb devices |findstr /i "\<device\>" >nul
if "%errorlevel%" neq "0" (
echo "device not found."
goto :eof
)
::set devices serial
for /f "tokens=1" %%i in ('adb devices^|findstr "\<device\>"') do (
set device_serial=%%i
echo
)
echo found devices %device_serial%

::find IP for the phone
for /f "tokens=3 delims= " %%i in ('adb shell ifconfig wlan0') do (
set phone_ip=%%i
)
echo device ip is %phone_ip%

echo "Connecting......"
adb -s %device_serial% tcpip %PORT_BASE%
adb -s %device_serial% connect %phone_ip%:%PORT_BASE%

echo Done
adb devices -l
ping -n 4 127.0.0.1 >&2 >nul

还有另外一种完全可以摆脱数据线的方法,还解决了小米的问题,那就是使用WiFi ADB。
WiFi ADB 是一个通过无线网络来使电脑和手机连接的手机App(可以去Google Play 搜索类似的),当我们做测试的时候,只需在手机上打开,电脑只需在命令行输入adb connect xxx.xxx.xxx.xxx:5555,电脑可以连接手机,就可以通过无线网络来调试开发的应用。
下载地址:http://download.csdn.net/detail/u013541140/9489574
    0 0
    原创粉丝点击