Click or drag to resize

SettingsProvider Interface

Interface that provides access to device related settings.

Namespace:  Zebra.Sdk.Settings
Assembly:  SdkApi.Core (in SdkApi.Core.dll) Version: 2.15.2634
Syntax
public interface SettingsProvider

The SettingsProvider type exposes the following members.

Methods
  NameDescription
Public methodGetAllSettings
Retrieve all settings and their attributes.
Public methodGetAllSettingValues
Retrieves all of the device's setting values.
Public methodGetAvailableSettings
Retrieve all of the setting identifiers for a device.
Public methodGetSettingRange
Retrieves the allowable range for a setting.
Public methodGetSettingsValues
Retrieves the device's setting values for a list of setting IDs.
Public methodGetSettingType
Returns the data type of the setting.
Public methodGetSettingValue
Retrieves the device's setting value for a setting id.
Public methodIsSettingReadOnly
Returns true if the setting is read only.
Public methodIsSettingValid
Returns true if value is valid for the given setting.
Public methodIsSettingWriteOnly
Returns true if the setting is write only.
Public methodProcessSettingsViaMap
Change or retrieve printer settings.
Public methodSetSetting
Sets the setting to the given value.
Public methodSetSettings
Set more than one setting.
Top
Examples
Desktop
using System;
using System.Collections.Generic;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Printer;
using Zebra.Sdk.Printer.Discovery;
using Zebra.Sdk.Settings;

public class SettingsProviderExample {

    public static void Main(string[] args) {
        try {
            foreach (DiscoveredUsbPrinter usbPrinter in UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter())) {
                Connection c = usbPrinter.GetConnection();
                c.Open();
                if (c.Connected) {
                    DisplaySettings(c);
                }
                c.Close();
            }
        } catch (SettingsException e) {
            Console.WriteLine(e.ToString());
        } catch (ConnectionException e) {
            Console.WriteLine(e.ToString());
        } catch (ZebraPrinterLanguageUnknownException e) {
            Console.WriteLine(e.ToString());
        } catch (ZebraIllegalArgumentException e) {
            Console.WriteLine(e.ToString());
        }
    }

    private static void DisplaySettings(Connection c) {
        ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(c);
        ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);

        if (linkOsPrinter != null) {
            Console.WriteLine("Available Settings for myDevice");
            HashSet<string> availableSettings = linkOsPrinter.GetAvailableSettings();
            foreach (string setting in availableSettings) {
                Console.WriteLine($"{setting}: Range = ({linkOsPrinter.GetSettingRange(setting)})");
            }

            Console.WriteLine("\nCurrent Setting Values for myDevice");
            Dictionary<string, string> allSettingValues = linkOsPrinter.GetAllSettingValues();
            foreach (string settingName in allSettingValues.Keys) {
                Console.WriteLine($"{settingName}:{allSettingValues[settingName]}");
            }

            string darknessSettingId = "print.tone";
            string newDarknessValue = "10.0";
            if (availableSettings.Contains(darknessSettingId) &&
                linkOsPrinter.IsSettingValid(darknessSettingId, newDarknessValue) &&
                linkOsPrinter.IsSettingReadOnly(darknessSettingId) == false) {
                linkOsPrinter.SetSetting(darknessSettingId, newDarknessValue);
            }

            Console.WriteLine($"\nNew {darknessSettingId} Value = {linkOsPrinter.GetSettingValue(darknessSettingId)}");
        }
    }
}
Examples
Android™
using Android.App;
using Android.Content;
using Android.Hardware.Usb;
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.Comm;
using Zebra.Sdk.Printer;
using Zebra.Sdk.Printer.Discovery;

public class SettingsProviderExample : Activity {

    private const int USB_PERMISSION_TIMEOUT = 30000;
    private const string ACTION_USB_PERMISSION = "com.settingsprovider.example.ACTION_USB_PERMISSION";

