public class MultichannelBluetoothConnection extends MultichannelConnection
package test.zebra.android.comm.examples;
import android.os.Looper;
import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection;
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(final String theBtMacAddress) {
new Thread(new Runnable() {
public void run() {
try {
// Instantiate connection for given Bluetooth® MAC Address.
Connection thePrinterConn = new BluetoothConnection(theBtMacAddress);
// Initialize
Looper.prepare();
// 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(zplData.getBytes());
// Make sure the data got to the printer before closing the connection
Thread.sleep(500);
// Close the connection to release resources.
thePrinterConn.close();
Looper.myLooper().quit();
} catch (Exception e) {
// Handle communications error here.
e.printStackTrace();
}
}
}).start();
}
private void sendCpclOverBluetooth(final String theBtMacAddress) {
new Thread(new Runnable() {
public void run() {
try {
// Instantiate connection for given Bluetooth® MAC Address.
Connection thePrinterConn = new BluetoothConnection(theBtMacAddress);
// Initialize
Looper.prepare();
// 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(cpclData.getBytes());
// Make sure the data got to the printer before closing the connection
Thread.sleep(500);
// Close the connection to release resources.
thePrinterConn.close();
Looper.myLooper().quit();
} catch (Exception e) {
// Handle communications error here.
e.printStackTrace();
}
}
}).start();
}
}
Note: 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).Constructor and Description |
---|
MultichannelBluetoothConnection(String macAddress)
Initializes a new instance of the
MultichannelBluetoothConnection class. |
MultichannelBluetoothConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData)
Initializes a new instance of the
MultichannelBluetoothConnection class. |
MultichannelBluetoothConnection(String macAddress,
int printingChannelMaxTimeoutForRead,
int printingChannelTimeToWaitForMoreData,
int statusChannelMaxTimeoutForRead,
int statusChannelTimeToWaitForMoreData)
Initializes a new instance of the
MultichannelBluetoothConnection class. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes both the printing and status channels of this MultichannelConnection.
|
String |
getSimpleConnectionName()
Return the MAC address as the description.
|
void |
open()
Opens both the printing and status channel of this Multichannel connection.
|
String |
toString()
Returns
Bluetooth_MULTI :[MAC Address]:[Friendly Name].The friendly name is obtained from the device when this connection is opened. |
addWriteLogStream, bytesAvailable, closePrintingChannel, closeStatusChannel, getConnectionReestablisher, getMaxTimeoutForRead, getPrintingChannel, getStatusChannel, getTimeToWaitForMoreData, isConnected, openPrintingChannel, openStatusChannel, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, write
public MultichannelBluetoothConnection(String macAddress)
MultichannelBluetoothConnection
class. This constructor will use
the default timeouts for Connection.read()
. The default timeout is a maximum of 5 seconds for any data to
be received. If no more data is available after 500 milliseconds the read operation is assumed to be complete.
MultichannelBluetoothConnection(String, int, int)
or
MultichannelBluetoothConnection(String, int, int, int, int)
.macAddress
- The MAC Address of the Printer.public MultichannelBluetoothConnection(String macAddress, int maxTimeoutForRead, int timeToWaitForMoreData)
MultichannelBluetoothConnection
class. This constructor will use
the specified timeouts for Connection.read()
for both channels. The timeout is a maximum of
maxTimeoutForRead
milliseconds for any data to be received. If no more data is available after
timeToWaitForMoreData
milliseconds the read operation is assumed to be complete. If you wish to
specify different timeouts for each channel, use
MultichannelBluetoothConnection(String, int, int, int, int)
.macAddress
- The MAC Address of the Printer.maxTimeoutForRead
- the maximum time, in milliseconds, to wait for any data to be received.timeToWaitForMoreData
- the maximum time, in milliseconds, to wait in-between reads after the initial read.public MultichannelBluetoothConnection(String macAddress, int printingChannelMaxTimeoutForRead, int printingChannelTimeToWaitForMoreData, int statusChannelMaxTimeoutForRead, int statusChannelTimeToWaitForMoreData)
MultichannelBluetoothConnection
class. This constructor will use
the specified timeouts for Connection.read()
for the channels. The timeout is a maximum of
printingChannelMaxTimeoutForRead/statusChannelMaxTimeoutForRead
milliseconds for any data to be
received on the printing/status channels respectively. If no more data is available after
printingChannelTimeToWaitForMoreData/statusChannelTimeToWaitForMoreData
milliseconds on the
printing/status channels respectively the read operation is assumed to be complete.macAddress
- The MAC Address of the Printer.printingChannelMaxTimeoutForRead
- the maximum time, in milliseconds, to wait for any data to be received on
the printing channel.printingChannelTimeToWaitForMoreData
- the maximum time, in milliseconds, to wait in-between reads after the
initial read on the printing channel.statusChannelMaxTimeoutForRead
- the maximum time, in milliseconds, to wait for any data to be received on
the status channel.statusChannelTimeToWaitForMoreData
- the maximum time, in milliseconds, to wait in-between reads after the
initial read on the status channel.public void open() throws ConnectionException
ConnectionException
is thrown. When this Multichannel connection is no longer needed, you must
call Connection.close()
to free up system resources.open
in interface Connection
open
in class MultichannelConnection
ConnectionException
- if the connection cannot be established.public void close() throws ConnectionException
MultichannelConnection
close
in interface Connection
close
in class MultichannelConnection
ConnectionException
MultichannelConnection.close()
public String toString()
Bluetooth_MULTI
:[MAC Address]:[Friendly Name].toString
in interface Connection
toString
in class Object
Object.toString()
public String getSimpleConnectionName()
Connection.getSimpleConnectionName()
© 2017 ZIH Corp. All Rights Reserved.