|
|
@@ -8,7 +8,6 @@ import com.clj.fastble.exception.BleException
|
|
|
import com.grkj.iscs.BusinessManager
|
|
|
import com.grkj.iscs.BusinessManager.deviceList
|
|
|
import com.grkj.iscs.BusinessManager.getBleDeviceByMac
|
|
|
-import com.grkj.iscs.BusinessManager.getCurrentStatus
|
|
|
import com.grkj.iscs.BusinessManager.isTestMode
|
|
|
import com.grkj.iscs.BusinessManager.removeExceptionKey
|
|
|
import com.grkj.iscs.BusinessManager.sendEventMsg
|
|
|
@@ -24,7 +23,6 @@ import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_LOADING
|
|
|
import com.grkj.iscs.util.ActivityUtils
|
|
|
import com.grkj.iscs.util.CommonUtils
|
|
|
import com.grkj.iscs.util.Executor
|
|
|
-import com.grkj.iscs.util.ToastUtils
|
|
|
import com.grkj.iscs.util.log.LogUtil
|
|
|
import com.grkj.iscs.view.base.BaseActivity
|
|
|
import com.sik.sikcore.activity.ActivityTracker
|
|
|
@@ -35,6 +33,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import kotlinx.coroutines.withContext
|
|
|
import pub.devrel.easypermissions.AfterPermissionGranted
|
|
|
import java.util.LinkedList
|
|
|
+import java.util.concurrent.atomic.AtomicInteger
|
|
|
import kotlin.coroutines.resume
|
|
|
import kotlin.coroutines.suspendCoroutine
|
|
|
|
|
|
@@ -115,6 +114,13 @@ object BleConnectionManager {
|
|
|
checkAndConnect()
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 是否有连接在等待
|
|
|
+ */
|
|
|
+ fun hasConnectWait(): Boolean {
|
|
|
+ return connectListeners.isEmpty() == false
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 连接监听反注册
|
|
|
*/
|
|
|
@@ -239,11 +245,11 @@ object BleConnectionManager {
|
|
|
}
|
|
|
|
|
|
override fun onScanning(bleDevice: BleDevice?) {
|
|
|
- val mac = bleDevice?.mac ?: return
|
|
|
- LogUtil.i("蓝牙连接-onScanning:$mac")
|
|
|
- if (mac.equals(mac, ignoreCase = true)) {
|
|
|
+ val bleMac = bleDevice?.mac ?: return
|
|
|
+ LogUtil.i("蓝牙连接-onScanning:$bleMac")
|
|
|
+ if (mac.equals(bleMac, ignoreCase = true)) {
|
|
|
// 找到目标设备,马上停止扫描
|
|
|
- LogUtil.i("找到目标设备 $mac,停止扫描并尝试连接")
|
|
|
+ LogUtil.i("蓝牙连接-找到目标设备 $bleMac,停止扫描并尝试连接")
|
|
|
BleManager.getInstance().cancelScan()
|
|
|
// 立刻调用 doConnect,下一步进入连接流程
|
|
|
doConnect(bleDevice, isNeedLoading, prepareDoneCallBack)
|
|
|
@@ -291,6 +297,7 @@ object BleConnectionManager {
|
|
|
BleUtil.instance?.connectBySelect(
|
|
|
bleDevice, object : CustomBleGattCallback() {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
+ LogUtil.i("蓝牙连接-连接指定设备参数:${promptStr}")
|
|
|
if (isNeedLoading) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
MSG_EVENT_LOADING, LoadingMsg(false, promptStr, null)
|
|
|
@@ -298,7 +305,9 @@ object BleConnectionManager {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- override fun onStartConnect() {}
|
|
|
+ override fun onStartConnect() {
|
|
|
+ LogUtil.i("蓝牙连接-开始连接")
|
|
|
+ }
|
|
|
|
|
|
override fun onConnectFail(bleDevice: BleDevice?, exception: BleException?) {
|
|
|
if (isNeedLoading) sendEventMsg(
|
|
|
@@ -369,15 +378,16 @@ object BleConnectionManager {
|
|
|
return
|
|
|
}
|
|
|
// 断开和重连之间最好间隔一段时间,否则可能会出现长时间连接不上的情况
|
|
|
- Executor.delayOnMain(300) {
|
|
|
- registerConnectListener(bleDevice.mac) { isDone, bleBean ->
|
|
|
- if (isDone && bleBean != null) {
|
|
|
- Executor.delayOnMain(300) {
|
|
|
- getCurrentStatus(6, bleBean.bleDevice)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+// Executor.delayOnMain(300) {
|
|
|
+// registerConnectListener(bleDevice.mac) { isDone, bleBean ->
|
|
|
+// if (isDone && bleBean != null) {
|
|
|
+// Executor.delayOnMain(300) {
|
|
|
+// getCurrentStatus(6, bleBean.bleDevice)
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ ModBusController.updateKeyReadyStatus(bleDevice.mac, false, 3)
|
|
|
} else {
|
|
|
ModBusController.updateKeyReadyStatus(bleDevice.mac, false, 3)
|
|
|
}
|
|
|
@@ -595,14 +605,20 @@ object BleConnectionManager {
|
|
|
override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
val devicesSnapshot = scanResultList?.toList().orEmpty()
|
|
|
ThreadUtils.runOnIO {
|
|
|
+ val handlerDeviceSize = AtomicInteger(0)
|
|
|
devicesSnapshot.forEach {
|
|
|
val connected = tryConnectWithOptionalCharge(it.mac, false)
|
|
|
if (connected) {
|
|
|
val sendSuccess = sendEmptyTicketJson(it)
|
|
|
LogUtil.i("设备录入-发送切换工作模式:${it.mac},${sendSuccess}")
|
|
|
+ handlerDeviceSize.addAndGet(1)
|
|
|
+ } else {
|
|
|
+ handlerDeviceSize.addAndGet(1)
|
|
|
+ }
|
|
|
+ if (devicesSnapshot.size == handlerDeviceSize.get()) {
|
|
|
+ parentCont.resume(true)
|
|
|
}
|
|
|
}
|
|
|
- parentCont.resume(true)
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
@@ -696,14 +712,14 @@ object BleConnectionManager {
|
|
|
/**
|
|
|
* 扫描在线的蓝牙
|
|
|
*/
|
|
|
- fun scanOnlineKeyLockMac(callback: (List<BleDevice>?) -> Unit) {
|
|
|
+ fun scanOnlineKeyLockMac(existsMac: List<String>, callback: (BleDevice?) -> Unit) {
|
|
|
BleUtil.instance?.scan(object : CustomBleScanCallback() {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
// 蓝牙未启动重试
|
|
|
LogUtil.d("设备录入-参数:${promptStr}")
|
|
|
BleManager.getInstance().enableBluetooth()
|
|
|
ThreadUtils.runOnMainDelayed(300) {
|
|
|
- scanOnlineKeyLockMac(callback)
|
|
|
+ scanOnlineKeyLockMac(existsMac, callback)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -711,7 +727,7 @@ object BleConnectionManager {
|
|
|
LogUtil.d("设备录入-onScanStarted:${success}")
|
|
|
if (!success) {
|
|
|
ThreadUtils.runOnMainDelayed(300) {
|
|
|
- scanOnlineKeyLockMac(callback)
|
|
|
+ scanOnlineKeyLockMac(existsMac, callback)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -719,11 +735,14 @@ object BleConnectionManager {
|
|
|
override fun onScanning(bleDevice: BleDevice?) {
|
|
|
val mac = bleDevice?.mac ?: return
|
|
|
LogUtil.d("设备录入-onScanning:$mac")
|
|
|
+ if (mac !in existsMac) {
|
|
|
+ callback(bleDevice)
|
|
|
+ BleManager.getInstance().cancelScan()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
LogUtil.d("设备录入-扫描完成:$scanResultList")
|
|
|
- callback(scanResultList?.toList())
|
|
|
}
|
|
|
})
|
|
|
}
|