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, write
addWriteLogStream
bytesAvailable, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, write
public 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 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_STATUS
:[MAC Address]:[Friendly Name].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
Connection
ConnectionReestablisher
which allows for easy recreation of a connection which may have
been closed.
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.