如何获取长距离USB蓝牙适配器的VendorId/ProductId并通过C#读取数据
获取USB蓝牙适配器的VendorId和ProductId并修改C#代码
一、获取VendorId(厂商ID)和ProductId(产品ID)
你可以通过以下两种方式获取目标适配器的这两个ID:
设备管理器查看
- 将适配器插入电脑USB接口,打开「设备管理器」
- 在「蓝牙」或「通用串行总线控制器」分类下找到该适配器
- 右键点击设备 → 属性 → 切换到「详细信息」标签页
- 在属性下拉菜单中选择「硬件ID」,你会看到类似
USB\VID_1234&PID_5678的内容,其中VID_后的四位十六进制数就是VendorId,PID_后的四位是ProductId - 在C#代码中使用时,可以直接用十六进制格式(前缀加
0x),比如0x1234,或者转成十进制数值
PowerShell命令查询
打开PowerShell,执行以下命令:Get-PnpDevice -PresentOnly | Where-Object { $_.FriendlyName -match "蓝牙" } | Select-Object FriendlyName, InstanceId从输出的
InstanceId字段中提取VID_xxxx和PID_xxxx部分,格式与设备管理器中一致
二、修改你的C#代码
将获取到的VendorId和ProductId替换到代码中对应的位置即可,示例如下(假设VendorId为0x0A12,ProductId为0x0001):
using System; using System.Threading.Tasks; using Windows.Devices.Usb; class USBBluetoothReader { private UsbDevice usbDevice; public async Task InitializeAsync() { // 替换为实际获取到的VendorId和ProductId var vendorId = 0x0A12; var productId = 0x0001; // Define the USB device selector based on the vendor ID and product ID. var deviceSelector = UsbDevice.GetDeviceSelector(vendorId, productId); // Get a list of USB devices that match the selector. var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(deviceSelector); // Check if a device was found. if (devices.Count > 0) { // Open the USB device. usbDevice = await UsbDevice.FromIdAsync(devices[0].Id); if (usbDevice != null) { // You can now communicate with the USB Bluetooth reader. // Depending on the device and its protocol, you may need to use Input and Output pipes for data transfer. // Refer to the device's documentation for communication details. // For example, to read data from an Input pipe: var inputPipe = usbDevice.DefaultInterface.DefaultBulkInPipe; byte[] buffer = new byte[inputPipe.EndpointDescriptor.MaxPacketSize]; var bytesRead = await inputPipe.ReadAsync(buffer.AsBuffer()); // Process the data in the buffer. } else { // Handle device opening failure. } } else { // Handle device not found. } } }
额外提示
如果你的目标是读取蓝牙广播信号,也可以考虑使用Windows.Devices.Bluetooth.Advertisement命名空间下的API,这类API专门针对蓝牙广播扫描,可能比直接操作USB设备更简便,不过前提是适配器被Windows蓝牙栈正常识别。
内容的提问来源于stack exchange,提问作者jignesh
相关产品推荐
相关产品推荐

