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

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

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

Public Member Functions

 BTClassicConnection (String descriptor)
 Initialize new class instance based on descriptor string. More...
 
 BTClassicConnection (long bt_address)
 Initialize new class instance based on macAddress. Bluetooth pairing with the printer is required before connecting. More...
 
int BytesAvailable ()
 
String Descriptor ()
 
boolean Connected ()
 
void Close ()
 
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 long DescriptorValidate (String descriptorHint)
 Validate descriptor string from descriptorHint that may or may not have a v_id and p_id. Returns long. More...
 

Private Attributes

BluetoothAdapter mBluetoothAdapter = null
 
BluetoothSocket btSocket = null
 
OutputStream OutStream = null
 
InputStream InStream = null
 
long _bt_address
 

Static Private Attributes

static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
 
static final int BLOCK_SIZE = 1024
 

Detailed Description

Class that implements the Bluetooth Classic 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";
BTClassicConnection ptrBTClassicComm = new BTClassicConnection(0x0080A3123456L); // sending through default data port
try {
ptrBTClassicComm.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) {
ptrBTClassicComm.Write(buffer);
}
}
} else {
System.out.println("File \"" + fileName + "\" not found");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrBTClassicComm.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";
BTClassicConnection ptrBTClassicComm = new BTClassicConnection(0x0080A3123456L); // sending through default data port
try {
ptrBTClassicComm.Open();
if (ptrBTClassicComm.Connected()) {
byte[] outBytes = dataToPrint.getBytes("US-ASCII");
ptrBTClassicComm.Write(outBytes);
} else {
System.out.println("Not connected to printer");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrBTClassicComm.Close();
}
}
}

Constructor & Destructor Documentation

◆ BTClassicConnection() [1/2]

com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection ( String  descriptor)

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:0080A3123456

References com.UniPRT.Sdk.Comm.BTClassicConnection._bt_address, and com.UniPRT.Sdk.Comm.BTClassicConnection.DescriptorValidate().

◆ BTClassicConnection() [2/2]

com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection ( long  bt_address)

Initialize new class instance based on macAddress. Bluetooth pairing with the printer is required before connecting.

Parameters
[in]macAddressSpecify the printer to be connected via the Bluetooth MAC address of the printer. e.g. 0x0080A3123456L

Member Function Documentation

◆ BytesAvailable()

int com.UniPRT.Sdk.Comm.BTClassicConnection.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.BTClassicConnection.InStream.

Referenced by com.UniPRT.Sdk.Comm.BTClassicConnection.Read().

◆ Close()

◆ Connected()

boolean com.UniPRT.Sdk.Comm.BTClassicConnection.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.BTClassicConnection.btSocket.

◆ Descriptor()

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

Get the string descriptor of the connection.

Returns
String descriptor of the connection.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BTClassicConnection._bt_address.

◆ DescriptorValidate()

static long com.UniPRT.Sdk.Comm.BTClassicConnection.DescriptorValidate ( String  descriptorHint)
static

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

descriptorHint accepted values:

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

Referenced by com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection().

◆ Open()

◆ Read()

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

Read all available bytes.

Returns
Array of bytes read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BTClassicConnection.BytesAvailable(), and com.UniPRT.Sdk.Comm.BTClassicConnection.InStream.

◆ Write()

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

Write all bytes from the array passed in.

Parameters
dataOutByte array to write.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.BTClassicConnection.BLOCK_SIZE, and com.UniPRT.Sdk.Comm.BTClassicConnection.OutStream.

Member Data Documentation

◆ _bt_address

◆ BLOCK_SIZE

final int com.UniPRT.Sdk.Comm.BTClassicConnection.BLOCK_SIZE = 1024
staticprivate

◆ btSocket

BluetoothSocket com.UniPRT.Sdk.Comm.BTClassicConnection.btSocket = null
private

◆ InStream

◆ mBluetoothAdapter

BluetoothAdapter com.UniPRT.Sdk.Comm.BTClassicConnection.mBluetoothAdapter = null
private

◆ MY_UUID_SECURE

final UUID com.UniPRT.Sdk.Comm.BTClassicConnection.MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
staticprivate

◆ OutStream

OutputStream com.UniPRT.Sdk.Comm.BTClassicConnection.OutStream = null
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
com
com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection
BTClassicConnection(String descriptor)
Initialize new class instance based on descriptor string.
Definition: BTClassicConnection.java:129