Click or drag to resize

BluetoothDiscoverer Class

A class that discovers Bluetooth® devices.
Inheritance Hierarchy
SystemObject
  Zebra.Sdk.Printer.DiscoveryBluetoothDiscoverer

Namespace:  Zebra.Sdk.Printer.Discovery
Assemblies:   SdkApi.Desktop (in SdkApi.Desktop.dll) Version: 2.15.2634
  ZebraPrinterSdk (in ZebraPrinterSdk.dll) Version: 2.15.2521
  ZebraPrinterSdk (in ZebraPrinterSdk.dll) Version: 2.15.2521
Syntax
public class BluetoothDiscoverer

The BluetoothDiscoverer type exposes the following members.

Constructors
  NameDescription
Public methodBluetoothDiscoverer
Initializes a new instance of the BluetoothDiscoverer class
Top
Methods
  NameDescription
Public methodDispose
Releases all resources used by the BluetoothDiscoverer
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodStatic memberFindPrinters(DiscoveryHandler)
Find Bluetooth® devices that are discoverable.
Public methodStatic memberFindPrinters(Context, DiscoveryHandler)
Find Bluetooth® devices that are discoverable.
Public methodStatic memberFindPrinters(DiscoveryHandler, Int32)
Find Bluetooth® devices that are discoverable.
Public methodStatic memberFindPrinters(DiscoveryHandler, DeviceFilter)
Find Bluetooth® devices that are discoverable.
Public methodStatic memberFindPrinters(Context, DiscoveryHandler, DeviceFilter)
Find Bluetooth® devices that are discoverable.
Public methodStatic memberFindPrinters(DiscoveryHandler, DeviceFilter, Int32)
Find Bluetooth® devices that are discoverable.
Public methodStatic memberFindServices(String, ServiceDiscoveryHandler)
Find services (ConnectionChannels) that are supported by the device at macAddress.
Public methodStatic memberFindServices(Context, String, ServiceDiscoveryHandler)
Find services(ConnectionChannels) that are supported by the device at macAddress.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Examples
Desktop
using System;
using System.Collections.Generic;
using Zebra.Sdk.Printer.Discovery;

class BluetoothDiscovererExample {

    public static void Main(string[] args) {
        try {
            Console.WriteLine("Starting printer discovery.");

            BluetoothDiscoveryHandler discoveryHandler = new BluetoothDiscoveryHandler();
            BluetoothDiscoverer.FindPrinters(discoveryHandler);
            while (!discoveryHandler.DiscoveryComplete) {
                System.Threading.Thread.Sleep(10);
            }
        } catch (DiscoveryException e) {
            Console.WriteLine(e.ToString());
        }
    }

    private class BluetoothDiscoveryHandler : DiscoveryHandler {

        private bool discoveryComplete = false;
        List<DiscoveredPrinter> printers = new List<DiscoveredPrinter>();

        public void DiscoveryError(string message) {
            Console.WriteLine($"An error occurred during discovery: {message}.");
            discoveryComplete = true;
        }

        public void DiscoveryFinished() {
            foreach (DiscoveredPrinter printer in printers) {
                Console.WriteLine(printer);
            }
            Console.WriteLine($"Discovered {printers.Count} Bluetooth printers.");
            discoveryComplete = true;
        }

        public void FoundPrinter(DiscoveredPrinter printer) {
            printers.Add(printer);
        }

        public bool DiscoveryComplete {
            get => discoveryComplete;
        }
    }
}
Examples
Android
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Zebra.Sdk.Printer.Discovery;

public class BluetoothDiscovererExample : Activity {

    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);

        LinearLayout layout = (LinearLayout)View.Inflate(this, Android.Resource.Layout.ActivityListItem, null);
        layout.Orientation = Orientation.Vertical;

        Button testButton = new Button(this) {
            Text = "Run Bluetooth Discovery Example",
            LayoutParameters = new ViewGroup.LayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent))
        };
        layout.AddView(testButton);

        SetContentView(layout);

        testButton.Click += async (sender, e) => {
            await Task.Run(() => {
                BluetoothDiscoveryHandler discoveryHandler = new BluetoothDiscoveryHandler();
                BluetoothDiscoverer.FindPrinters(ApplicationContext, discoveryHandler);
                while (!discoveryHandler.DiscoveryComplete) {
                    Thread.Sleep(10);
                }

                foreach (DiscoveredPrinter printer in discoveryHandler.DiscoveredPrinters) {
                    Console.WriteLine(printer);
                }
                Console.WriteLine($"Discovered {discoveryHandler.DiscoveredPrinters.Count} Link-OS™ printers.");
            });
        };
    }

    private class BluetoothDiscoveryHandler : DiscoveryHandler {

        public void DiscoveryError(string message) {
            Console.WriteLine($"An error occurred during discovery: {message}.");
            DiscoveryComplete = true;
        }

        public void DiscoveryFinished() {
            DiscoveryComplete = true;
        }

        public void FoundPrinter(DiscoveredPrinter printer) {
            DiscoveredPrinters.Add(printer);
        }

        public bool DiscoveryComplete { get; private set; } = false;

        public List<DiscoveredPrinter> DiscoveredPrinters { get; } = new List<DiscoveredPrinter>();
    }
}
Examples
iOS
using CoreGraphics;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UIKit;
using Zebra.Sdk.Printer.Discovery;

public class BluetoothDiscovererExample : UIViewController {

    public BluetoothDiscovererExample(IntPtr handle) : base(handle) {
        UIButton testButton = new UIButton(UIButtonType.System) {
            Frame = new CGRect(25, 25, 300, 150)
        };

        testButton.SetTitle("Run Bluetooth Discovery Example", UIControlState.Normal);

        testButton.TouchUpInside += async (sender, e) => {
            await Task.Run(() => {
                BluetoothDiscoveryHandler discoveryHandler = new BluetoothDiscoveryHandler();
                BluetoothDiscoverer.FindPrinters(discoveryHandler);
                while (!discoveryHandler.DiscoveryComplete) {
                    Thread.Sleep(10);
                }

                foreach (DiscoveredPrinter printer in discoveryHandler.DiscoveredPrinters) {
                    Console.WriteLine(printer);
                }
                Console.WriteLine($"Discovered {discoveryHandler.DiscoveredPrinters.Count} Link-OS™ printers.");
            });
        };

        View.AddSubview(testButton);
    }

    private class BluetoothDiscoveryHandler : DiscoveryHandler {

        public void DiscoveryError(string message) {
            Console.WriteLine($"An error occurred during discovery: {message}.");
            DiscoveryComplete = true;
        }

        public void DiscoveryFinished() {
            DiscoveryComplete = true;
        }

        public void FoundPrinter(DiscoveredPrinter printer) {
            DiscoveredPrinters.Add(printer);
        }

        public bool DiscoveryComplete { get; private set; } = false;

        public List<DiscoveredPrinter> DiscoveredPrinters { get; } = new List<DiscoveredPrinter>();
    }
}
See Also