Click or drag to resize

BluetoothConnection Class

Establishes a Bluetooth® connection to a printer
Inheritance Hierarchy

Namespace:  Zebra.Sdk.Comm
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 BluetoothConnection : ConnectionA

The BluetoothConnection type exposes the following members.

Constructors
  NameDescription
Public methodBluetoothConnection(String)
Constructs a new Bluetooth® connection with the given macAddress.
Public methodBluetoothConnection(String, Int32, Int32)
Constructs a new Bluetooth® connection with the givenmacAddress and timeout values.
Top
Properties
  NameDescription
Public propertyConnected
Returns true if the connection is open.
(Inherited from ConnectionA.)
Public propertyFriendlyName
Gets the friendly name of the Bluetooth® connection. The friendly name is obtained from the device when this connection is opened.If the friendly name changes on the device, it will not be refreshed until the connection is closed and reopened.
Public propertyMACAddress
Gets the MAC address which was passed into the constructor. The MAC address is a hexadecimal string with separators between the octets. (e.g. 00:11:BB:DD:55:FF).
Public propertyManufacturer
See the classes which implement this property for the format of the printer manufacturer string.
(Inherited from ConnectionA.)
Public propertyMaxDataToWrite
Gets or sets the maximum number of bytes to write at one time
(Inherited from ConnectionA.)
Public propertyMaxTimeoutForRead
Gets or sets the maximum time, in milliseconds, to wait for any data to be received.
(Inherited from ConnectionA.)
Public propertyMultipartTimeToWaitAfterSend
(Inherited from ConnectionA.)
Public propertySimpleConnectionName
Gets the MAC address and the friendly name as the description.
(Overrides ConnectionASimpleConnectionName.)
Public propertyTimeToWaitBeforeClose
Gets/sets the time to wait before closing the connection (milliseconds).
Public propertyTimeToWaitForMoreData
Gets or sets the maximum time, in milliseconds, to wait in-between reads after the initial read.
(Inherited from ConnectionA.)
Top
Methods
  NameDescription
