コード例 #1
0
        /// <summary>
        /// 通过adb下达获取设备型号的命令检测设备是否连接
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <bool> CheckDeviceConnection(UsbDeviceInfoEx device)
        {
            string command  = $"adb -s {device.SerialNumber} shell getprop ro.product.model";
            int    count    = 0;
            string response = String.Empty;

            while ((String.IsNullOrEmpty(response) || response.Contains("error") || response.Contains("offline")) &&
                   count <= config_inc.CMD_REPEAT_MAX_TIME)
            {
                common.m_log.Add_Debug(command);
                response = await CMD_RunAsync(command);

                common.m_log.Add_Debug(response);

                await Task.Delay(config_inc.CMD_REPEAT_WAIT_TIME);

                count++;
            }
            if (String.IsNullOrEmpty(response) || response.Contains("error") || response.Contains("offline"))
            {
                common.m_log.Add(response, LogHelper.MessageType.ERROR);
                return(false);
            }
            device.ModelName = response.Trim();
            return(true);
        }
コード例 #2
0
        public async Task <List <UsbDeviceInfoEx> > GetAllDevices()
        {
            List <UsbDeviceInfoEx> device_list = new List <UsbDeviceInfoEx>();
            UsbRegDeviceList       allDevices  = UsbDevice.AllDevices;
            await CMDHelper.Adb_KillServer();

            common.m_log.Add_Debug($"find devices {allDevices.Count}");
            for (int index = 0; index < allDevices.Count; index++)
            {
                var       device = (WinUsbRegistry)allDevices[index];
                UsbDevice usbDevice;
                bool      result = device.Open(out usbDevice);
                common.m_log.Add_Debug($"open device registry info page:{result}");
                if (result)
                {
                    string[] locationPaths = (string[])device.DeviceProperties["LocationPaths"];
                    usbDevice.Close();

                    P_ID p_id = P_ID.NULL;
                    V_ID v_id = V_ID.NULL;
                    Enum.TryParse <P_ID>(device.Pid.ToString(), out p_id);
                    Enum.TryParse <V_ID>(device.Vid.ToString(), out v_id);

                    DeviceManufactory man = new DeviceManufactory();
                    man.p_id         = p_id;
                    man.v_id         = v_id;
                    man.company_name = device.FullName;

                    string portNum = filterUsbPort(locationPaths[0], man);

                    UsbDeviceInfoEx deviceInfo = new UsbDeviceInfoEx();
                    deviceInfo.Index        = -1;
                    deviceInfo.Port_Path    = portNum;
                    deviceInfo.SerialNumber = usbDevice.Info.SerialString;
                    device_list.Add(deviceInfo);
                }
                if (usbDevice != null)
                {
                    usbDevice.Close();
                }
            }
            await Task.Delay(config_inc.CMD_REPEAT_WAIT_TIME);

            CMDHelper.Adb_StartServer();
            return(device_list);
        }
コード例 #3
0
ファイル: UsbPortTree.cs プロジェクト: radtek/MultiControl
        /// <summary>
        /// Port节点
        /// </summary>
        /// <param name="HubPath">Hub路径</param>
        /// <param name="NumberOfPorts">端口数</param>
        /// <returns>Port节点集合</returns>
        private static List <UsbPortTree> AddPortNode(String HubPath, Int32 NumberOfPorts)
        {
            // 深度遍历端口
            UsbNodeConnectionInformation[] NodeConnectionInfoCollection = USB.GetUsbNodeConnectionInformation(HubPath, NumberOfPorts);
            if (NodeConnectionInfoCollection != null)
            {
                List <UsbPortTree> PortNodeCollection = new List <UsbPortTree>(NumberOfPorts);
                foreach (UsbNodeConnectionInformation NodeConnectionInfo in NodeConnectionInfoCollection)
                {   // 增加端口节点
                    UsbPortTree PortNode = new UsbPortTree();

                    //PortNode.Icon = ImageDevice;
                    PortNode.Name     = "[Port" + NodeConnectionInfo.ConnectionIndex + "]" + NodeConnectionInfo.ConnectionStatus;
                    PortNode.Data     = NodeConnectionInfo;
                    PortNode.Children = null;
                    if (NodeConnectionInfo.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected)
                    {
                        // 设备连接
                        ConnectedDevicesCnt++; // 连接的USB设备数目
                        if (!String.IsNullOrEmpty(NodeConnectionInfo.DeviceDescriptor.Product))
                        {                      // 产品名称
                            PortNode.Name = String.Concat(PortNode.Name, ": ", NodeConnectionInfo.DeviceDescriptor.Product);
                            common.m_log.Add($"{PortNode.Name}");
                            common.m_log.Add($"P_ID:{NodeConnectionInfo.DeviceDescriptor.idProduct};V_ID:{NodeConnectionInfo.DeviceDescriptor.idVendor};Product:{NodeConnectionInfo.DeviceDescriptor.Product}");
                            if (NodeConnectionInfo.DeviceDescriptor.Product.ToUpper().Contains("ANDROID"))
                            {
                                UsbDeviceInfoEx device = new UsbDeviceInfoEx();
                                device.Port_Path    = NodeConnectionInfo.DevicePath;
                                device.Port_Index   = NodeConnectionInfo.ConnectionIndex;
                                device.Product      = NodeConnectionInfo.DeviceDescriptor.Product;
                                device.SerialNumber = NodeConnectionInfo.DeviceDescriptor.SerialNumber;
                                device.idProduct    = NodeConnectionInfo.DeviceDescriptor.idProduct;
                                device.idVender     = NodeConnectionInfo.DeviceDescriptor.idVendor;
                                if (!_connectedAndroidDevices.Contains(device, UsbDeviceInfoEx.Default))
                                {
                                    _connectedAndroidDevices.Add(device);
                                }
                            }
                        }

                        if (NodeConnectionInfo.DeviceIsHub)
                        {
                            // 获取外部Hub设备路径
                            String ExternalHubPath = USB.GetExternalHubPath(NodeConnectionInfo.DevicePath, NodeConnectionInfo.ConnectionIndex);
                            UsbNodeInformation[] NodeInfoCollection = USB.GetUsbNodeInformation(HubPath);
                            if (NodeInfoCollection != null)
                            {
                                PortNode.Data = new ExternalHubInfo {
                                    NodeInfo = NodeInfoCollection[0], NodeConnectionInfo = NodeConnectionInfo
                                };
                                if (NodeInfoCollection[0].NodeType == USB_HUB_NODE.UsbHub)
                                {
                                    PortNode.Children = AddPortNode(ExternalHubPath, NodeInfoCollection[0].NumberOfPorts);
                                }

                                if (String.IsNullOrEmpty(NodeConnectionInfo.DeviceDescriptor.Product))
                                {
                                    if (!String.IsNullOrEmpty(NodeInfoCollection[0].Name))
                                    {   // 产品名称
                                        PortNode.Name = String.Concat(PortNode.Name, ": ", NodeInfoCollection[0].Name);
                                    }
                                }
                            }

                            ConnectedHubs++;    // 连接的外部Hub数目
                        }
                    }

                    PortNodeCollection.Add(PortNode);
                }

                return(PortNodeCollection);
            }

            return(null);
        }