|
|
@@ -26,6 +26,7 @@ import com.grkj.iscs.util.Executor
|
|
|
import com.grkj.iscs.util.log.LogUtil
|
|
|
import com.grkj.iscs.view.base.BaseActivity
|
|
|
import com.sik.sikcore.activity.ActivityTracker
|
|
|
+import com.sik.sikcore.thread.ThreadUtils
|
|
|
import pub.devrel.easypermissions.AfterPermissionGranted
|
|
|
import java.util.LinkedList
|
|
|
|
|
|
@@ -41,6 +42,9 @@ object BleConnectionManager {
|
|
|
@Volatile
|
|
|
var maxStandbyCount: Int = BleConst.MAX_KEY_STAND_BY
|
|
|
|
|
|
+ @Volatile
|
|
|
+ var maxConnectCount: Int = BleConst.MAX_KEY_CONNECT_COUNT
|
|
|
+
|
|
|
|
|
|
// 按连接完成顺序维护待机队列
|
|
|
private val standbyQueue = LinkedList<String>()
|
|
|
@@ -49,27 +53,39 @@ object BleConnectionManager {
|
|
|
private val connectListeners = mutableListOf<ConnectListener>()
|
|
|
private var isPreparing: Boolean = false
|
|
|
|
|
|
+ @Volatile
|
|
|
+ private var currentConnectingMac: String? = null
|
|
|
+
|
|
|
/**
|
|
|
- * 注册连接监听
|
|
|
+ * 注册连接监听:
|
|
|
+ * - 如果设备已在 deviceList 且拥有 token,立即回调并返回
|
|
|
+ * - 如果 mac 已在待连接队列或正在连接,忽略重复请求
|
|
|
+ * - 否则将 mac 添加到队列并触发连接流程
|
|
|
*/
|
|
|
fun registerConnectListener(mac: String, callBack: ((Boolean, BleBean?) -> Unit)? = null) {
|
|
|
LogUtil.i("registerConnectListener : $mac")
|
|
|
- if (connectListeners.any { it.mac == mac }) {
|
|
|
- LogUtil.w("Ignore mac : $mac 已存在")
|
|
|
+ // 已连接且已获取 token
|
|
|
+ deviceList.find { it.bleDevice.mac == mac && it.token != null }?.let { bean ->
|
|
|
+ callBack?.invoke(true, bean)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 重复注册检查
|
|
|
+ if (connectListeners.any { it.mac == mac } || currentConnectingMac == mac) {
|
|
|
+ LogUtil.w("忽略重复注册 mac: $mac")
|
|
|
return
|
|
|
}
|
|
|
- if (deviceList.none { it.bleDevice.mac == mac }) {
|
|
|
- connectListeners.add(ConnectListener(mac, callBack))
|
|
|
- if (connectListeners.isNotEmpty()) connectKey()
|
|
|
- } else {
|
|
|
- val bean = deviceList.find { it.bleDevice.mac == mac }!!
|
|
|
- if (bean.token == null) {
|
|
|
+ // 加入队列并启动连接
|
|
|
+ fun checkAndConnect() {
|
|
|
+ if (BleManager.getInstance().allConnectedDevice.size < maxConnectCount) {
|
|
|
connectListeners.add(ConnectListener(mac, callBack))
|
|
|
connectKey()
|
|
|
} else {
|
|
|
- callBack?.invoke(true, bean)
|
|
|
+ ThreadUtils.runOnIODelayed(500) {
|
|
|
+ checkAndConnect()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ checkAndConnect()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -98,11 +114,13 @@ object BleConnectionManager {
|
|
|
Executor.delayOnMain(1000) { connectKey() }
|
|
|
return
|
|
|
}
|
|
|
- isPreparing = true
|
|
|
val listener = connectListeners.first()
|
|
|
+ currentConnectingMac = listener.mac
|
|
|
+ isPreparing = true
|
|
|
if (ActivityTracker.getCurrentActivity() == null) {
|
|
|
LogUtil.w("Ignore connectKey : ${listener.mac} no current activity")
|
|
|
isPreparing = false
|
|
|
+ currentConnectingMac = null
|
|
|
return
|
|
|
}
|
|
|
prepareBle(
|
|
|
@@ -110,6 +128,7 @@ object BleConnectionManager {
|
|
|
) { isDone, bleBean ->
|
|
|
Executor.runOnMain {
|
|
|
isPreparing = false
|
|
|
+ currentConnectingMac = null
|
|
|
if (!isDone) {
|
|
|
// 判断是否仍然待连,防止拿走;移到末尾,防止循环影响
|
|
|
if (checkProcess(listener.mac)) {
|
|
|
@@ -129,9 +148,9 @@ object BleConnectionManager {
|
|
|
listener.callBack?.invoke(true, bleBean)
|
|
|
unregisterConnectListener(listener.mac)
|
|
|
}
|
|
|
- bleBean?.bleDevice?.mac?.let { mac ->
|
|
|
- addToStandby(mac)
|
|
|
- }
|
|
|
+// bleBean?.bleDevice?.mac?.let { mac ->
|
|
|
+// addToStandby(mac)
|
|
|
+// }
|
|
|
if (connectListeners.isNotEmpty()) connectKey()
|
|
|
}
|
|
|
}
|