如果在Windows中连接了一个特定接口的USB设备,如何获得通知?

1 人关注

我想知道,如果一个特定接口(USB调试)的(安卓)USB设备连接到我的Windows电脑上。

For this, I'm trying to use .Net with this code:

const string GUID_DEVINTERFACE_ANDROID = "f72fe0d4-cbcb-407d-8814-9ed673d0dd6b";
const string usbDeviceSelector = "System.Devices.InterfaceClassGuid:=\"{" + GUID_DEVINTERFACE_ANDROID + "}\" AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
_usbDeviceWatcher = DeviceInformation.CreateWatcher(
    usbDeviceSelector,
    new string[] { "System.Devices.InterfaceEnabled" },
    DeviceInformationKind.AssociationEndpoint);
_usbDeviceWatcher.Updated += UsbDeviceWatcher_Updated;

不幸的是,该事件不会被抛给我的UsbDeviceWatcher_Update函数。

我不想被告知某个特定的设备,我想被告知所有支持这个接口的设备。

如果带有这种特殊接口的设备将与我的计算机连接/断开,我怎样才能得到一个事件?

如果有一个WinUsb的解决方案,我也会很高兴。

3 个评论
你可以运行 "adb devices "命令。它将列出当前连接到计算机的所有可用的可调试的安卓设备。你可以每隔X秒左右在一个线程中运行这个命令
Codo
Have you tried _usbDeviceWatcher = DeviceInformation.CreateWatcher(UsbDevice.GetDeviceSelector(new Guid("f72fe0d4-cbcb-407d-8814-9ed673d0dd6b"))); ?
@Codo。不幸的是,这也行不通。
android
.net
windows
usb
winusb
Saabious
Saabious
发布于 2022-10-31
2 个回答
Codo
Codo
发布于 2022-11-01
已采纳
0 人赞同

筛选出具有接口类 {f72fe0d4-cbcb-407d-8814-9ed673d0dd6b} 的USB设备,然后订阅 Added Removed 事件。

using Windows.Devices.Enumeration;
using Windows.Devices.Usb;
var filter = UsbDevice.GetDeviceSelector(new Guid("f72fe0d4-cbcb-407d-8814-9ed673d0dd6b"));
var usbDeviceWatcher = DeviceInformation.CreateWatcher(filter);
usbDeviceWatcher.Added += UsbDeviceWatcher_Added;
usbDeviceWatcher.Removed += UsbDeviceWatcher_Removed;
usbDeviceWatcher.Start();
Console.WriteLine("Press key to exit...");
Console.ReadKey();
void UsbDeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
    Console.WriteLine("Added");
void UsbDeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
    Console.WriteLine("Removed");

另外,你也可以过滤接口类0xff、子类0x42和协议0x01。

var filter = UsbDevice.GetDeviceClassSelector(new UsbDeviceClass() {
    ClassCode = 0xff,
    SubclassCode = 0x42,
    ProtocolCode = 0x01