Commit 05019768 authored by 王雷's avatar 王雷

修改打印库

parent 4ac27dea
......@@ -55,8 +55,6 @@ dependencies {
implementation 'com.google.android.material:material:1.3.0-alpha01'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation project(path: ':printer')
// todo test
// implementation 'com.android.tools.build:gradle:3.1.1'
// implementation 'com.android.tools.lint:lint-gradle:26.1.1'
......
......@@ -91,7 +91,7 @@ class MainActivityPtinter : AppCompatActivity(), PrintConnOrDisCallBack {
}
}
tv_connect_wifi.setOnClickListener {
PrintManagerUtils.getInstance().wifiConnect("192.168.10.165",
PrintManagerUtils.getInstance().wifiConnect("192.168.10.200",
object : OnConnectCallBack {
override fun onConnectStart() {
Log.d(TAG, "onConnectStart: ")
......
......@@ -51,7 +51,7 @@ repositories {
}
group 'com.qmai.android.print'
version '1.1.38-SNAPSHOT'
version '1.1.43-SNAPSHOT'
gradlePublish {
......
......@@ -167,6 +167,8 @@ class BlueDeviceManager(override var mType: Int = BT) : DeviceManager() {
mStatus = true
// name = it.productName
name = it.name
address = it.address
deviceId = it.address
/* if (name.isNullOrEmpty()) {
name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"
}*/
......
......@@ -166,7 +166,7 @@ class DeviceManagerUtils {
* 通知观察者们,该设备添加了
* ***/
private fun notifyaddObserver(deviceManager: DeviceManager) {
// Log.d(TAG, "notifyaddObserver: ${Log.getStackTraceString(Throwable())}")
// Log.d(TAG, "notifyaddObserver: ${Log.getStackTraceString(Throwable())}")
mCallBacklist?.forEach {
it.onConectPrint(deviceManager)
}
......@@ -204,14 +204,18 @@ class DeviceManagerUtils {
if (lists.isNullOrEmpty()) {
return false
}
lists.forEach {
if (it is BlueDeviceManager) {
if (it.address == address) {
return true
}
}
return lists.any {
it is BlueDeviceManager && it.address == address
}
}
//自己传条件判断设备是否已经连接
fun isContainerDevice(predicate: (DeviceManager) -> Boolean): Boolean {
if (lists.isNullOrEmpty()) {
return false
} else {
return lists.any(predicate)
}
return false
}
//移除所有已连接蓝牙设备 主要使用于广播检测到蓝牙开关关掉
......@@ -286,13 +290,18 @@ class DeviceManagerUtils {
}
return tempList
}
//通过deviceId获取设备
fun getDeviceAccordDeviceId(deviceId: String): DeviceManager? {
if (lists.isNullOrEmpty()) {
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
}
}
\ No newline at end of file
package zs.qimai.com.printer2.manager
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import zs.qimai.com.printer2.callback.OnConnectCallBack
import zs.qimai.com.printer2.printStatus.PrintStatusCallBack
import zs.qimai.com.printer2.printStatus.PrinterStatusUtils
import zs.qimai.com.printer2.utils.PrintFormat.Companion.ESC_COMMAND
import java.io.IOException
import java.net.SocketException
import java.util.*
......@@ -44,18 +42,20 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
mPrintMode = status ?: ESC
mStatus = true
name = deviceId
address = deviceId
if (name.isNullOrEmpty()) {
name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"
}
DeviceManagerUtils.getInstance().addDevice(this@WifiDeviceManager)
Log.d(TAG, "searchResult: WifiDeviceManager success")
mConnectCallBack?.onConnectSuccess(this@WifiDeviceManager)
timeDetachStatus()
}
}
}.queryStatus()
} else {
withContext(Dispatchers.Main) {
DeviceManagerUtils.getInstance().removeDevice(this@WifiDeviceManager)
mConnectCallBack?.onConnectError(0, "连接失败")
}
}
......@@ -97,4 +97,57 @@ class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager()
companion object {
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() {
DeviceManagerUtils.getInstance().removeBtDevice(it)
}
}
BluetoothAdapter.ACTION_STATE_CHANGED ->{
BluetoothAdapter.ACTION_STATE_CHANGED -> {
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")
DeviceManagerUtils.getInstance().removeAllBtDevice()
}
......
......@@ -250,6 +250,13 @@ class PrintManagerUtils {
//网口连接
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 {
mConnectCallBack = callback
......@@ -263,10 +270,11 @@ class PrintManagerUtils {
fun connectSunmiInnerBluePrint(onBtConnectCallBack: OnBtConnectCallBack? = null) {
val brand = Build.BRAND
val model = Build.MODEL
Log.d(TAG, "connectSunmiInnerBluePrint: model= ${model}")
//商米品牌&&(T系列||D2s系列 D2mini))
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"
))
) {
......
......@@ -22,7 +22,7 @@ android {
}
group 'com.qmai.android.sdk'
version '2.1.1-SNAPSHOT'
version '2.1.2-SNAPSHOT'
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