如何在C#中仅查找打印机蓝牙设备?

如何在C#中仅查找打印机蓝牙设备?,c#,bluetooth,uwp,C#,Bluetooth,Uwp,我正在开发一个应用程序,实现蓝牙打印。作为此过程的一部分,我希望通过仅键入打印机设备来限制查找到的设备。目前,我用于查找蓝牙设备的代码如下: public List<ScannerInfo> GetPrinters() { // Declare results List<ScannerInfo> result = new List<ScannerInfo>(); // Get all the blue

我正在开发一个应用程序,实现蓝牙打印。作为此过程的一部分,我希望通过仅键入打印机设备来限制查找到的设备。目前,我用于查找蓝牙设备的代码如下:

 public List<ScannerInfo> GetPrinters()
    {
        // Declare results
        List<ScannerInfo> result = new List<ScannerInfo>();

        // Get all the bluetooth and bluetooth serial devices
        DeviceInformationCollection pairedBluetoothDevices = Task.Run(async () =>
            await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector())).Result;
        DeviceInformationCollection pairedBluetoothSerialDevices = Task.Run(async () =>
                await DeviceInformation.FindAllAsync(
                    RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)))
            .Result;

        // Get scanner data
        foreach (DeviceInformation pairedBluetoothSerialDevice in pairedBluetoothSerialDevices)
        {
            var d = DeviceInformation.CreateFromIdAsync(pairedBluetoothSerialDevice.Id);
            // Create object
            ScannerInfo newScanner = new ScannerInfo
            {
                Id = pairedBluetoothSerialDevice.Id,
                Name = pairedBluetoothSerialDevice.Name
            };

            // Correct name (this is necessary as following the anniversary update the serial object name no longer holds the bluetooth device name, only the prototcol name.
            // Therefore we attempt to get this by matching id components via the full bluetooth device list, which is what the user sees in windows settings).
            foreach (var pairedBluetoothDevice in pairedBluetoothDevices)
            {
                if (pairedBluetoothSerialDevice.Id.Contains(pairedBluetoothDevice.Id))
                {
                    newScanner.Name = pairedBluetoothDevice.Name;
                    break;
                }
            }

            // Add to result set
            result.Add(newScanner);
        }

        // Return items
        return result;
    }
公共列表GetPrinters()
{
//宣布结果
列表结果=新列表();
//获取所有蓝牙和蓝牙串行设备
DeviceInformation Collection pairedBluetoothDevices=Task.Run(异步()=>
等待设备信息.findalsync(BluetoothDevice.GetDeviceSelector()).Result;
DeviceInformation Collection pairedBluetoothSerialDevices=Task.Run(异步()=>
等待设备信息。FindAllAsync(
RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)))
.结果;
//获取扫描仪数据
foreach(pairedBluetoothSerialDevices中的设备信息pairedBluetoothSerialDevice)
{
var d=设备信息.CreateFromIdAsync(pairedBluetothSerialDevice.Id);
//创建对象
ScannerInfo newScanner=新的ScannerInfo
{
Id=pairedBluetoothSerialDevice.Id,
Name=pairedBluetoothSerialDevice.Name
};
//正确的名称(这是必要的,因为在周年纪念更新之后,串行对象名称不再包含蓝牙设备名称,而仅包含协议名称。
//因此,我们试图通过完整的蓝牙设备列表(用户在windows设置中看到的)匹配id组件来实现这一点。
foreach(pairedBluetoothDevices中的var pairedBluetoothDevice)
{
if(PairedBluetothSerialDevice.Id.Contains(PairedBluetothDevice.Id))
{
newScanner.Name=pairedBluetoothDevice.Name;
打破
}
}
//添加到结果集
结果。添加(新闻扫描程序);
}
//退货项目
返回结果;
}

您可以尝试使用高级查询语法(AQS)筛选器作为
设备信息的选择器。FindAllAsync
方法来筛选打印机设备

在本文档中,它使用
{0ECEF634-6EF0-472A-8085-5AD023ECBCCD}
作为PrinterInterfaceClass GUID,您可以尝试


我们也感谢您的详细信息。

谢谢您的建议!我会看一看,非常感谢!