转自:http://blog.csdn.net/linweidong/article/details/6273507

需求:

Android的apk获取手机信息,把结果发给PC client

注意地方:

1.android默认手机端的IP为“127.0.0.1”

2.要想联通PC与android手机的sokcet,一定要用adb forward 来作下端口转发才能连上socket.

3.使用socket通信,需要在mainfest.xml中添加permission: android.permission.INTERNET

Runtime.getRuntime().exec("adb forward tcp:12580 tcp:10086");
Thread.sleep(3000);

Android作为服务import java.io.BufferedInputStream;import java.io.BufferedOutputStream;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket; public class TcpConnect implements Runnable{
private final int SERVER_PORT = 10086;
private ServerSocket mServerSocket;
private Socket mClient;
private String mDeviceId;
private String mDeviceType;
public TcpConnect(String aDeviceId, String aDeviceType){
this.mDeviceId= aDeviceId;
this.mDeviceType = aDeviceType;
try {
String ip = InetAddress.getLocalHost().getHostAddress();
System.out.println("ip地址是: " + ip);
//System.out.println(aDeviceId + " 型号: " + aDeviceType);
mServerSocket = new ServerSocket(SERVER_PORT);
System.out.println("TcpConnect" + "建立Socket");
  // listen(); } catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("TcpConnect" + e.getMessage());
}
} public void listen(){
while(true){
try {
  mClient = mServerSocket.accept();
       // Log.e("TcpConnect", "在积极的监听");
  } catch (IOException e) {
  // TODO Auto-generated catch block
  //e1.printStackTrace();
  System.out.println("TcpConnect" + e.getMessage());
  }
} } @Override
public void run() {
// TODO Auto-generated method stub
  // if(mClient.isConnected()){
BufferedOutputStream out = null;
System.out.println("TcpConnect" + "开始监听");
while(true){
try{
  // Log.e("TcpConnect", "开始监听");
mClient = mServerSocket.accept();
  // if(mClient.isConnected()){
System.out.println("TcpConnect" + "检测到有连接");
out = new BufferedOutputStream(mClient.getOutputStream());
String recordStr = mDeviceId + "|" + mDeviceType;
out.write(recordStr.getBytes("utf-8"));
     // int length = recordStr.getBytes().length;
     // byte[] b = recordStr.getBytes();
     // out.writeInt(length);
     // out.write(b);
out.flush();
// Log.e("TcpConnect", recordStr);
       // out.flush();
     // }
} catch(Exception e){
      System.out.println("TcpConnect" + e.getMessage()); }finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("TcpConnect" + e.getMessage());
} }
if(mServerSocket != null){
try {
mServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("TcpConnect" + e.getMessage());
}
}
// }
} }
} public static void main(String[] args){
new Thread(new TcpConnect("2366578546946", "T959")).start();
} }

C#作为客户端,在客户端进行绑定端口:

 Process p = new Process(); //实例一个Process类,启动一个独立进程
p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; // 设置不显示窗口
p.StartInfo.ErrorDialog = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.StandardInput.WriteLine(@"adb forward tcp:12580 tcp:10086"); // Thread.Sleep(3000);
SocketClient client = new SocketClient();
MessageBox.Show("收到的数据为: " + client.listen());

C#的Socket客户端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets; namespace PreInstaller.IO
{
class SocketClient
{ public string listen()
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress myIP = IPAddress.Parse("127.0.0.1");
IPEndPoint EPhost = new IPEndPoint(myIP, int.Parse(""));
client.Connect(EPhost);
byte[] t_data = new byte[];
string data = null;
int i = ;
while ((i = client.Receive(t_data)) != )
{
data = Encoding.UTF8.GetString(t_data, , i); }
client.Close();
return data;
}
}
}