Public methodAddWriteLogStream
Sets the stream to log the write data to.
(Inherited from ConnectionA.)
Public methodBytesAvailable
Returns an estimate of the number of bytes that can be read from this connection without blocking.
(Overrides ConnectionABytesAvailable.)
Public methodClose
Closes the Bluetooth® connection.
(Overrides ConnectionAClose.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodGetConnectionReestablisher
Returns a ConnectionReestablisher which allows for easy recreation of a connection which may have been closed.
(Overrides ConnectionAGetConnectionReestablisher(Int64).)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodOpen
Opens a Bluetooth® connection as specified in the constructor.
(Overrides ConnectionAOpen.)
Public methodOpen
Opens the connection to a device.
(Inherited from ConnectionA.)
Public methodRead
Reads all the available data from the connection. This call is non-blocking.
(Inherited from ConnectionA.)
Public methodRead(Int32)
Reads maxBytesToRead of the available data from the connection.
(Overrides ConnectionARead(Int32).)
Public methodRead(Int32)
Reads maxBytesToRead of the available data from the connection.
(Inherited from ConnectionA.)
Public methodRead(BinaryWriter)
Reads all the available data from the connection.
(Inherited from ConnectionA.)
Public methodRead(Int32, Boolean)
Reads maxBytesToRead of the available data from the connection.
(Inherited from ConnectionA.)
Public methodReadChar
Reads the next byte of data from the connection.
(Overrides ConnectionAReadChar.)
Public methodReadChar
Reads the next byte of data from the connection.
(Inherited from ConnectionA.)
Public methodSendAndWaitForResponse(Byte, Int32, Int32, String)
Sends dataToSend and returns the response data.
(Inherited from ConnectionA.)
Public methodSendAndWaitForResponse(BinaryWriter, BinaryReader, Int32, Int32, String)
Sends data from sourceStream and writes the response data to destinationStream.
(Inherited from ConnectionA.)
Public methodSendAndWaitForValidResponse(Byte, Int32, Int32, ResponseValidator)
Sends dataToSend and returns the response data.
(Inherited from ConnectionA.)
Public methodSendAndWaitForValidResponse(BinaryWriter, BinaryReader, Int32, Int32, ResponseValidator)
Sends data from sourceStream and writes the response data to destinationStream.
(Inherited from ConnectionA.)
Public methodSetReadTimeout
Sets the underlying read timeout value.
(Inherited from ConnectionA.)
Public methodToString
The friendly name is obtained from the device when this connection is opened. If the friendly name changes on the device, it will not be refreshed until the connection is closed and reopened.
(Overrides ConnectionAToString.)
Public methodWaitForData
Causes the currently executing thread to sleep until BytesAvailable > 0, or for a maximum of maxTimeout milliseconds.
(Inherited from ConnectionA.)
Public methodWrite(Byte)
Writes data.Length bytes from the specified byte array to this output stream.
(Inherited from ConnectionA.)
Public methodWrite(BinaryReader)
Writes all available bytes from the data source to this output stream.
(Inherited from ConnectionA.)
Public methodWrite(Byte, Int32, Int32)
Writes length bytes from data starting at offset.
(Inherited from ConnectionA.)
Top
Remarks
In order to connect to a device with Bluetooth®, the device must be discoverable, authentication must be enabled, and the authentication pin must be set (1-16 alphanumeric characters).
The encryption type and whether or not it is used is determined by the device initiating the connection and not device being connected to.
Examples
Desktop
using System;
using System.Text;
using Zebra.Sdk.Comm;

public class BluetoothConnectionExample {

    public static void Main(string[] Args) {
        BluetoothConnectionExample example = new BluetoothConnectionExample();

        string theBtMacAddress = "00:11:BB:DD:55:FF";
        example.SendZplOverBluetooth(theBtMacAddress);
        example.SendCpclOverBluetooth(theBtMacAddress);
    }

    private void SendZplOverBluetooth(string theBtMacAddress) {
        Connection thePrinterConn = null;
        try {
            // Instantiate connection for given Bluetooth® MAC Address.
            thePrinterConn = new BluetoothConnection(theBtMacAddress);

            // Open the connection - physical connection is established here.
            thePrinterConn.Open();

            // This example prints "This is a ZPL test." near the top of the label.
            string zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";

            // Send the data to printer as a byte array.
            thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));
        } catch (Exception e) {
            // Handle communications error here.
            Console.WriteLine(e.ToString());
        } finally {
            // Close the connection to release resources.
            if (thePrinterConn != null) {
                thePrinterConn.Close();
            }
        }
    }

    private void SendCpclOverBluetooth(string theBtMacAddress) {
        Connection thePrinterConn = null;
        try {
            // Instantiate connection for given Bluetooth® MAC Address.
            thePrinterConn = new BluetoothConnection(theBtMacAddress);

            // Open the connection - physical connection is established here.
            thePrinterConn.Open();

            // This example prints "This is a CPCL test." near the top of the label.
            string cpclData = "! 0 200 200 210 1\r\n"
                + "TEXT 4 0 30 40 This is a CPCL test.\r\n"
                + "FORM\r\n"
                + "PRINT\r\n";

            // Send the data to printer as a byte array.
            thePrinterConn.Write(Encoding.UTF8.GetBytes(cpclData));
        } catch (Exception e) {
            // Handle communications error here.
            Console.WriteLine(e.ToString());
        } finally {
            // Close the connection to release resources.
            if (thePrinterConn != null) {
                thePrinterConn.Close();
            }
        }
    }
}
Examples
Android™
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.Text;
using System.Threading.Tasks;
using Zebra.Sdk.Comm;

