首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在C#中仅发现打印机蓝牙设备?

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

Stack Overflow用户
提问于 2017-10-02 16:29:23
回答 1查看 449关注 0票数 1

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

代码语言:javascript
复制
 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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-03 11:04:02

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

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

Build a device selector也是为了你的详细信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46522000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档