UniPRT SDK  v2.0.0.1
com.UniPRT.Sdk.Comm.BLEConnection Class Reference

Class that implements the BLE specific communication and extends the AComm class. More...

Inheritance diagram for com.UniPRT.Sdk.Comm.BLEConnection:
com.UniPRT.Sdk.Comm.AComm com.UniPRT.Sdk.Comm.IComm

Public Member Functions

 BLEConnection (Context context, String address)
 Initialize new class instance based on descriptor string. More...
 
int BytesAvailable ()
 
String Descriptor ()
 Validate descriptor string from descriptorHint that may or may not have a v_id and p_id. Returns long. More...
 
boolean Connected ()
 
void Close ()
 
void CloseWithTimeout (int timeoutMs)
 
void Open ()
 
byte[] Read ()
 
void Write (byte[] dataOut)
 
- Public Member Functions inherited from com.UniPRT.Sdk.Comm.AComm
void Read (OutputStream binDataIn) throws IOException
 
void WaitForData (int msTimeOut)
 Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call. More...
 
void Write (InputStream binReader) throws IOException
 Write from input stream to output stream. More...
 
void WriteAndWaitForResponse (OutputStream binDataIn, InputStream binDataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 Write binDataOut stream data to output stream and return data received in binDataIn stream. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 
byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 Write byte data to output stream and return data received. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 

Static Public Member Functions

static CompletableFuture< List< BluetoothDevice > > scanDevicesForDuration (int seconds)
 

Private Member Functions

CompletableFuture< Void > waitForDisconnectedAsync (int timeoutMs)
 
CompletableFuture< Void > waitForConnectedAsync (int timeoutMs)
 
void waitAck () throws InterruptedException
 
void notifyAck ()
 

Private Attributes

BluetoothDevice device
 
BluetoothGatt mBluetoothGatt
 
BluetoothGattCharacteristic rxCharacteristic
 
BluetoothGattCharacteristic txCharacteristic
 
String deviceAddress = ""
 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
 
int ble_mtu = 512
 
boolean isConnected = false
 
final Object ackLock = new Object()
 
boolean ackReceived = false
 
final Context context
 
final Semaphore semaphore = new Semaphore(1)
 
final BluetoothGattCallback mGattCallback
 

Static Private Attributes

static final UUID SERVICE_UUID = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb")
 
static final UUID TX_UUID = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb")
 
static final UUID RX_UUID = UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb")
 
static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
 

Detailed Description

Class that implements the BLE specific communication and extends the AComm class.

Examples

package com.test.UniPRTSdk;
import java.io.*;
public class commBTClassicSnippet {
public static void test() {
sendPrintFile(); // send file over default printer data port
sendPrintString(); // send print data over default printer data port
}
public static void sendPrintFile() {
String fileName = "C:\\testFiles\\Hello.pgl";
BLEConnection comm = BLEConnection(this, "DC:0D:30:23:3A:6C");
try {
comm.Open();
if (new File(fileName).exists()) {
try (InputStream binReader = new BufferedInputStream(new FileInputStream(fileName))) {
System.out.println("Sending \"" + fileName + "\" to printer");
byte[] buffer = new byte[1024];
while ((binReader.read(buffer)) != -1) {
comm.Write(buffer);
}
}
} else {
System.out.println("File \"" + fileName + "\" not found");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
comm.Close();
}
}
public static void sendPrintString() {
String dataToPrint =
"~CREATE;C39;72\n" +
"SCALE;DOT\n" +
"PAGE;30;40\n" +
"ALPHA\n" +
"C10;1;33;0;0;@HELLO@\n" +
"C16;54;37;0;0;@*World*@\n" +
"STOP\n" +
"BARCODE\n" +
"C128C;XRD3:3:6:6:9:9:12:12;H6;10;32\n" +
"@World@\n" +
"STOP\n" +
"END\n" +
"~EXECUTE;C39\n" +
"~NORMAL\n";
BLEConnection comm = BLEConnection(this, "DC:0D:30:23:3A:6C");
try {
comm.Open();
if (comm.Connected()) {
byte[] outBytes = dataToPrint.getBytes("US-ASCII");
comm.Write(outBytes);
} else {
System.out.println("Not connected to printer");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
comm.Close();
}
}
}

Constructor & Destructor Documentation

◆ BLEConnection()

com.UniPRT.Sdk.Comm.BLEConnection.BLEConnection ( Context  context,
String  address 
)

Initialize new class instance based on descriptor string.

Parameters
descriptorAcceptable Formats:
  • "BT:MAC Address:<_bt_mac>"
    • _bt_mac is hexadecimal String. e.g. MAC Address:"00:80:A3:12:34:56"

References com.UniPRT.Sdk.Comm.BLEConnection.context.

Member Function Documentation

◆ BytesAvailable()

int com.UniPRT.Sdk.Comm.BLEConnection.BytesAvailable ( )

Get the number of bytes available to read.

Returns
Number of bytes available to read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BLEConnection.byteArrayOutputStream.

◆ Close()

void com.UniPRT.Sdk.Comm.BLEConnection.Close ( )

◆ CloseWithTimeout()

◆ Connected()

boolean com.UniPRT.Sdk.Comm.BLEConnection.Connected ( )

Check if connection is established.

Returns
true if connection is established, otherwise false.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BLEConnection.isConnected.

Referenced by com.UniPRT.Sdk.Comm.BLEConnection.Open().

◆ Descriptor()

String com.UniPRT.Sdk.Comm.BLEConnection.Descriptor ( )

Validate descriptor string from descriptorHint that may or may not have a v_id and p_id. Returns long.

descriptorHint accepted values:

  • "BLE:MAC Address:<_bt_mac>"
    • _bt_mac is hexadecimal String. e.g. MAC Address:0080A3123456

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BLEConnection.deviceAddress.

◆ notifyAck()

void com.UniPRT.Sdk.Comm.BLEConnection.notifyAck ( )
private

◆ Open()

◆ Read()

byte [] com.UniPRT.Sdk.Comm.BLEConnection.Read ( )

Read all available bytes.

Returns
Array of bytes read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BLEConnection.byteArrayOutputStream, and com.UniPRT.Sdk.Comm.BLEConnection.semaphore.

◆ scanDevicesForDuration()

static CompletableFuture<List<BluetoothDevice> > com.UniPRT.Sdk.Comm.BLEConnection.scanDevicesForDuration ( int  seconds)
static

◆ waitAck()

void com.UniPRT.Sdk.Comm.BLEConnection.waitAck ( ) throws InterruptedException
private

◆ waitForConnectedAsync()

CompletableFuture<Void> com.UniPRT.Sdk.Comm.BLEConnection.waitForConnectedAsync ( int  timeoutMs)
private

◆ waitForDisconnectedAsync()

CompletableFuture<Void> com.UniPRT.Sdk.Comm.BLEConnection.waitForDisconnectedAsync ( int  timeoutMs)
private

◆ Write()

void com.UniPRT.Sdk.Comm.BLEConnection.Write ( byte[]  dataOut)

Member Data Documentation

◆ ackLock

final Object com.UniPRT.Sdk.Comm.BLEConnection.ackLock = new Object()
private

◆ ackReceived

boolean com.UniPRT.Sdk.Comm.BLEConnection.ackReceived = false
private

◆ ble_mtu

int com.UniPRT.Sdk.Comm.BLEConnection.ble_mtu = 512
private

◆ byteArrayOutputStream

ByteArrayOutputStream com.UniPRT.Sdk.Comm.BLEConnection.byteArrayOutputStream = new ByteArrayOutputStream()
private

◆ context

final Context com.UniPRT.Sdk.Comm.BLEConnection.context
private

◆ device

BluetoothDevice com.UniPRT.Sdk.Comm.BLEConnection.device
private

◆ deviceAddress

String com.UniPRT.Sdk.Comm.BLEConnection.deviceAddress = ""
private

◆ isConnected

◆ mBluetoothAdapter

BluetoothAdapter com.UniPRT.Sdk.Comm.BLEConnection.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
staticprivate

◆ mBluetoothGatt

BluetoothGatt com.UniPRT.Sdk.Comm.BLEConnection.mBluetoothGatt
private

◆ mGattCallback

final BluetoothGattCallback com.UniPRT.Sdk.Comm.BLEConnection.mGattCallback
private

◆ RX_UUID

final UUID com.UniPRT.Sdk.Comm.BLEConnection.RX_UUID = UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb")
staticprivate

◆ rxCharacteristic

BluetoothGattCharacteristic com.UniPRT.Sdk.Comm.BLEConnection.rxCharacteristic
private

◆ semaphore

final Semaphore com.UniPRT.Sdk.Comm.BLEConnection.semaphore = new Semaphore(1)
private

◆ SERVICE_UUID

final UUID com.UniPRT.Sdk.Comm.BLEConnection.SERVICE_UUID = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb")
staticprivate

◆ TX_UUID

final UUID com.UniPRT.Sdk.Comm.BLEConnection.TX_UUID = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb")
staticprivate

◆ txCharacteristic

BluetoothGattCharacteristic com.UniPRT.Sdk.Comm.BLEConnection.txCharacteristic
private

The documentation for this class was generated from the following file:
com.UniPRT.Sdk.Comm
Definition: AComm.java:1
com.UniPRT
com.UniPRT.Sdk.Comm.BTClassicConnection
Class that implements the Bluetooth Classic specific communication and extends the AComm class.
Definition: BTClassicConnection.java:36
com.UniPRT.Sdk.Comm.BLEConnection.BLEConnection
BLEConnection(Context context, String address)
Initialize new class instance based on descriptor string.
Definition: BLEConnection.java:105
com.UniPRT.Sdk
com