如何在C#中只发现打印机蓝牙设备?

时间:2017-10-02 08:29:23

标签: c# bluetooth uwp

我正在开发一款实现蓝牙打印的应用程序。作为此过程的一部分,我希望按类型限制发现的设备 - 仅限打印机设备。目前,我用于发现蓝牙设备的代码如下:

 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;
    }

1 个答案:

答案 0 :(得分:1)

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

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

Build a device selector也是您的详细信息。