首页 > 代码库 > 利用ADB实现无线连接安卓设备

利用ADB实现无线连接安卓设备

昨天用C++写了个命令行工具,我在Linux下用g++编译的。可以实现无线ADB快速连接安卓设备,不过其他平台需要重新编译源代码。

要求计算机已经把adb 添加到系统环境变量中,安卓设备获取root权限并开启无线调试,推荐使用adbWirless。

 

在shell 中输入以下命令都可以连接到 192.168.1.10 设备上,并列出当前已经连接的设备

$AndroidConnector -c 192.168.1.10$AndroidConnector 192.168.1.10
$AndroidConnector -c 1.10
$AndroidConnector 1.10

 

在shell中输入以下命令可以断开已连接的设备并列出当前已经连接的设备

$AndroidConnector -d 192.168.1.10$AndroidConnector -d 1.10

 

你可以在shell 中输入以下命令来查看帮助文档

$AndroidConnector --help

  

以下是该命令行工具的源代码,需要自行编译。

/******************* AndroidConnector *************************//*				       By Geequlim                 				   *//**************************    2014   *****************************/#include <iostream>#include <string>#include <cstdlib>#include <cstring>using namespace std;///show informations bool showInfo(const char* arg){	if(strcmp( arg,"--help" )==0)//show help	{		cout<<"Wirless adb helper \n"			"NOTIC:\n"				"     You must keep debugable for you Android device first!\n"				"     Your system should added the adb to the PATH!\n\n"			" To connect device:\n"			"-c ip 	( $AndroidConnecter -c 192.168.1.10 ) \n"					"    or the device ip  ( $AndroidConnector 192.168.1.10  )\n\n"						" To disconnect device:\n"			"-d ip 	( $AndroidConnector -d 192.168.1.10 )\n\n"							" To disconnect all devices:\n"			"-d all\n"			"-d\n\n"			"If your the ip starts with \"192.168\" : \n"					"   ($AndroidConnector -d 1.10)\n"  				" It is the same  as ($AndroidConnector -d 192.168.1.10 )\n"			<<endl;		return true;	}	else if( strcmp(arg,"--version")==0)//show version	{		cout<<"AndroidConnector  0.1.1\n"			"Copyright (C) 2014 Geequlim\t"            "Compile on "__DATE__			<<endl;		return true;	}	return false;}int main(int argc , char * argv[]){	string command="adb connect ";	string input;	string ipAdress="192.168." ;	if( argc>=2   )	{		if( argc>=3 )//more than 2 arguments		{			if (strcmp( argv[1] , "-d" ) ==0 )				command = "adb disconnect ";			//disconnect all			if (strcmp( argv[2],"all")==0)				ipAdress=" ";			else //disconnect target 				input = argv[2];		}		else//1 or 2 arguments		{			//if the second argument is not --help or --version but -d or ip adress			if(showInfo(argv[1])==false)			{				if (strcmp( argv[1] , "-d" ) ==0 )//disconnet all devices				{						command = "adb disconnect ";						ipAdress = " ";				}				else					input = argv[1];			}			else 				return 0;		}	}	else	{		cout<<"Input your phone ip adress :"<<flush;		cin>>input;	}	if( argc>1 && strcmp(argv[1],"-d") && input.size() <3 )	{		cerr<<"Worr Input!"<<endl;	return 1;	}	else if(  input.size()<8)		ipAdress+=input;	else if (  input.size()>15 || (input.size()< 11&&(input.size()>=8)) )	{		cerr<<"Worr Input!"<<endl;  return 1;	}	else		ipAdress = input;	cout<<command+ipAdress<<endl;	system( (command+ipAdress).c_str() );	system("adb devices");	return 0;}