UniPRT SDK  v2.0.0.0
BTClassicConnection Class Reference

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

Inheritance diagram for BTClassicConnection:
AComm IComm

Public Member Functions

 BTClassicConnection (long bt_address)
 Initialize new class instance based on Bluetooth MAC address. More...
 
 BTClassicConnection (String descriptor)
 Initialize new class instance based on descriptor string. More...
 
int BytesAvailable ()
 Number of bytes available to read. This is estimate and number can change as data is received.
 
void Close ()
 Closes a connection.
 
boolean Connected ()
 true if connection is established
 
String Descriptor ()
 Returns a string description of the connection. More...
 
void Open ()
 Opens a connection.
 
byte[] Read ()
 Read all bytes available.
 
void Read (OutputStream binDataIn) throws IOException
 Reads all bytes available into the stream passed in.
 
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 (byte[] dataOut)
 Writes all bytes from the array passed in.
 
void Write (InputStream binReader) throws IOException
 Write from input stream to output stream
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...
 
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...
 

Static Public Member Functions

static long DescriptorValidate (String descriptorHint)
 Validate descriptor string and extract MAC address. More...
 

Detailed Description

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

Examples

package com.test.UniPRTSdk;
import com.UniPRT.Sdk.Comm.BTClassicConnection;
import java.io.*;
public class commBTClassicSnippet {
public static void main(String[] args) {
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]

BTClassicConnection ( String  descriptor)

Initialize new class instance based on descriptor string.

Parameters
descriptorFormat: BT:MAC Address:<_bt_mac>

◆ BTClassicConnection() [2/2]

BTClassicConnection ( long  bt_address)

Initialize new class instance based on Bluetooth MAC address.

Parameters
bt_addressBluetooth MAC address in long format, e.g. 0x0080A3123456L

Member Function Documentation

◆ Descriptor()

String Descriptor ( )

Returns a string description of the connection.

Format: BT:MAC Address:<_bt_mac> Example: MAC Address:0080A3123456

Implements IComm.

◆ DescriptorValidate()

static long DescriptorValidate ( String  descriptorHint)
static

Validate descriptor string and extract MAC address.

Acceptable format:

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

◆ WaitForData()

void WaitForData ( int  msTimeOut)
inherited

Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call.

Parameters
[in]msTimeOutMillisecond timeout before return if no data received.

Implements IComm.

◆ Write()

void Write ( InputStream  binReader) throws IOException
inherited

Write from input stream to output stream

Parameters
[in]binReaderSource data to send

Implements IComm.

◆ WriteAndWaitForResponse() [1/2]

byte [] WriteAndWaitForResponse ( byte[]  dataOut,
int  responseStartTimeOut,
int  responseEndTimeOut,
String  completionToken 
) throws IOException
inherited

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.

Return values
byte[]Array of bytes received from connection.
Data returned is any data received or up to completetionToken if token received
Parameters
[in]dataOutSource byte array to write to connection.
[in]responseStartTimeOutTimeout to wait for first data to be received. Time to first byte.
[in]responseEndTimeOutTimeout of no new data received. Time after last byte.
[in]completetionTokenString token denoting the end of the response expected. This can be empty string if simply want to wait for response after writing data to connection.

Implements IComm.

◆ WriteAndWaitForResponse() [2/2]

void WriteAndWaitForResponse ( OutputStream  binDataIn,
InputStream  binDataOut,
int  responseStartTimeOut,
int  responseEndTimeOut,
String  completionToken 
) throws IOException
inherited

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.

Parameters
[out]binDataInDestination stream to store response received from connection.
[in]binDataOutSource stream data to write to connection.
[in]responseStartTimeOutTimeout to wait for first data to be received. Time to first byte.
[in]responseEndTimeOutTimeout of no new data received. Time after last byte.
[in]completetionTokenString token denoting the end of the response expected. This can be empty string if simply want to wait for response after writing data to connection.

Implements IComm.

com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection
BTClassicConnection(String descriptor)
Initialize new class instance based on descriptor string.
Definition: BTClassicConnection.java:227