public class BluetoothConnectionExample : 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 buttonPrint = new Button(this) {
            Text = "Run Bluetooth Connection Example",
            LayoutParameters = new ViewGroup.LayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent))
        };
        layout.AddView(buttonPrint);

        SetContentView(layout);

        buttonPrint.Click += async (sender, e) => {
            string theBtMacAddress = "00:11:BB:DD:55:FF";

            await Task.Run(() => {
                SendZplOverBluetooth(theBtMacAddress);
            });

            await Task.Run(() => {
                SendCpclOverBluetooth(theBtMacAddress);
            });
        };
    }

    private void SendZplOverBluetooth(string theBtMacAddress) {
        Connection thePrinterConn = null;
        try {
            // Instantiate connection for given Bluetooth® MAC Address.
            thePrinterConn = new BluetoothConnection(theBtMacAddress);

            // Open the connection - physical connection is established here.
            thePrinterConn.Open();

            // This example prints "This is a ZPL test." near the top of the label.
            string zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";

            // Send the data to printer as a byte array.
            thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));
        } catch (Exception e) {
            // Handle communications error here.
            Console.WriteLine(e.ToString());
        } finally {
            // Close the connection to release resources.
            if (thePrinterConn != null) {
                thePrinterConn.Close();
            }
        }
    }

    private void SendCpclOverBluetooth(string theBtMacAddress) {
        Connection thePrinterConn = null;
        try {
            // Instantiate connection for given Bluetooth® MAC Address.
            thePrinterConn = new BluetoothConnection(theBtMacAddress);

            // Open the connection - physical connection is established here.
            thePrinterConn.Open();

            // This example prints "This is a CPCL test." near the top of the label.
            string cpclData = "! 0 200 200 210 1\r\n"
                + "TEXT 4 0 30 40 This is a CPCL test.\r\n"
                + "FORM\r\n"
                + "PRINT\r\n";

            // Send the data to printer as a byte array.
            thePrinterConn.Write(Encoding.UTF8.GetBytes(cpclData));
        } catch (Exception e) {
            // Handle communications error here.
            Console.WriteLine(e.ToString());
        } finally {
            // Close the connection to release resources.
            if (thePrinterConn != null) {
                thePrinterConn.Close();
            }
        }
    }
}
Examples
iOS
using CoreGraphics;
using System;
using System.Text;
using System.Threading.Tasks;
using UIKit;
using Zebra.Sdk.Comm;

public class BluetoothConnectionExample : UIViewController {

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

        testButton.SetTitle("Run Connection Builder Example", UIControlState.Normal);

        testButton.TouchUpInside += async (sender, e) => {
            string theBluetoothMACAddress = "11:22:33:44:55:66";

            await Task.Run(() => {
                SendZplOverBluetooth(theBluetoothMACAddress);
                SendCpclOverBluetooth(theBluetoothMACAddress);
            });
        };

        View.AddSubview(testButton);
    }

    private void SendZplOverBluetooth(string theBtMacAddress) {
        Connection thePrinterConn = null;
        try {
            // Instantiate connection for given Bluetooth® MAC Address.
            thePrinterConn = new BluetoothConnection(theBtMacAddress);

            // Open the connection - physical connection is established here.
            thePrinterConn.Open();

            // This example prints "This is a ZPL test." near the top of the label.
            string zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";

            // Send the data to printer as a byte array.
            thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));
        } catch (Exception e) {
            // Handle communications error here.
            Console.WriteLine(e.ToString());
        } finally {
            // Close the connection to release resources.
            if (thePrinterConn != null) {
                thePrinterConn.Close();
            }
        }
    }

    private void SendCpclOverBluetooth(string theBtMacAddress) {
        Connection thePrinterConn = null;
        try {
            // Instantiate connection for given Bluetooth® MAC Address.
            thePrinterConn = new BluetoothConnection(theBtMacAddress);

            // Open the connection - physical connection is established here.
            thePrinterConn.Open();

            // This example prints "This is a CPCL test." near the top of the label.
            string cpclData = "! 0 200 200 210 1\r\n"
                + "TEXT 4 0 30 40 This is a CPCL test.\r\n"
                + "FORM\r\n"
                + "PRINT\r\n";

            // Send the data to printer as a byte array.
            thePrinterConn.Write(Encoding.UTF8.GetBytes(cpclData));
        } catch (Exception e) {
            // Handle communications error here.
            Console.WriteLine(e.ToString());
        } finally {
            // Close the connection to release resources.
            if (thePrinterConn != null) {
                thePrinterConn.Close();
            }
        }
    }
}
See Also