public class BluetoothConnection extends ConnectionA
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 |
|---|
BluetoothConnection(String macAddress)
Constructs a new Bluetooth® connection with the given
macAddress. |
BluetoothConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData)
Constructs a new Bluetooth® connection with the given
macAddress and timeout values. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the Bluetooth® connection.
|
ConnectionReestablisher |
getConnectionReestablisher(long thresholdTime)
Returns a
ConnectionReestablisher which allows for easy recreation of a connection which may have
been closed. |
String |
getFriendlyName()
Returns the friendly name of the Bluetooth® connection.
|
String |
getMACAddress()
Returns the MAC address which was passed into the constructor.
|
String |
getSimpleConnectionName()
Return the MAC address and the friendly name as the description.
|
void |
open()
Opens a Bluetooth® connection as specified in the constructor.
|
String |
toString()
Returns
Bluetooth:[MAC Address]:[Friendly Name].The friendly name is obtained from the device when this connection is opened. |
addWriteLogStream, bytesAvailable, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, read, read, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write, write, writepublic BluetoothConnection(String macAddress)
macAddress. The MAC address is a
hexadecimal string with or without separators between the octets. (e.g. 00:11:BB:DD:55:FF or 0011BBDD55FF). 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.BluetoothConnection(String, int, int).macAddress - the device's MAC address.public BluetoothConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData)
macAddress and timeout values. The MAC
address is a hexadecimal string with or without separators between the octets. (e.g. 00:11:BB:DD:55:FF or
0011BBDD55FF). This constructor will use the specified timeouts for Connection.read(). 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.macAddress - the device's MAC address.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 void open()
throws ConnectionException
open in interface Connectionopen in class ConnectionAConnectionException - if the connection cannot be established.ConnectionA.open()public void close()
throws ConnectionException
close in interface Connectionclose in class ConnectionAConnectionException - if an I/O error occurs.Connection.close()public String toString()
Bluetooth:[MAC Address]:[Friendly Name].toString in interface ConnectiontoString in class ObjectConnection.toString()public String getSimpleConnectionName()
Connection.getSimpleConnectionName()public String getMACAddress()
public String getFriendlyName()
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
ConnectionConnectionReestablisher which allows for easy recreation of a connection which may have
been closed.
getConnectionReestablisher in interface ConnectiongetConnectionReestablisher in class ConnectionAthresholdTime - how long the Connection reestablisher will wait before attempting to reconnection to the
printerConnectionException - if the ConnectionReestablisher could not be created.Connection.getConnectionReestablisher(long thresholdTime)
© 2017 ZIH Corp. All Rights Reserved.