public class BluetoothLeConnection extends ConnectionA
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®, the device must the device must have Bluetooth® Low Energy capabilies.Constructor and Description |
---|
BluetoothLeConnection(String macAddress)
Constructs a new Bluetooth® connection with the given
macAddress and timeout values. |
BluetoothLeConnection(String macAddress,
android.content.Context context)
Constructs a new Bluetooth® connection with the given
macAddress and timeout values. |
BluetoothLeConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData,
android.content.Context context)
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.
|
android.content.Context |
getContext()
Get the Context.
|
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® Low Energy connection as specified in the constructor.
|
void |
setContext(android.content.Context context)
Set Application Context.
Note: This method will need to be called after building a connection with com.zebra.sdk.comm.ConnectionBuilder.build(String) |
String |
toString()
Returns
BluetoothLe :[MAC Address]:[Friendly Name]. |
addWriteLogStream, bytesAvailable, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, read, read, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write, write, write
public BluetoothLeConnection(String macAddress)
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 com.zebra.sdk.comm.Connection. 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.setContext(Context)
in order to connect to a Printer.macAddress
- the device's MAC address.public BluetoothLeConnection(String macAddress, android.content.Context context)
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 com.zebra.sdk.comm.Connection. 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.context
- Android application context.public BluetoothLeConnection(String macAddress, int maxTimeoutForRead, int timeToWaitForMoreData, android.content.Context context)
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 com.zebra.sdk.comm.Connection. 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.context
- Android application context.public void open() throws ConnectionException
open
in interface Connection
open
in class ConnectionA
ConnectionException
- if the connection cannot be established.ConnectionA.open()
public void close() throws ConnectionException
close
in interface Connection
close
in class ConnectionA
ConnectionException
- if an I/O error occurs.Connection.close()
public String toString()
BluetoothLe
:[MAC Address]:[Friendly Name].
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.toString
in interface Connection
toString
in class Object
Connection.toString()
public String getSimpleConnectionName()
Connection.getSimpleConnectionName()
public String getMACAddress()
public String getFriendlyName()
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
getConnectionReestablisher
in interface Connection
getConnectionReestablisher
in class ConnectionA
thresholdTime
- 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)
public android.content.Context getContext()
public void setContext(android.content.Context context)
context
- Android application context.
© 2017 ZIH Corp. All Rights Reserved.