2017-10-02 2 views
0

Je développe une application qui implémente l'impression Bluetooth. Dans le cadre de ce processus, je souhaite limiter les périphériques détectés par des périphériques d'imprimante de type uniquement. Actuellement, le code que j'utilise pour découvrir les périphériques Bluetooth est le suivant:Comment découvrir uniquement les périphériques Bluetooth d'imprimante [C#, 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; 
    } 

J'aimerais avoir un conseil, s'il vous plaît.

Répondre

1

Vous pouvez essayer d'utiliser le filtre AQS (Advanced Query Syntax) comme sélecteur pour la méthode DeviceInformation.FindAllAsync pour filtrer les périphériques d'imprimante.

Dans ce document Quickstart: enumerating commonly used devices, il utilise {0ECEF634-6EF0-472A-8085-5AD023ECBCCD} comme GUID PrinterInterfaceClass, vous pouvez l'essayer. Le Build a device selector était aussi pour vos détails.

+0

Nous vous remercions de votre suggestion! Je vais jeter un coup d'oeil, très apprécié! –