UniPRT SDK  v2.0.0.1
com.UniPRT.Sdk.Comm.BTClassicConnection类 参考

实现蓝牙经典(Bluetooth Classic)通信的类,并继承自 AComm 类。 更多...

类 com.UniPRT.Sdk.Comm.BTClassicConnection 继承关系图:
com.UniPRT.Sdk.Comm.AComm com.UniPRT.Sdk.Comm.IComm

Public 成员函数

 BTClassicConnection (String descriptor)
 根据 descriptor 字符串初始化新实例。 更多...
 
 BTClassicConnection (long bt_address)
 根据 macAddress 初始化新实例。 连接前需先与打印机进行蓝牙配对。 更多...
 
int BytesAvailable ()
 
String Descriptor ()
 
boolean Connected ()
 
void Close ()
 
void Open ()
 
byte[] Read ()
 
void Write (byte[] dataOut)
 
- Public 成员函数 继承自 com.UniPRT.Sdk.Comm.AComm
void Read (OutputStream binDataIn) throws IOException
 
void WaitForData (int msTimeOut)
 等待当前线程有可用数据 BytesAvailable。当前线程会休眠直到接收到数据或超时为止。此为阻塞调用。 更多...
 
void Write (InputStream binReader) throws IOException
 从输入流写入输出流。 更多...
 
void WriteAndWaitForResponse (OutputStream binDataIn, InputStream binDataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 将 binDataOut 流数据写入输出流,并将接收到的数据返回到 binDataIn 流中。返回的数据是收到的数据或收到的完成标记。等待响应超时后返回。 更多...
 
byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 将字节数据写入输出流并返回接收到的数据。返回的数据是接收到的任何数据或收到的完成标记。等待响应超时后返回。 更多...
 

静态 Public 成员函数

static long DescriptorValidate (String descriptorHint)
 验证 descriptorHint 中的描述字符串是否有效,可能包含或不包含 v_id 和 p_id。返回 long 值。 更多...
 

Private 属性

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

静态 Private 属性

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

详细描述

实现蓝牙经典(Bluetooth Classic)通信的类,并继承自 AComm 类。

示例

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();
}
}
}

构造及析构函数说明

◆ BTClassicConnection() [1/2]

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

根据 descriptor 字符串初始化新实例。

参数
descriptor可接受格式:
  • "BT:MAC Address:<_bt_mac>"
    • _bt_mac 为十六进制字符串,例如:MAC Address:0080A3123456

引用了 com.UniPRT.Sdk.Comm.BTClassicConnection._bt_address , 以及 com.UniPRT.Sdk.Comm.BTClassicConnection.DescriptorValidate().

◆ BTClassicConnection() [2/2]

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

根据 macAddress 初始化新实例。 连接前需先与打印机进行蓝牙配对。

参数
[in]macAddress通过蓝牙 MAC 地址指定要连接的打印机,例如:0x0080A3123456L

成员函数说明

◆ BytesAvailable()

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

Get the number of bytes available to read.

返回
Number of bytes available to read.

实现了 com.UniPRT.Sdk.Comm.IComm.

引用了 com.UniPRT.Sdk.Comm.BTClassicConnection.InStream.

被这些函数引用 com.UniPRT.Sdk.Comm.BTClassicConnection.Read().

◆ Close()

◆ Connected()

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

Check if connection is established.

返回
true if connection is established, otherwise false.

实现了 com.UniPRT.Sdk.Comm.IComm.

引用了 com.UniPRT.Sdk.Comm.BTClassicConnection.btSocket.

◆ Descriptor()

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

Get the string descriptor of the connection.

返回
String descriptor of the connection.

实现了 com.UniPRT.Sdk.Comm.IComm.

引用了 com.UniPRT.Sdk.Comm.BTClassicConnection._bt_address.

◆ DescriptorValidate()

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

验证 descriptorHint 中的描述字符串是否有效,可能包含或不包含 v_id 和 p_id。返回 long 值。

可接受的 descriptorHint 格式:

  • "BT:MAC Address:<_bt_mac>"
    • _bt_mac 是十六进制字符串,例如:MAC Address:0080A3123456

被这些函数引用 com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection().

◆ Open()

◆ Read()

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

Read all available bytes.

返回
Array of bytes read.

实现了 com.UniPRT.Sdk.Comm.IComm.

引用了 com.UniPRT.Sdk.Comm.BTClassicConnection.BytesAvailable() , 以及 com.UniPRT.Sdk.Comm.BTClassicConnection.InStream.

◆ Write()

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

Write all bytes from the array passed in.

参数
dataOutByte array to write.

实现了 com.UniPRT.Sdk.Comm.IComm.

引用了 com.UniPRT.Sdk.Comm.BTClassicConnection.BLOCK_SIZE , 以及 com.UniPRT.Sdk.Comm.BTClassicConnection.OutStream.

类成员变量说明

◆ _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

该类的文档由以下文件生成:
com.UniPRT.Sdk.Comm
Definition: AComm.java:1
com.UniPRT
com.UniPRT.Sdk.Comm.BTClassicConnection
实现蓝牙经典(Bluetooth Classic)通信的类,并继承自 AComm 类。
Definition: BTClassicConnection.java:36
com.UniPRT.Sdk
com
com.UniPRT.Sdk.Comm.BTClassicConnection.BTClassicConnection
BTClassicConnection(String descriptor)
根据 descriptor 字符串初始化新实例。
Definition: BTClassicConnection.java:129