Widnows 作为 Android USB 附件的实现

原理:通过 WinUSB 发送 adk 协议,将 Android USB 设置为附件模式后进行数据通信

主要用作在硬件实现前Android软件的调试工作


1. 下载 http://www.lvr.com/files/winusb_cs_20.zip ,该软件实现了winusb 控制

2. WinUsbDevice.cs 增加方法,并在主界面添加调用按钮


internal  Boolean DoControlTransfer(IntPtr winUsbHandle)
         {
             UInt32 bytesReturned = 0;
             Byte[] dataStage =  new  Byte[2];
             try
             {
                 // 发送51
                 NativeMethods.WINUSB_SETUP_PACKET setupPacket;
                 setupPacket.RequestType = 0XC0;                
                 setupPacket.Request = 51;
                 setupPacket.Index = 0;
                 setupPacket.Length = Convert.ToUInt16(dataStage.Length);
                 setupPacket.Value = 0;           
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 String str =  "sundyn" ;
     
                 dataStage = System.Text.Encoding.Default.GetBytes(str);
                     
                 setupPacket.RequestType = 0X40;
                 setupPacket.Request = 52;
                 setupPacket.Index = 0;
                 setupPacket.Length = Convert.ToUInt16(dataStage.Length);
                 setupPacket.Value = 0;           
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 setupPacket.Index = 1;
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 setupPacket.Index = 2;
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 setupPacket.Index = 3;
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 setupPacket.Index = 4;
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 setupPacket.Index = 5;
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 setupPacket.RequestType = 0X40;
                 setupPacket.Request = 53;
                 setupPacket.Index = 0;
                 setupPacket.Length = 0;
                 setupPacket.Value = 0; 
                 if (!(NativeMethods.WinUsb_ControlTransfer(winUsbHandle, setupPacket, dataStage, Convert.ToUInt16(dataStage.Length),  ref  bytesReturned, IntPtr.Zero)))
                     return  false ;
     
                 return  true ;
     
             }
             catch  (AccessViolationException ex)
             {
                 // The handle might be closed.
     
                 return  false ;
             }
         }

3.修改Winusb 驱动 INF  文件适应Android设备,主要是GUID和PID、VID

4.Android USB 口强制安装修改过的驱动


OK ... 通信成功


你可能感兴趣的:(Android,C#)