    private readonly IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    private UsbReceiver usbReceiver;

    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 Settings Util Example",
            LayoutParameters = new ViewGroup.LayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent))
        };
        layout.AddView(testButton);

        SetContentView(layout);
        usbReceiver = new UsbReceiver();

        testButton.Click += async (sender, e) => {
            try {
                UsbDiscoveryHandler discoveryHandler = new UsbDiscoveryHandler();
                UsbDiscoverer.FindPrinters(ApplicationContext, discoveryHandler);

                while (!discoveryHandler.DiscoveryComplete) {
                    Thread.Sleep(10);
                }

                foreach (DiscoveredPrinter printer in discoveryHandler.DiscoveredPrinters) {
                    await RequestUsbPermission(printer);

                    Connection connection = printer.GetConnection();
                    try {
                        connection.Open();
                        if (connection.Connected) {
                            DisplaySettings(connection);
                        }
                    } finally {
                        connection.Close();
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        };
    }

    private void DisplaySettings(Connection connection) {
        ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(connection);
        ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);

        if (linkOsPrinter != null) {
            Console.WriteLine("Available Settings for myDevice");
            HashSet<string> availableSettings = linkOsPrinter.GetAvailableSettings();
            foreach (string setting in availableSettings) {
                Console.WriteLine($"{setting}: Range = ({linkOsPrinter.GetSettingRange(setting)})");
            }

            Console.WriteLine("\nCurrent Setting Values for myDevice");
            Dictionary<string, string> allSettingValues = linkOsPrinter.GetAllSettingValues();
            foreach (string settingName in allSettingValues.Keys) {
                Console.WriteLine($"{settingName}:{allSettingValues[settingName]}");
            }

            string darknessSettingId = "print.tone";
            string newDarknessValue = "10.0";
            if (availableSettings.Contains(darknessSettingId) &&
                linkOsPrinter.IsSettingValid(darknessSettingId, newDarknessValue) &&
                linkOsPrinter.IsSettingReadOnly(darknessSettingId) == false) {
                linkOsPrinter.SetSetting(darknessSettingId, newDarknessValue);
            }

            Console.WriteLine($"\nNew {darknessSettingId} Value = {linkOsPrinter.GetSettingValue(darknessSettingId)}");
        }
    }

    protected override void OnPause() {
        UnregisterReceiver(usbReceiver);
        base.OnPause();
    }

    protected override void OnResume() {
        base.OnResume();
        RegisterReceiver(usbReceiver, filter);
    }

    /// <exception cref="ConnectionException">If USB permission is not granted.</exception>
    private async Task RequestUsbPermission(DiscoveredPrinter printer) {
        UsbManager usbManager = (UsbManager)GetSystemService(UsbService);
        UsbDevice usbDevice = ((DiscoveredPrinterUsb)printer).Device;
        if (!usbManager.HasPermission(usbDevice)) {
            PendingIntent permissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            usbManager.RequestPermission(usbDevice, permissionIntent);

            await Task.Run(() => {
                long timeout = Math.Abs(System.Environment.TickCount) + USB_PERMISSION_TIMEOUT;
                do {
                    Thread.Sleep(10);
                    if (Math.Abs(System.Environment.TickCount) > timeout) {
                        throw new ConnectionException("Timed out waiting for Usb permission.");
                    }
                } while (usbReceiver.Result != Result.Ok);

                if (!usbReceiver.HasPermission) {
                    throw new ConnectionException("Usb permission denied.");
                }
            });
        }
    }

    private class UsbDiscoveryHandler : 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>();
    }

    private class UsbReceiver : BroadcastReceiver {

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

        public Result Result {
            get;
            private set;
        } = Result.Canceled;


        public override void OnReceive(Context context, Intent intent) {
            string action = intent.Action;
            if (ACTION_USB_PERMISSION.Equals(action)) {
                UsbDevice device = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice);
                if (intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted, false)) {
                    if (device != null) {
                        HasPermission = true;
                    }
                }
                Result = Result.Ok;
            }
        }
    }
}
Examples
iOS
using CoreGraphics;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UIKit;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;
using Zebra.Sdk.Printer.Discovery;

public class SettingsProviderExample : UIViewController {

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

        testButton.SetTitle("Run Settings Util Example", UIControlState.Normal);

        testButton.TouchUpInside += async (sender, e) => {
            await Task.Run(() => {
                try {
                    BluetoothDiscoveryHandler discoveryHandler = new BluetoothDiscoveryHandler();
                    BluetoothDiscoverer.FindPrinters(discoveryHandler);

                    while (!discoveryHandler.DiscoveryComplete) {
                        Thread.Sleep(10);
                    }

                    foreach (DiscoveredPrinter printer in discoveryHandler.DiscoveredPrinters) {
                        Connection connection = printer.GetConnection();
                        try {
                            connection.Open();
                            if (connection.Connected) {
                                DisplaySettings(connection);
                            }
                        } finally {
                            connection.Close();
                        }
                    }
                } catch (Exception ex) {
                    Console.WriteLine(ex.ToString());
                }
            });
        };

        View.AddSubview(testButton);
    }

    private void DisplaySettings(Connection connection) {
        ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(connection);
        ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);

        if (linkOsPrinter != null) {
            Console.WriteLine("Available Settings for myDevice");
            HashSet<string> availableSettings = linkOsPrinter.GetAvailableSettings();
            foreach (string setting in availableSettings) {
                Console.WriteLine($"{setting}: Range = ({linkOsPrinter.GetSettingRange(setting)})");
            }

            Console.WriteLine("\nCurrent Setting Values for myDevice");
            Dictionary<string, string> allSettingValues = linkOsPrinter.GetAllSettingValues();
            foreach (string settingName in allSettingValues.Keys) {
                Console.WriteLine($"{settingName}:{allSettingValues[settingName]}");
            }

            string darknessSettingId = "print.tone";
            string newDarknessValue = "10.0";
            if (availableSettings.Contains(darknessSettingId) &&
                linkOsPrinter.IsSettingValid(darknessSettingId, newDarknessValue) &&
                linkOsPrinter.IsSettingReadOnly(darknessSettingId) == false) {
                linkOsPrinter.SetSetting(darknessSettingId, newDarknessValue);
            }

            Console.WriteLine($"\nNew {darknessSettingId} Value = {linkOsPrinter.GetSettingValue(darknessSettingId)}");
        }
    }

    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