mwhrbDVFNt xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">

蓝牙断线监测

蓝牙断线监测的实现方式有两种。

 (1).利用 Android 底层提供的广播接收器 。

详见 BluetoothOpreation.java 中的 myReceiver 的注册和使用。此种方法的原理为,Android 底层每隔20秒会进行一次蓝牙连接状态的检测。如果发现蓝牙断开,会进行广播。该注册器就能收到对应的广播信息。从而实现蓝牙断线的监测。优点是实现方法简单,但缺点是状态监测不精确。监测到断线的时间取决于断线的时间点。比如,若在上一次蓝牙检测之后1秒断线,则监测到蓝牙断线需要到20秒之后;若在下一次蓝牙检测之前1秒断线,则监测到蓝牙断线只需要到1秒之后。

(2). 利用自定义心跳线程实现。

此种方法的原理为,在蓝牙连接成功之后,启动一个心跳线程。该线程可调用 AndroidPrinterSDK 提供的 Utils.getPrinterStatus() 方法。该方法向打印机发送指令以获得打印机当前的状态。返回值中:0表示状态正常,1表示缺纸,2表示上盖打开,3表示未读到值。故若返回值为3,则可判定蓝牙已经断线。另此方法可传递超时参数,以此控制心跳线程的频率。优点是状态监测精度可调整,缺点是若心跳线程频率太短,消耗的资源会很大。

 

 

Bluetooth Disconnection Monitoring

Two ways to monitor the bluetooth disconnection

(1). Use the “broadcast receiver” 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 user-defined Heartbeat Daemon

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.