(转)C#与Android通过adb实现usb通讯的更多相关文章

  1. Android - 设置adb的usb连接配置

    设置adb的usb连接配置 本文地址: http://blog.csdn.net/caroline_wendy 把须要測试的手机连接入电脑.通过系统查找USB连接配置,找到厂商ID: 把ID加入进ad ...

  2. Android 工具-adb

    Android 工具-adb 版权声明:本文为博主原创文章,未经博主允许不得转载. Android 开发中, adb 是开发者经常使用的工具,是 Android 开发者必须掌握的. Android D ...

  3. Android 神兵利器—— Adb 常用命令

    总结的Android工具类文章: Android 神兵利器-- Adb 常用命令 Android 神兵利器-- Git 常用命令 Adb的全称为Android Debug Bridge,是管理andr ...

  4. Linux下安装Android的adb驱动-解决不能识别的问题

    Linux下安装Android的adb驱动-解决不能识别的问题 20141011更新:      老方法对我当时使用的一款设备一直都没有出现问题,最后遇到小米手机还有Android4.4版本的系统都会 ...

  5. 【Android】adb connect 手机的两种方式

    adb支持两种连接Android系统的方式,USB方式及网络方式.一般android手机及android平板默认会设置为USB方式(直接插数据线的方式). 下边介绍两种方式的切换方式. 1. 背景知识 ...

  6. Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息

    Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息   by:授客 QQ:1033553122 1.  查看内存信息 1)查看所有内存信息 命令: dumpsys mem ...

  7. Android 查看 无wifi/usb设备的logcat方法

    Android 查看 无wifi/usb设备的logcat方法 一.情况 一个定制Android设备,wifi被去掉.我须要调试一个USB设备这样也无法用usb来输出logcat. 由于这个USB设备 ...

  8. [RN] Android 设备adb连接后unauthorized解决方法

    Android 设备adb连接后unauthorized解决方法 安卓设备usb或者adbwireless连接后输入adb device后都是未授权状态 相信很多同学都会遇到这种情况,除了一直重复开关 ...

  9. [原创]Android 常用adb命令总结

    [原创]Android 常用adb命令总结 1 adb介绍 1.1 adb官方网站及下载 官方网站下载安装:http://adbshell.com/downloads 1.2 adb安装是否成功检查? ...

随机推荐

  1. JavaScript之对数组中元素进行增删改

    JavaScript中数据类型无非是:简单类型+复杂类型,什么是简单什么又是复杂.从电脑物理存储上讲,简单就是所见即所得,你看见什么,电脑里面存的就只是什么,并不会因为一些关系(比如引用)而多出一部分 ...

  2. javaScript入门1--组成,基本使用

    javaScript:脚本语言,最初是为了实现在客户端进行的表单验证以减少服务器的数据交互次数,如今可用在页面交互,计算,拥有了闭包,匿名(拉姆达)函数,甚至元编程等特性. javaScript实现的 ...

  3. HTML5 indexedDB数据库的入门学习(二)

    上一篇关于indexedDB的学习笔记主要写了indexedDB数据库的基本操作—增删改查:但是为什么我们要用indexedDB呢?为什么indexedDB受到了开发者们的青睐呢?最主要的就是inde ...

  4. 关于搭建haddoop分布式系统的全部过程复习

    1\在vmware中更改了虚拟机的网络类型,--->NAT方式,(虚拟交换机的ip可以从vmvare的edit-->vertual network editor看到)2.根据这个交换机(网 ...

  5. 【数值方法,水题】UVa 10341 - Solve It

    题目链接 题意: 解方程:p ∗ e^(−x) + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x^2 + u = 0 (0 <= x <= 1) ...

  6. 转: Lua 语言 15 分钟快速入门

    看点: 1. 以很特殊的方式工,把Lua的语法全部输出一段,很容易让人记住..不错 转: http://blog.jobbole.com/70480/

  7. Spring(3.2.3) - Beans(1): Spring 容器

    BeanFactory & ApplicationContext org.springframework.beans.factory.BeanFactory 是最基本的 Spring 容器接口 ...

  8. MyBatis(3.2.3) - Configuring MyBatis using XML, Properties

    The properties configuration element can be used to externalize the configuration values into a prop ...

  9. Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms

    About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evi ...

  10. jQuery之对话框

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...