gHIKFoWNMR xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
AndroidPrinterSDK 提供通过 Bluetooth、WIFI、USB 三种方式和打印机进行连接的方法。 具体调用的方法详见 AndroidPrinterDemo 中 com.android.print.demo 包下的 BluetoothOperation.java、WifiOperation.java、UsbOperation.java 三个类中。
以 Bluetooth 为例 :
PrinterInstance mPrinter=new BluetoothPort().btConnnect(mContext, mac, adapter, mHandler);
1.adapter 为蓝牙适配器。
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
2.mac 为目标设备的蓝牙地址,可通过扫描附近蓝牙的方式获得。
adapter.startDiscovery();
扫描前需注册 BroadcastReceiver ,详见 BluetoothDeviceList 中 mReceiver 的注册与使用,当扫描到周围设备时,device.getAddress() 即可获得。
3.mHandler 为消息接收器。需自定义。详见 MainActivity.java 中的 mHandler。 设定此 handler 的作用时,当连接成功时,SDK 会发送固定消息给此 handler,以便识别连接状态和更新UI。
1.AndroidPrinterSDK 提供的打印功能包括 打印文本,打印图片,打印表格,打印条码。
以上功能均需由连接成功的打印机实例调用。 如 mPrinter.printText();mPrinter.printImage(); mPrinter.printTable();mPrinter.printBarCode(); 以此为基础可拓展的功能包括,打印文件,打印画布,打印小票等。详见 AndroidPrinterDemo 中 com.android.print.demo.utils 包下 PrintUtils.java 。
2.打印流程有两种。
(1).连接 - 打印 - 打印 - … - 关闭 . 优点是打印间隔时间短,打印速度快,但如果有需求,实现蓝牙断线监测,蓝牙自动回连等功能需手动实现。
(2).连接 - 打印 - 关闭 连接 - 打印 - 关闭 . 优点是可避免蓝牙断线监测,自动回连等功能的实现,但打印间隔时间长,打印速度较慢。
1. 蓝牙断线监测的实现方式有两种。
(1).利用 Android 底层提供的广播接收器 。
详见 BluetoothOpreation.java 中的 myReceiver 的注册和使用。此种方法的原理为,Android 底层每隔20秒会进行一次蓝牙连接状态的检测。如果发现蓝牙断开,会进行广播。该注册器就能收到对应的广播信息。从而实现蓝牙断线的监测。优点是实现方法简单,但缺点是状态监测不精确。监测到断线的时间取决于断线的时间点。比如,若在上一次蓝牙检测之后1秒断线,则监测到蓝牙断线需要到20秒之后;若在下一次蓝牙检测之前1秒断线,则监测到蓝牙断线只需要到1秒之后。
<2>(2). 利用自定义心跳线程实现。
此种方法的原理为,在蓝牙连接成功之后,启动一个心跳线程。该线程可调用 AndroidPrinterSDK 提供的 Utils.getPrinterStatus() 方法。该方法向打印机发送指令以获得打印机当前的状态。返回值中:0表示状态正常,1表示缺纸,2表示上盖打开,3表示未读到值。故若返回值为3,则可判定蓝牙已经断线。另此方法可传递超时参数,以此控制心跳线程的频率。优点是状态监测精度可调整,缺点是若心跳线程频率太短,消耗的资源会很大。
2.蓝牙自动回连的方式.
参考断线监测的心跳线程方式。不同点在于,监测到蓝牙断线之后,调用的是连接函数进行尝试连接。若连接成功,表明可回连。此种方式消耗资源也较大,一般若对蓝牙自动回连有较高需求时,建议采用打印流程的第二种进行避免。
AndroidPrinterSDK 提供保存与提取蓝牙地址的方法。当检测到蓝牙绑定或连接成功之后,可调用Utils.saveBtConnInfo()方法,将蓝牙地址传入保存。下一次连接前调用Utils.getBtConnInfo(),即可取得保存的蓝牙地址,省去蓝牙扫描与选择设备的时间。保存的地址关闭APP后重新进入时依然可以取得。但需注意,最多只能保存一个蓝牙地址,即每一次保存蓝牙地址,都会覆盖上一次保存的地址。
Connection
Theory and Sample Code
AndroidPrinterSDK supplies three ways ?C Bluetooth, WIFI, USB to connect with the printer. The detailed methods can be referred to three CLASS ?C “BluetoothOperation.java”, “WifiOperation.java”, “UsbOperation.java” in “com.android.print.demo” of “AndroidPrinterDemo”.
PrinterInstance mPrinter=new BluetoothPort().btConnnect(mContext, mac, adapter, mHandler);
mContext is the object of current context. Type is “Context”
1. Adapter is Bluetooth Adapter
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
2. mac is the Bluetooth address of target device. It can be gained by scanning the nearby Bluetooth.
adapter.startDiscovery();
BroadcastReceiver needs to be registered before scanning. Pls refer to the details of registration and using of “myReceiver” in “BluetoothOpreation.java”. When the surrounding device is scanned, device.getAddress() can be gained.
3. mHandler is message receiver. It needs to be user-defined. The details can be checked in “mHandler” of “MainActivity.java”. After the function of this “handler: is set, when the connection is successfully, SDK will send the confirmed message to this “handler to recognize the connection status and update UI.
Printing Theory and Sample Code
1. All the above functions need to be called on the successfully connected printer.
e.g. mPrinter.printText();mPrinter.printImage();
mPrinter.printTable();mPrinter.printBarCode();
Basing on this code, extended functions can be: printing document, printing image, printing receipt, etc.
The detail refers: the “PrintUtils.java” of package “com.android.print.demo.utils” in of “AndroidPrinterDemo”.
2. There are two methods to control printing print work.
(1). Connect-print-print-…...-close. In this mode, the advantage is shorter interval and faster printing speed of print but disadvantage is the functions of “Bluetooth Disconnection Monitor Connecting Watch” and “Automatic Reconnection” have to be set by manual.
(2). Connect ?C print - close, connect ?C print ?C close. In this mode, the advantage is needn’t control the functions of “Bluetooth Disconnection Monitor connecting watch” and “Automatic Reconnection ” are not needed but disadvantage is longer interval and slower printing speed.
Bluetooth
Disconnection Monitoring and Automatic Reconnection
1. Two ways to monitor the bluetooth
disconnection
(1). Use the “broadcast receiver” which
it provided by Android underlying.
Pls refer to
the details of registration and using of “myReceiver”
in “BluetoothOpreation.java”. In this method, Android
underlying detects the Bluetooth connection status once per 20seconds. If the
Bluetooth is unconnected, it will broadcast. This receiver will receive the
related broadcast message; while the Bluetooth Disconnection Monitoring is
implemented.
The advantage of this method is simple but
disadvantage is inaccuracy. The detected disconnection time depends on the
point of unconnected time. For example, if the disconnection happens after 1
second of the last detection, it has to be monitored 20seconds later; if the
disconnection happens before 1 second of the last detection, it will be
monitored only 1 second later.
(2). Use the user-defined Heartbeat
Daemon to implementation.
In this method, after the Bluetooth is
connected successfully, launch one heartbeat daemon. In this daemon, the method
of “Utils.getPrinterStatus() ” supplied in AndroidPrinterSDK
can be called. Using this method to send commands to the printer and get the
current status of printer. For returned values: 0 means the normal status, 1
means out of paper, 2 means the upper cover is open, 3 means no value is read.
If the returned value is 3, it can be confirmed the Bluetooth has been
disconnected. This method can transmit overtime parameter to control the
frequency of heartbeat daemon. The advantage is the monitor precision can be adjustable;
while the disadvantage is the frequency of heartbeat daemon is too short and
the wasting resources will be much.
2. The method of Bluetooth
Automatic Reconnecting.
Refer to the heartbeat daemon method of Bluetooth
disconnection monitor. The difference is that the connection function is called
to try the connection after the Bluetooth disconnection is monitored. If the
connection is successful, it means the reconnection can be ok. The wasting
resources of this method are also much. If there is high request for Bluetooth
reconnection, this second printing method is suggested to use to save the
resource.
Bluetooth quick connect
Note:
only one MAC address can be saved. When new address is saved, the old one will be covered.