Commit 2f8ae2e8 authored by 王雷's avatar 王雷

打印管理增加各种状态回调

parent 7c90626b
...@@ -51,7 +51,7 @@ repositories { ...@@ -51,7 +51,7 @@ repositories {
} }
group 'com.qmai.android.print' group 'com.qmai.android.print'
version '1.1.60-SNAPSHOT' version '1.1.71-SNAPSHOT'
gradlePublish { gradlePublish {
......
package zs.qimai.com.printer2
import zs.qimai.com.printer2.manager.DeviceManager
/**
* @author: leiwang
* @time: 2021/12/9 5:15 下午
* @desc:
*/
data class PrintDeviceStatus(
val type: Int,
val deviceManager: DeviceManager?,
val transferLink: String? = null,
var others: String? = null
) {
fun getTypeDesc(): String {
return when (type) {
CONNECT_SUCCESS -> {
"打印机连接成功"
}
CONNECT_FAILED -> {
"打印机连接失败"
}
PRINT_ERROR -> {
"打印错误"
}
PRINT_CLOSED -> {
"打印机断开"
}
BLUETOOTH_CLOSED_RECEIVER -> {
"广播通知蓝牙打印机断开"
}
USER_CANCEL_CONNECTED -> "用户主动取消连接"
else -> {
""
}
}
}
companion object {
const val CONNECT_SUCCESS = 1
const val CONNECT_FAILED = 2
//打印异常
const val PRINT_ERROR = 3
const val PRINT_CLOSED = 4
//广播收到蓝牙打印机断开
const val BLUETOOTH_CLOSED_RECEIVER = 5
//用户主动取消连接
const val USER_CANCEL_CONNECTED = 6
//设备重复添加
const val PRINT_REPEAT_ADD = 7
}
}
\ No newline at end of file
package zs.qimai.com.printer2.callback
import zs.qimai.com.printer2.PrintDeviceStatus
/**
* @author: leiwang
* @time: 2021/12/9 3:22 下午
* @desc:
*/
interface PrintStatusChangeCallBack {
fun onChange(printDeviceStatus: PrintDeviceStatus)
}
\ No newline at end of file
...@@ -8,6 +8,7 @@ import android.util.Log ...@@ -8,6 +8,7 @@ import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import kotlinx.coroutines.* import kotlinx.coroutines.*
import zs.qimai.com.printer2.PrintDeviceStatus
import zs.qimai.com.printer2.printStatus.PrintStatusCallBack import zs.qimai.com.printer2.printStatus.PrintStatusCallBack
import zs.qimai.com.printer2.printStatus.PrinterStatusUtils import zs.qimai.com.printer2.printStatus.PrinterStatusUtils
import zs.qimai.com.printer2.utils.PrintFormat import zs.qimai.com.printer2.utils.PrintFormat
...@@ -91,8 +92,8 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -91,8 +92,8 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
mBlueToothDevice?.let { it -> mBlueToothDevice?.let { it ->
try { try {
Log.d(TAG, "connectBt: begin ") Log.d(TAG, "connectBt: begin ")
mBluetoothSocket = it.createInsecureRfcommSocketToServiceRecord(uuid) // mBluetoothSocket = it.createInsecureRfcommSocketToServiceRecord(uuid)
mBluetoothSocket = it.createRfcommSocketToServiceRecord(uuid)
if (mBluetoothSocket != null) { if (mBluetoothSocket != null) {
try { try {
//目前已知 Android 7版本这里必须要单独开个线程才能连接 fuck //目前已知 Android 7版本这里必须要单独开个线程才能连接 fuck
...@@ -121,10 +122,18 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -121,10 +122,18 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
DeviceManagerUtils.getInstance() DeviceManagerUtils.getInstance()
.addDevice(this@BlueDeviceManager) .addDevice(this@BlueDeviceManager)
mOnBtConnectCallBack?.onConnectSuccess(this@BlueDeviceManager) mOnBtConnectCallBack?.onConnectSuccess(this@BlueDeviceManager)
//回调通知
val printDeviceStatus = PrintDeviceStatus(
PrintDeviceStatus.CONNECT_SUCCESS,
this@BlueDeviceManager,
Log.getStackTraceString(Throwable())
)
DeviceManagerUtils.getInstance()
.notifyPrintStatusChangeCallBack(printDeviceStatus)
//回调完成 赋值为Null //回调完成 赋值为Null
mOnBtConnectCallBack = null mOnBtConnectCallBack = null
//mUsbPrintConnCallBack?.onConnSucess(this@UsbDeviceManager) //mUsbPrintConnCallBack?.onConnSucess(this@UsbDeviceManager)
connectSuccessNotify()
Log.d(TAG, "searchResult: status= $status") Log.d(TAG, "searchResult: status= $status")
/* status?.let { /* status?.let {
handleConnectSuccess(status) handleConnectSuccess(status)
...@@ -135,6 +144,10 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -135,6 +144,10 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
} }
} catch (e: IOException) { } catch (e: IOException) {
Log.d(TAG, "connectBt: e= $e") Log.d(TAG, "connectBt: e= $e")
name = it.name
address = it.address
deviceId = it.address
connectFailedNotify(e.message)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
mOnBtConnectCallBack?.onConnectError( mOnBtConnectCallBack?.onConnectError(
PrintManagerUtils.SOCKET_ERROR, PrintManagerUtils.SOCKET_ERROR,
...@@ -147,6 +160,7 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -147,6 +160,7 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
mStatus = false mStatus = false
} }
} else { } else {
connectFailedNotify("获取不到socket")
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
mOnBtConnectCallBack?.onConnectError( mOnBtConnectCallBack?.onConnectError(
PrintManagerUtils.SOCKET_NOT_FOUND, PrintManagerUtils.SOCKET_NOT_FOUND,
...@@ -156,7 +170,9 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -156,7 +170,9 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
mOnBtConnectCallBack = null mOnBtConnectCallBack = null
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
connectFailedNotify(e.message)
Log.d(TAG, "openPort: e= ${e}") Log.d(TAG, "openPort: e= ${e}")
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
mOnBtConnectCallBack?.onConnectError( mOnBtConnectCallBack?.onConnectError(
...@@ -210,11 +226,14 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -210,11 +226,14 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
}*/ }*/
//mOnBtConnectCallBack?.onConnectError(DEVICE_NOT_FIND, "根据地址获取不到设备") //mOnBtConnectCallBack?.onConnectError(DEVICE_NOT_FIND, "根据地址获取不到设备")
} }
} }
} }
override fun closePort() { override fun closePort() {
printCloseNotify("")
DeviceManagerUtils.getInstance().removeDevice(this@BlueDeviceManager) DeviceManagerUtils.getInstance().removeDevice(this@BlueDeviceManager)
mStatus = false mStatus = false
job?.cancel() job?.cancel()
...@@ -279,8 +298,13 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -279,8 +298,13 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
} }
override fun writeData(bytes: ByteArray) { override fun writeData(bytes: ByteArray) {
this.mOutPutStream?.write(bytes) try {
this.mOutPutStream?.flush() this.mOutPutStream?.write(bytes)
this.mOutPutStream?.flush()
} catch (e: Exception) {
printErrorNotify(e.message)
throw e
}
} }
/**** /****
...@@ -362,9 +386,9 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -362,9 +386,9 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
return false return false
} }
override fun toString(): String { /* override fun toString(): String {
return "BlueDeviceManager(mType=$mType, len=$len, mBlueToothDevice=$mBlueToothDevice, mBlueAdapter=$mBlueAdapter, mBluetoothSocket=$mBluetoothSocket, mOnBtConnectCallBack=$mOnBtConnectCallBack, TAG='$TAG', disposable=$disposable, uuid=$uuid, job=$job)" return "BlueDeviceManager(蓝牙, 打印机名称 = $name 打印机模式 = ${if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"} 票据大小 = ${mPrintSize} deviceId = ${deviceId} " + super.toString()
} }*/
} }
\ No newline at end of file
package zs.qimai.com.printer2.manager package zs.qimai.com.printer2.manager
import android.util.Log
import zs.qimai.com.printer2.PrintDeviceStatus
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
import java.lang.StringBuilder
/**** /****
* 设备基类 * 设备基类
...@@ -16,20 +19,27 @@ abstract class DeviceManager { ...@@ -16,20 +19,27 @@ abstract class DeviceManager {
var mPrintMode: Int? = null var mPrintMode: Int? = null
abstract var mType: Int abstract var mType: Int
var name: String? = null var name: String? = null
@Deprecated("后续用 mPrintSize 代替打印杯贴大小") @Deprecated("后续用 mPrintSize 代替打印杯贴大小")
var maxLineChars: Int = 80 var maxLineChars: Int = 80
var mPrintSize:Int = -1 var mPrintSize: Int = -1
companion object { companion object {
//小票模式 //小票模式
val ESC = 1 val ESC = 1
//标签模式 //标签模式
val TSC = 2 val TSC = 2
//蓝牙 //蓝牙
val BT = 1 val BT = 1
//usb //usb
val USB = 2 val USB = 2
//wifi(网线) //wifi(网线)
const val WIFI = 3 const val WIFI = 3
//支持小票尺寸 //支持小票尺寸
const val TSC_40_30 = 10 const val TSC_40_30 = 10
const val TSC_40_40 = 11 const val TSC_40_40 = 11
...@@ -59,5 +69,63 @@ abstract class DeviceManager { ...@@ -59,5 +69,63 @@ abstract class DeviceManager {
return deviceId.hashCode() return deviceId.hashCode()
} }
override fun toString(): String {
val str =
"打印机名称: $name\n 打印机连接方式: ${
when (mType) {
BT -> "蓝牙"
WIFI -> "网口"
USB -> "USB"
else -> "未知"
}
} \n 打印机模式: ${if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"}\n deviceId: $deviceId\n address: $address\n 票据大小: $mPrintSize"
return str
}
fun connectSuccessNotify() {
//回调通知
val printDeviceStatus = PrintDeviceStatus(
PrintDeviceStatus.CONNECT_SUCCESS,
this,
Log.getStackTraceString(Throwable())
)
DeviceManagerUtils.getInstance()
.notifyPrintStatusChangeCallBack(printDeviceStatus)
}
fun connectFailedNotify(msg: String?) {
//回调通知
val printDeviceStatus = PrintDeviceStatus(
PrintDeviceStatus.CONNECT_FAILED,
this, Log.getStackTraceString(Throwable()),
"错误原因: $msg"
)
DeviceManagerUtils.getInstance()
.notifyPrintStatusChangeCallBack(printDeviceStatus)
}
//打印异常
fun printErrorNotify(msg: String?) {
//回调通知
val printDeviceStatus = PrintDeviceStatus(
PrintDeviceStatus.PRINT_ERROR,
this, Log.getStackTraceString(Throwable()),
"错误原因: $msg"
)
DeviceManagerUtils.getInstance()
.notifyPrintStatusChangeCallBack(printDeviceStatus)
}
//打印机关闭
fun printCloseNotify(msg: String) {
//回调通知
val printDeviceStatus = PrintDeviceStatus(
PrintDeviceStatus.PRINT_CLOSED,
this, Log.getStackTraceString(Throwable()),
"错误原因: $msg"
)
DeviceManagerUtils.getInstance()
.notifyPrintStatusChangeCallBack(printDeviceStatus)
}
} }
\ No newline at end of file
...@@ -3,7 +3,12 @@ package zs.qimai.com.printer2.manager ...@@ -3,7 +3,12 @@ package zs.qimai.com.printer2.manager
import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothDevice
import android.hardware.usb.UsbDevice import android.hardware.usb.UsbDevice
import android.util.Log import android.util.Log
import zs.qimai.com.printer2.PrintDeviceStatus
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.CONNECT_FAILED
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.PRINT_CLOSED
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.PRINT_REPEAT_ADD
import zs.qimai.com.printer2.callback.PrintConnOrDisCallBack import zs.qimai.com.printer2.callback.PrintConnOrDisCallBack
import zs.qimai.com.printer2.callback.PrintStatusChangeCallBack
import zs.qimai.com.printer2.manager.DeviceManager.Companion.BT import zs.qimai.com.printer2.manager.DeviceManager.Companion.BT
import zs.qimai.com.printer2.manager.DeviceManager.Companion.USB import zs.qimai.com.printer2.manager.DeviceManager.Companion.USB
import zs.qimai.com.printer2.utils.PrintManagerUtils import zs.qimai.com.printer2.utils.PrintManagerUtils
...@@ -19,10 +24,22 @@ class DeviceManagerUtils { ...@@ -19,10 +24,22 @@ class DeviceManagerUtils {
private val TAG = "DeviceManagerUtils" private val TAG = "DeviceManagerUtils"
private var mCallBackList: CopyOnWriteArrayList<PrintConnOrDisCallBack> = CopyOnWriteArrayList() private var mCallBackList: CopyOnWriteArrayList<PrintConnOrDisCallBack> = CopyOnWriteArrayList()
//打印机状态监听
private var mPrintStatusChangeCallBackList: CopyOnWriteArrayList<PrintStatusChangeCallBack> =
CopyOnWriteArrayList()
var lists: CopyOnWriteArrayList<DeviceManager> = CopyOnWriteArrayList() var lists: CopyOnWriteArrayList<DeviceManager> = CopyOnWriteArrayList()
internal fun addDevice(deviceManager: DeviceManager?) { internal fun addDevice(deviceManager: DeviceManager?) {
deviceManager?.let { deviceManager?.let {
//避免重复添加
if (lists.contains(it)) {
val printDeviceStatus = PrintDeviceStatus(
PRINT_REPEAT_ADD, it, Log.getStackTraceString(
Throwable()
)
)
DeviceManagerUtils.getInstance().notifyPrintStatusChangeCallBack(printDeviceStatus)
return
}
lists.add(deviceManager) lists.add(deviceManager)
notifyaddObserver(it) notifyaddObserver(it)
} }
...@@ -46,11 +63,18 @@ class DeviceManagerUtils { ...@@ -46,11 +63,18 @@ class DeviceManagerUtils {
// lists.remove(deviceManager) // lists.remove(deviceManager)
} }
internal fun removeUsbDevice(device: UsbDevice) { private fun removeUsbDevice(device: UsbDevice) {
lists.forEach { lists.forEach {
if (it is UsbDeviceManager) { if (it is UsbDeviceManager) {
if (it.usbDevice == device) { if (it.usbDevice == device) {
//回调通知
val printDeviceStatus = PrintDeviceStatus(
PRINT_CLOSED, it,
Log.getStackTraceString(Throwable())
)
getInstance().notifyPrintStatusChangeCallBack(printDeviceStatus)
//closePort()会清除所有配置项,并调用removeDevice() //closePort()会清除所有配置项,并调用removeDevice()
it.closePort() it.closePort()
return@forEach return@forEach
...@@ -152,6 +176,36 @@ class DeviceManagerUtils { ...@@ -152,6 +176,36 @@ class DeviceManagerUtils {
} }
/***
* 添加状态监听
* **/
fun addPrintStatusCallBack(callBack: PrintStatusChangeCallBack) {
mPrintStatusChangeCallBackList.add(callBack)
}
/****
* 移除状态监听
* **/
fun removePrintStatusCallBack(callBack: PrintStatusChangeCallBack) {
mPrintStatusChangeCallBackList.remove(callBack)
}
fun notifyPrintDeviceStatusOnDisConnectObserver(
printDeviceStatus: PrintDeviceStatus
) {
mPrintStatusChangeCallBackList.forEach {
it.onChange(printDeviceStatus)
}
}
fun notifyPrintStatusChangeCallBack(printDeviceStatus: PrintDeviceStatus) {
mPrintStatusChangeCallBackList.forEach {
it.onChange(printDeviceStatus)
}
}
/**** /****
* 通知观察者们,该设备移除了 * 通知观察者们,该设备移除了
* ***/ * ***/
...@@ -297,7 +351,7 @@ class DeviceManagerUtils { ...@@ -297,7 +351,7 @@ class DeviceManagerUtils {
return null return null
} }
val device = lists.find { it.deviceId == deviceId } val device = lists.find { it.deviceId == deviceId }
return if (device != null) device else null return device
} }
......
...@@ -2,6 +2,10 @@ package zs.qimai.com.printer2.manager ...@@ -2,6 +2,10 @@ package zs.qimai.com.printer2.manager
import android.hardware.usb.* import android.hardware.usb.*
import android.util.Log import android.util.Log
import zs.qimai.com.printer2.PrintDeviceStatus
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.CONNECT_SUCCESS
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.PRINT_CLOSED
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.PRINT_ERROR
import zs.qimai.com.printer2.callback.UsbPrintConnCallBack import zs.qimai.com.printer2.callback.UsbPrintConnCallBack
import zs.qimai.com.printer2.printStatus.PrintStatusCallBack import zs.qimai.com.printer2.printStatus.PrintStatusCallBack
import zs.qimai.com.printer2.printStatus.PrinterStatusUtils import zs.qimai.com.printer2.printStatus.PrinterStatusUtils
...@@ -59,11 +63,16 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() { ...@@ -59,11 +63,16 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() {
} }
DeviceManagerUtils.getInstance().addDevice(this@UsbDeviceManager) DeviceManagerUtils.getInstance().addDevice(this@UsbDeviceManager)
mUsbPrintConnCallBack?.onConnSucess(this@UsbDeviceManager) mUsbPrintConnCallBack?.onConnSucess(this@UsbDeviceManager)
//回调通知
connectSuccessNotify()
} }
} }
}.queryStatus() }.queryStatus()
} else { } else {
connectFailedNotify("连接失败")
mUsbPrintConnCallBack?.onConnFailed(USB_CONN_FAILED, "连接失败") mUsbPrintConnCallBack?.onConnFailed(USB_CONN_FAILED, "连接失败")
} }
...@@ -72,6 +81,8 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() { ...@@ -72,6 +81,8 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() {
} }
override fun closePort() { override fun closePort() {
//回调通知
printCloseNotify("")
mStatus = false mStatus = false
mUsbPrintConnCallBack = null mUsbPrintConnCallBack = null
DeviceManagerUtils.getInstance().removeDevice(this@UsbDeviceManager) DeviceManagerUtils.getInstance().removeDevice(this@UsbDeviceManager)
...@@ -104,7 +115,7 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() { ...@@ -104,7 +115,7 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() {
for (i in data.indices) { for (i in data.indices) {
if (sendData.size >= 1024) { if (sendData.size >= 1024) {
Log.e( Log.e(
TAG, TAG,
"i = " + i + "\tsendData size -> " + sendData.size + "\tdata size -> " + data.size "i = " + i + "\tsendData size -> " + sendData.size + "\tdata size -> " + data.size
) )
result += mmConnection!!.bulkTransfer( result += mmConnection!!.bulkTransfer(
...@@ -131,7 +142,8 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() { ...@@ -131,7 +142,8 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() {
Log.d(TAG, "send success") Log.d(TAG, "send success")
} }
} catch (var7: Exception) { } catch (var7: Exception) {
Log.d(TAG, "Exception occured while sending data immediately: " + var7.message) //打印异常通知
printErrorNotify(var7.message)
} }
} }
...@@ -149,9 +161,9 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() { ...@@ -149,9 +161,9 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() {
return address?.hashCode() ?: 0 + usbDevice.hashCode() return address?.hashCode() ?: 0 + usbDevice.hashCode()
}*/ }*/
override fun toString(): String { /* override fun toString(): String {
return "UsbDeviceManager(mType=$mType, TAG='$TAG', usbDevice=$usbDevice, usbManager=$usbManager, mmConnection=$mmConnection, mmIntf=$mmIntf, mmEndIn=$mmEndIn, mmEndOut=$mmEndOut, mUsbPrintConnCallBack=$mUsbPrintConnCallBack)" return "usb打印机: UsbDeviceManager(mType=$mType, TAG='$TAG', usbDevice=$usbDevice, usbManager=$usbManager, mmConnection=$mmConnection, mmIntf=$mmIntf, mmEndIn=$mmEndIn, mmEndOut=$mmEndOut, mUsbPrintConnCallBack=$mUsbPrintConnCallBack)" + super.toString()
} }*/
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
...@@ -165,6 +177,7 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() { ...@@ -165,6 +177,7 @@ class UsbDeviceManager(override var mType: Int = USB) : DeviceManager() {
override fun hashCode(): Int { override fun hashCode(): Int {
return deviceId.hashCode() return deviceId.hashCode()
} }
protected fun convertVectorByteToBytes(data: Vector<Byte>): ByteArray? { protected fun convertVectorByteToBytes(data: Vector<Byte>): ByteArray? {
val sendData = ByteArray(data.size) val sendData = ByteArray(data.size)
if (data.size > 0) { if (data.size > 0) {
......
...@@ -52,6 +52,7 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager() ...@@ -52,6 +52,7 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
//ip连接的很奇怪,长时间不操作,会自动化断开 https://www.jianshu.com/p/0f44f9e3f604 按这个教程,每次打印完断开端口连接,打印的时候重新连接在打印 //ip连接的很奇怪,长时间不操作,会自动化断开 https://www.jianshu.com/p/0f44f9e3f604 按这个教程,每次打印完断开端口连接,打印的时候重新连接在打印
mPort.closePort() mPort.closePort()
// timeDetachStatus() // timeDetachStatus()
connectSuccessNotify()
} }
} }
}.queryStatus() }.queryStatus()
...@@ -60,10 +61,10 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager() ...@@ -60,10 +61,10 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
DeviceManagerUtils.getInstance().removeDevice(this@WifiDeviceManager) DeviceManagerUtils.getInstance().removeDevice(this@WifiDeviceManager)
mConnectCallBack?.onConnectError(0, "连接失败") mConnectCallBack?.onConnectError(0, "连接失败")
} }
connectFailedNotify("连接失败")
} }
} }
} }
override fun closePort() { override fun closePort() {
...@@ -100,8 +101,10 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager() ...@@ -100,8 +101,10 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
} }
} catch (e: IOException) { } catch (e: IOException) {
printErrorNotify(e.message)
Log.d(TAG, "writeData: e=$e") Log.d(TAG, "writeData: e=$e")
} catch (e: SocketException) { } catch (e: SocketException) {
printErrorNotify(e.message)
Log.d(TAG, "writeData: e = $e") Log.d(TAG, "writeData: e = $e")
} }
} }
......
...@@ -7,6 +7,9 @@ import android.content.BroadcastReceiver ...@@ -7,6 +7,9 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
import zs.qimai.com.printer2.PrintDeviceStatus
import zs.qimai.com.printer2.PrintDeviceStatus.Companion.BLUETOOTH_CLOSED_RECEIVER
import zs.qimai.com.printer2.manager.BlueDeviceManager
import zs.qimai.com.printer2.manager.DeviceManagerUtils import zs.qimai.com.printer2.manager.DeviceManagerUtils
class BlueToothStatusReceiver : BroadcastReceiver() { class BlueToothStatusReceiver : BroadcastReceiver() {
...@@ -20,10 +23,26 @@ class BlueToothStatusReceiver : BroadcastReceiver() { ...@@ -20,10 +23,26 @@ class BlueToothStatusReceiver : BroadcastReceiver() {
} }
BluetoothDevice.ACTION_ACL_DISCONNECTED -> { BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
var bluetoothDevice = intent.getParcelableExtra<BluetoothDevice?>(EXTRA_DEVICE) val bluetoothDevice = intent.getParcelableExtra<BluetoothDevice?>(EXTRA_DEVICE)
bluetoothDevice?.let { bluetoothDevice?.let { bluetoothDevice ->
//如果检测到设备已经断开连接,就从保存的集合中删除 //如果检测到设备已经断开连接,就从保存的集合中删除
DeviceManagerUtils.getInstance().removeBtDevice(it) var deviceManager = DeviceManagerUtils.getInstance()
.getDevice { it.deviceId == bluetoothDevice.address }
if (deviceManager == null) {
deviceManager = BlueDeviceManager().apply {
address = bluetoothDevice.address
name = bluetoothDevice.name
}
}
val printDeviceStatus = PrintDeviceStatus(
BLUETOOTH_CLOSED_RECEIVER, deviceManager, Log.getStackTraceString(
Throwable()
)
)
DeviceManagerUtils.getInstance()
.notifyPrintStatusChangeCallBack(printDeviceStatus)
DeviceManagerUtils.getInstance().removeBtDevice(bluetoothDevice)
} }
} }
BluetoothAdapter.ACTION_STATE_CHANGED -> { BluetoothAdapter.ACTION_STATE_CHANGED -> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment