public class BluetoothLeStatusConnection extends ConnectionStatusA
BluetoothLeConnection.
package test.zebra.android.comm.examples;
import com.zebra.sdk.btleComm.BluetoothLeStatusConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.SGD;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class BluetoothLeStatusConnectionExample extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String theBtMacAddress = "00:11:BB:DD:55:FF";
Context context = getApplicationContext();
sendJSONOverStatusChannel(theBtMacAddress, context);
}
private void sendJSONOverStatusChannel(final String theBtMacAddress, final Context context) {
new Thread(new Runnable() {
public void run() {
Connection thePrinterConn = null;
try {
// Instantiate a status only connection for given Bluetooth® MAC Address.
thePrinterConn = new BluetoothLeStatusConnection(theBtMacAddress, context);
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This sends down JSON to the status channel to retrieve the 'appl.name' setting
String firmwareVersion = SGD.GET("appl.name", thePrinterConn);
System.out.println("The firmware version is : " + firmwareVersion);
} 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 have Bluetooth® Low Energy capabilies.| Constructor and Description |
|---|
BluetoothLeStatusConnection(String macAddress,
android.content.Context context)
Constructs a new Bluetooth® connection with the given
macAddress. |
BluetoothLeStatusConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData,
android.content.Context context)
Exposed this protected constructor for testing.
|
| 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® Low Energy 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.
|
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_STATUS:[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, writeaddWriteLogStreambytesAvailable, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, writepublic BluetoothLeStatusConnection(String macAddress,
android.content.Context context)
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 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.BluetoothLeStatusConnection(String, int, int, Context).macAddress - the device's MAC address.context - Android application context.public BluetoothLeStatusConnection(String macAddress,
int maxTimeoutForRead,
int timeToWaitForMoreData,
android.content.Context context)
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 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()
BluetoothLe_STATUS:[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)public android.content.Context getContext()
public void setContext(android.content.Context context)
context - Android application context.
© 2017 ZIH Corp. All Rights Reserved.