Commit 05019768 authored by 王雷's avatar 王雷

修改打印库

parent 4ac27dea
...@@ -55,8 +55,6 @@ dependencies { ...@@ -55,8 +55,6 @@ dependencies {
implementation 'com.google.android.material:material:1.3.0-alpha01' implementation 'com.google.android.material:material:1.3.0-alpha01'
implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation project(path: ':printer') implementation project(path: ':printer')
// todo test
// implementation 'com.android.tools.build:gradle:3.1.1' // implementation 'com.android.tools.build:gradle:3.1.1'
// implementation 'com.android.tools.lint:lint-gradle:26.1.1' // implementation 'com.android.tools.lint:lint-gradle:26.1.1'
......
...@@ -91,7 +91,7 @@ class MainActivityPtinter : AppCompatActivity(), PrintConnOrDisCallBack { ...@@ -91,7 +91,7 @@ class MainActivityPtinter : AppCompatActivity(), PrintConnOrDisCallBack {
} }
} }
tv_connect_wifi.setOnClickListener { tv_connect_wifi.setOnClickListener {
PrintManagerUtils.getInstance().wifiConnect("192.168.10.165", PrintManagerUtils.getInstance().wifiConnect("192.168.10.200",
object : OnConnectCallBack { object : OnConnectCallBack {
override fun onConnectStart() { override fun onConnectStart() {
Log.d(TAG, "onConnectStart: ") Log.d(TAG, "onConnectStart: ")
......
...@@ -51,7 +51,7 @@ repositories { ...@@ -51,7 +51,7 @@ repositories {
} }
group 'com.qmai.android.print' group 'com.qmai.android.print'
version '1.1.38-SNAPSHOT' version '1.1.43-SNAPSHOT'
gradlePublish { gradlePublish {
......
...@@ -167,6 +167,8 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() { ...@@ -167,6 +167,8 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
mStatus = true mStatus = true
// name = it.productName // name = it.productName
name = it.name name = it.name
address = it.address
deviceId = it.address
/* if (name.isNullOrEmpty()) { /* if (name.isNullOrEmpty()) {
name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机" name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"
}*/ }*/
......
...@@ -166,7 +166,7 @@ class DeviceManagerUtils { ...@@ -166,7 +166,7 @@ class DeviceManagerUtils {
* 通知观察者们,该设备添加了 * 通知观察者们,该设备添加了
* ***/ * ***/
private fun notifyaddObserver(deviceManager: DeviceManager) { private fun notifyaddObserver(deviceManager: DeviceManager) {
// Log.d(TAG, "notifyaddObserver: ${Log.getStackTraceString(Throwable())}") // Log.d(TAG, "notifyaddObserver: ${Log.getStackTraceString(Throwable())}")
mCallBacklist?.forEach { mCallBacklist?.forEach {
it.onConectPrint(deviceManager) it.onConectPrint(deviceManager)
} }
...@@ -204,14 +204,18 @@ class DeviceManagerUtils { ...@@ -204,14 +204,18 @@ class DeviceManagerUtils {
if (lists.isNullOrEmpty()) { if (lists.isNullOrEmpty()) {
return false return false
} }
lists.forEach { return lists.any {
if (it is BlueDeviceManager) { it is BlueDeviceManager && it.address == address
if (it.address == address) { }
return true }
}
} //自己传条件判断设备是否已经连接
fun isContainerDevice(predicate: (DeviceManager) -> Boolean): Boolean {
if (lists.isNullOrEmpty()) {
return false
} else {
return lists.any(predicate)
} }
return false
} }
//移除所有已连接蓝牙设备 主要使用于广播检测到蓝牙开关关掉 //移除所有已连接蓝牙设备 主要使用于广播检测到蓝牙开关关掉
...@@ -286,13 +290,18 @@ class DeviceManagerUtils { ...@@ -286,13 +290,18 @@ class DeviceManagerUtils {
} }
return tempList return tempList
} }
//通过deviceId获取设备 //通过deviceId获取设备
fun getDeviceAccordDeviceId(deviceId: String): DeviceManager? { fun getDeviceAccordDeviceId(deviceId: String): DeviceManager? {
if (lists.isNullOrEmpty()) { if (lists.isNullOrEmpty()) {
return null return null
} }
val device = lists.find { it.deviceId == deviceId} val device = lists.find { it.deviceId == deviceId }
return if (device != null) (device as DeviceManager) else null return if (device != null) (device as DeviceManager) else null
} }
} }
\ No newline at end of file
package zs.qimai.com.printer2.manager package zs.qimai.com.printer2.manager
import android.util.Log import android.util.Log
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import zs.qimai.com.printer2.callback.OnConnectCallBack import zs.qimai.com.printer2.callback.OnConnectCallBack
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.Companion.ESC_COMMAND
import java.io.IOException import java.io.IOException
import java.net.SocketException import java.net.SocketException
import java.util.* import java.util.*
...@@ -44,18 +42,20 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager() ...@@ -44,18 +42,20 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
mPrintMode = status ?: ESC mPrintMode = status ?: ESC
mStatus = true mStatus = true
name = deviceId name = deviceId
address = deviceId
if (name.isNullOrEmpty()) { if (name.isNullOrEmpty()) {
name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机" name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"
} }
DeviceManagerUtils.getInstance().addDevice(this@WifiDeviceManager) DeviceManagerUtils.getInstance().addDevice(this@WifiDeviceManager)
Log.d(TAG, "searchResult: WifiDeviceManager success") Log.d(TAG, "searchResult: WifiDeviceManager success")
mConnectCallBack?.onConnectSuccess(this@WifiDeviceManager) mConnectCallBack?.onConnectSuccess(this@WifiDeviceManager)
timeDetachStatus()
} }
} }
}.queryStatus() }.queryStatus()
} else { } else {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
DeviceManagerUtils.getInstance().removeDevice(this@WifiDeviceManager)
mConnectCallBack?.onConnectError(0, "连接失败") mConnectCallBack?.onConnectError(0, "连接失败")
} }
} }
...@@ -97,4 +97,57 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager() ...@@ -97,4 +97,57 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
companion object { companion object {
private const val TAG = "WifiDeviceManager" private const val TAG = "WifiDeviceManager"
} }
override fun equals(other: Any?): Boolean {
if (other == null || other !is WifiDeviceManager) {
return false
} else {
return other.ip == this.ip
}
}
override fun hashCode(): Int {
return ip.hashCode()
}
/***
* 定时检查打印机状态 原理是ping ip地址 与往打印机写指令看是否能写通
*/
private fun timeDetachStatus() {
var job: Job? = null
job = GlobalScope.launch(Dispatchers.IO) {
while (true) {
delay(1000)
try {
Log.d(TAG, "timeDetachStatus: send time detach success")
//ping一下能不能联通
val p = Runtime.getRuntime().exec("ping -c 3 -w 10 " + ip)
val status = p.waitFor()
//ping成功
if (status == 0) {
mOutPutStream?.write(ESC_COMMAND)
} else {
job?.cancel()
closePort()
//移除设备
DeviceManagerUtils.getInstance().removeDevice(this@WifiDeviceManager)
}
Log.d(TAG, "timeDetachStatus: status= $status")
} catch (e: IOException) {
job?.cancel()
closePort()
Log.d(TAG, "timeDetachStatus: e= ${e.message}")
val ip = this@WifiDeviceManager.ip
//移除设备
DeviceManagerUtils.getInstance().removeDevice(this@WifiDeviceManager)
//this@WifiDeviceManager.openPort()
}
}
}
}
} }
\ No newline at end of file
...@@ -26,10 +26,10 @@ class BlueToothStatusReceiver : BroadcastReceiver() { ...@@ -26,10 +26,10 @@ class BlueToothStatusReceiver : BroadcastReceiver() {
DeviceManagerUtils.getInstance().removeBtDevice(it) DeviceManagerUtils.getInstance().removeBtDevice(it)
} }
} }
BluetoothAdapter.ACTION_STATE_CHANGED ->{ BluetoothAdapter.ACTION_STATE_CHANGED -> {
val blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0) val blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)
//判断蓝牙开关是否关闭了 //判断蓝牙开关是否关闭了
if (blueState==BluetoothAdapter.STATE_TURNING_OFF){ if (blueState == BluetoothAdapter.STATE_TURNING_OFF) {
Log.d(TAG, "onReceive: bluetooth enable closed") Log.d(TAG, "onReceive: bluetooth enable closed")
DeviceManagerUtils.getInstance().removeAllBtDevice() DeviceManagerUtils.getInstance().removeAllBtDevice()
} }
......
...@@ -250,6 +250,13 @@ class PrintManagerUtils { ...@@ -250,6 +250,13 @@ class PrintManagerUtils {
//网口连接 //网口连接
fun wifiConnect(ip: String, callback: OnConnectCallBack? = null, port: Int = 9100) { fun wifiConnect(ip: String, callback: OnConnectCallBack? = null, port: Int = 9100) {
val status = DeviceManagerUtils.getInstance().isContainerDevice {
it is WifiDeviceManager && it.ip == ip
}
if (status) {
callback?.onConnectError(BT_DEVICE_ALREAD_CONN, "该设备已经连接过")
return
}
WifiDeviceManager(ip, port).apply { WifiDeviceManager(ip, port).apply {
mConnectCallBack = callback mConnectCallBack = callback
...@@ -263,10 +270,11 @@ class PrintManagerUtils { ...@@ -263,10 +270,11 @@ class PrintManagerUtils {
fun connectSunmiInnerBluePrint(onBtConnectCallBack: OnBtConnectCallBack? = null) { fun connectSunmiInnerBluePrint(onBtConnectCallBack: OnBtConnectCallBack? = null) {
val brand = Build.BRAND val brand = Build.BRAND
val model = Build.MODEL val model = Build.MODEL
Log.d(TAG, "connectSunmiInnerBluePrint: model= ${model}")
//商米品牌&&(T系列||D2s系列 D2mini)) //商米品牌&&(T系列||D2s系列 D2mini))
if (brand == "SUNMI" && if (brand == "SUNMI" &&
(model.contains("T2") ||model.contains("t2")|| model.contains("t1host") || model.contains("D2s") || model.contains( (model.contains("T2") || model.contains("t2") || model.contains("t1host") || model.contains(
"D2s"
) || model.contains(
"D2mini" "D2mini"
)) ))
) { ) {
......
...@@ -22,7 +22,7 @@ android { ...@@ -22,7 +22,7 @@ android {
} }
group 'com.qmai.android.sdk' group 'com.qmai.android.sdk'
version '2.1.1-SNAPSHOT' version '2.1.2-SNAPSHOT'
gradlePublish { gradlePublish {
......
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