public class MultichannelBluetoothLeConnection extends MultichannelConnection
package test.zebra.android.comm.examples;
import com.zebra.sdk.btleComm.BluetoothLeConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class BluetoothLeConnectionExample extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String theBtMacAddress = "00:11:BB:DD:55:FF";
Context context = getApplicationContext();
sendZplOverBluetoothLe(theBtMacAddress, context);
}
private void sendZplOverBluetoothLe(final String theBtMacAddress, final Context context) {
new Thread(new Runnable() {
public void run() {
Connection thePrinterConn = null;
try {
// Instantiate connection for given Bluetooth® MAC Address.
thePrinterConn = new BluetoothLeConnection(theBtMacAddress, context);
// 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);
} catch (Exception e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
if (null != thePrinterConn) {
try {
thePrinterConn.close();
} catch (ConnectionException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}
Note: In order to connect to a device with Bluetooth® Low Energy,
the device must be discoverable, authentication must be enabled, and the authentication pin must be set (1-16
alphanumeric characters).Constructor and Description |
---|
MultichannelBluetoothLeConnection(String macAddress,
android.content.Context context)
Initializes a new instance of the
MultichannelBluetoothLeConnection class. |
MultichannelBluetoothLeConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData,
android.content.Context context)
Initializes a new instance of the
MultichannelBluetoothLeConnection class. |
MultichannelBluetoothLeConnection(String macAddress,
int printingChannelMaxTimeoutForRead,
int printingChannelTimeToWaitForMoreData,
int statusChannelMaxTimeoutForRead,
int statusChannelTimeToWaitForMoreData,
android.content.Context context)
Initializes a new instance of the
MultichannelBluetoothLeConnection class. |
Modifier and Type | Method and Description |
---|---|
String |
getSimpleConnectionName()
Return the MAC address as the description.
|
String |
toString()
Returns
BluetoothLe_MULTI :[MAC Address]:[Friendly Name].The friendly name is obtained from the device when this connection is opened. |
addWriteLogStream, bytesAvailable, close, closePrintingChannel, closeStatusChannel, getConnectionReestablisher, getMaxTimeoutForRead, getPrintingChannel, getStatusChannel, getTimeToWaitForMoreData, isConnected, open, openPrintingChannel, openStatusChannel, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, write
public MultichannelBluetoothLeConnection(String macAddress, android.content.Context context)
MultichannelBluetoothLeConnection
class. This constructor will use the
default timeouts for com.zebra.sdk.comm.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.MultichannelBluetoothLeConnection(String, int, int, Context)
or
MultichannelBluetoothLeConnection(String, int, int, int, int, Context)
.macAddress
- the device's MAC address.context
- Android application context.public MultichannelBluetoothLeConnection(String macAddress, int maxTimeoutForRead, int timeToWaitForMoreData, android.content.Context context)
MultichannelBluetoothLeConnection
class. This constructor will use the
specified timeouts for com.zebra.sdk.comm.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
MultichannelBluetoothLeConnection(String, int, int, int, int, Context)
.macAddress
- the device's MAC address.context
- Android application context.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 MultichannelBluetoothLeConnection(String macAddress, int printingChannelMaxTimeoutForRead, int printingChannelTimeToWaitForMoreData, int statusChannelMaxTimeoutForRead, int statusChannelTimeToWaitForMoreData, android.content.Context context)
MultichannelBluetoothLeConnection
class. This constructor will use the
specified timeouts for com.zebra.sdk.comm.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 device's MAC address.context
- Android application context.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 String toString()
BluetoothLe_MULTI
:[MAC Address]:[Friendly Name].toString
in interface Connection
toString
in class Object
Connection.toString()
public String getSimpleConnectionName()
Connection.getSimpleConnectionName()
© 2017 ZIH Corp. All Rights Reserved.