Click or drag to resize

UrlPrinterDiscovererFindPrinters Method

This method will search using a combination of discovery methods to find the printer described by the specified URL. (Windows 10 only)

Namespace:  Zebra.Sdk.Printer.Discovery
Assembly:  SdkApi_Desktop (in SdkApi_Desktop.dll) Version: 2.14.1989
Syntax
public static void FindPrinters(
	string url,
	DiscoveryHandler discoveryHandler
)

Parameters

url
Type: SystemString
The URL describing the targeted printer (Typically, this information is encoded on an NFC tag attached to the printer)
Example:
"http://www.zebra.com/apps/r/nfc?mBL=00225832C75F&mW=000000000000&mE=000000000000&c=QN3-AUBA0E01-00&s=XXQLJ112600422&v=0"
discoveryHandler
Type: Zebra.Sdk.Printer.DiscoveryDiscoveryHandler
A DiscoveryHandler instance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).
Exceptions
ExceptionCondition
DiscoveryExceptionIf an error occurs while starting the discovery (errors during discovery will be sent via DiscoveryError(String)).
Remarks
This method will invoke the FoundPrinter(DiscoveredPrinter) method for each interface that the specified printer is found. DiscoveryFinished will be invoked when the discovery is finished and DiscoveryError(String) will be invoked when any errors are encountered during discovery. When DiscoveryError(String) is invoked, the discovery will be canceled and DiscoveryFinished will not be invoked.

This method will typically be used when reading an NFC tag attached to a Zebra printer. To launch your app when a Zebra NFC tag is read:
  • Register to listen for Zebra NFC Tag scans in your application
  • Handle the NFC scan in your application
Examples
using System;
using System.Diagnostics;
using Windows.Networking.Proximity;
using Windows.Storage.Streams;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;
using Zebra.Sdk.Printer.Discovery;

public class UrlPrinterDiscovererExample {

    private long messageId = -1;
    private ProximityDevice proximityDevice = null;

    public void SubscribeForNfcMessage() {
        proximityDevice = ProximityDevice.GetDefault();
        if (proximityDevice != null) {
            if (messageId == -1) {  // Only subscribe once
                messageId = proximityDevice.SubscribeForMessage("WindowsUri", (device, message) => {
                    using (var reader = DataReader.FromBuffer(message.Data)) {
                        reader.UnicodeEncoding = UnicodeEncoding.Utf16LE;
                        string receivedMessage = reader.ReadString(reader.UnconsumedBufferLength / 2 - 1);

                        ProcessReceivedMessage(receivedMessage);
                    }
                });
            }
        }
    }

    public void UnsubscribeForNfcMessage() {
        if (proximityDevice != null) {
            proximityDevice.StopSubscribingForMessage(messageId);
            messageId = -1;
        }
    }

    private void ProcessReceivedMessage(string message) {
        try {
            UrlPrinterDiscoveryHandler urlDiscoHandler = new UrlPrinterDiscoveryHandler();
            UrlPrinterDiscoverer.FindPrinters(message, urlDiscoHandler);
        } catch (DiscoveryException e) {
            Debug.WriteLine(e.ToString());
        }
    }

    private class UrlPrinterDiscoveryHandler : DiscoveryHandler {

        public void DiscoveryError(string message) {
        }

        public void DiscoveryFinished() {
        }

        public void FoundPrinter(DiscoveredPrinter printer) {
            Connection connection = null;
            try {
                connection = printer.GetConnection();
                connection.Open();

                ZebraPrinter zebraPrinter = ZebraPrinterFactory.GetInstance(connection);
                if (zebraPrinter != null) {
                    zebraPrinter.PrintConfigurationLabel();
                }
            } catch (Exception e) {
                Debug.WriteLine(e.ToString());
            } finally {
                if (connection != null) {
                    try {
                        connection.Close();
                    } catch { }
                }
            }
        }
    }
}
See Also