Selaa lähdekoodia

refactor(蓝牙):
- 优化蓝牙扫描及连接逻辑,增加是否需要设备名称的配置项
- 调整钥匙分配逻辑,确保连接成功后再执行操作
- 延长开启充电后的等待时间,保证硬件电源稳定
- 修复部分页面蓝牙扫描设备名称过滤问题
- 更新部分国际化文本

周文健 1 kuukausi sitten
vanhempi
sitoutus
90a09131ec

+ 6 - 1
app/src/main/assets/i18n/en-US.json

@@ -702,7 +702,7 @@
   "exception_job": {
     "key": "exception_job",
     "type": "text",
-    "value": "Exception Job Name"
+    "value": "Excp Jobs"
   },
   "exception_job_title": {
     "key": "exception_job_title",
@@ -4153,5 +4153,10 @@
     "key": "todo_wait",
     "type": "text",
     "value": "Wait:"
+  },
+  "excp_job_name_title": {
+    "key": "excp_job_name_title",
+    "type": "text",
+    "value": "Exception Job Name"
   }
 }

+ 5 - 0
app/src/main/assets/i18n/zh-CN.json

@@ -4158,5 +4158,10 @@
     "key": "todo_wait",
     "type": "text",
     "value": "等待:"
+  },
+  "excp_job_name_title": {
+    "key": "excp_job_name_title",
+    "type": "text",
+    "value": "异常作业名称"
   }
 }

+ 7 - 0
app/src/main/java/com/grkj/iscs/features/init/fragment/InitDeviceRegistrationKeyAndLockFragment.kt

@@ -14,6 +14,7 @@ import com.google.android.flexbox.FlexDirection
 import com.google.android.flexbox.FlexWrap
 import com.google.android.flexbox.FlexboxLayoutManager
 import com.google.android.flexbox.JustifyContent
+import com.grkj.data.config.ISCSConfig
 import com.grkj.data.data.EventConstants
 import com.grkj.data.enums.HardwareMode
 import com.grkj.data.hardware.DockData
@@ -117,6 +118,12 @@ class InitDeviceRegistrationKeyAndLockFragment :
                 binding.dockRv.models = allDevice
             }
         }
+        ISCSConfig.needDeviceName = true
+    }
+
+    override fun onPause() {
+        super.onPause()
+        ISCSConfig.needDeviceName = false
     }
 
     private fun BindingAdapter.BindingViewHolder.onLockDockRVListBinding(

+ 6 - 0
app/src/main/java/com/grkj/iscs/features/main/fragment/hardware_manage/SlotsManageFragment.kt

@@ -587,6 +587,7 @@ class SlotsManageFragment : BaseFragment<FragmentSlotsManageBinding>() {
 
     override fun initData() {
         super.initData()
+        ISCSConfig.needDeviceName = true
         getExceptionData()
         viewModel.registerStatusListener {
             if (it.deviceList.isNotEmpty()) {
@@ -596,6 +597,11 @@ class SlotsManageFragment : BaseFragment<FragmentSlotsManageBinding>() {
         getData()
     }
 
+    override fun onPause() {
+        super.onPause()
+        ISCSConfig.needDeviceName = false
+    }
+
     /**
      * 获取异常数据
      */

+ 1 - 1
app/src/main/res/layout/fragment_exception_job.xml

@@ -86,7 +86,7 @@
 
                             <TextView
                                 style="@style/TableCellKey"
-                                app:i18nKey='@{"exception_job"}' />
+                                app:i18nKey='@{"excp_job_name_title"}' />
 
                             <TextView
                                 android:id="@+id/exception_job"

+ 5 - 0
data/src/main/java/com/grkj/data/config/ISCSConfig.kt

@@ -7,6 +7,11 @@ import com.sik.sikcore.extension.getMMKVData
  * 锁柜配置
  */
 object ISCSConfig {
+    /**
+     * 是否需要设备名称
+     */
+    var needDeviceName: Boolean = false
+
     /**
      * 是否是测试模式
      */

+ 5 - 4
data/src/main/java/com/grkj/data/hardware/ble/BleConnectionManager.kt

@@ -290,7 +290,7 @@ object BleConnectionManager {
     ) {
         logger.info("蓝牙连接-doScanBle:$mac")
         if (isNeedLoading) LoadingEvent.Companion.sendLoadingEvent("正在扫描设备...", true)
-        BleUtil.Companion.instance?.scan(object : CustomBleScanCallback() {
+        BleUtil.instance?.scan(object : CustomBleScanCallback() {
             override fun onPrompt(promptStr: String?) {
                 // 蓝牙未启动重试
                 logger.info("蓝牙连接-参数:${promptStr}")
@@ -723,7 +723,6 @@ object BleConnectionManager {
      */
     suspend fun tryConnectWithOptionalCharge(
         mac: String,
-        withOpenCharge: Boolean = true,
         isSend: Boolean
     ): Boolean =
         withContext(Dispatchers.IO) {
@@ -731,17 +730,19 @@ object BleConnectionManager {
             // 开电,并等待回调
             suspendCoroutine<Unit> { unitCont ->
                 HardwareMode.getCurrentHardwareMode().controlKeyCharge(false, mac) {
+                    logger.info("接收到消息,关闭充电")
                     ThreadUtils.runOnIO {
                         delay(500)
                         HardwareMode.getCurrentHardwareMode().controlKeyCharge(true, mac) {
+                            logger.info("接收到消息,打开")
                             unitCont.resume(Unit)
                         }
                     }
                 }
             }
-            logger.debug("蓝牙连接-开启充电并等待500ms")
+            logger.debug("蓝牙连接-开启充电并等待1500ms")
             // 等 500ms 保证硬件电源稳定
-            delay(500)
+            delay(1500)
 
             // 再次注册连接监听
             val secondTry = suspendCancellableCoroutine<Boolean> { cont ->

+ 21 - 13
data/src/main/java/com/grkj/data/hardware/ble/BleUtil.kt

@@ -7,6 +7,7 @@ import android.bluetooth.le.ScanSettings
 import android.os.Build
 import android.util.Log
 import androidx.annotation.RequiresPermission
+import com.grkj.data.config.ISCSConfig
 import com.grkj.shared.utils.extension.toHexStrings
 import com.huyuhui.fastble.BleManager
 import com.huyuhui.fastble.callback.BleGattCallback
@@ -18,6 +19,7 @@ import com.sik.sikcore.SIKCore
 import com.sik.sikcore.thread.ThreadUtils
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
+import java.util.UUID
 
 /**
  * 蓝牙工具类
@@ -46,19 +48,6 @@ class BleUtil private constructor() {
                     this.maxConnectCount = 2
                     operateTimeout = OPERATE_TIMEOUT
                 }
-            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
-                //Android 12及以上不允许添加过滤器
-                val bleScanRuleConfig = BleScanRuleConfig.Builder()
-                    .setScanTimeOut(10_000L)
-                    .setDeviceName(BleConst.BLE_LOCAL_NAME)
-                    .apply {
-                        BleScanRuleConfig.Builder().setScanSettings(ScanSettings.Builder().apply {
-                            this.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
-                        }.build())
-                    }
-                    .build()
-                BleManager.bleScanRuleConfig = bleScanRuleConfig
-            }
         } catch (e: Exception) {
             Log.d("initBlueTooth", "蓝牙初始化:${e.message}")
         }
@@ -71,6 +60,25 @@ class BleUtil private constructor() {
                 if (inScan) {
                     BleManager.cancelScan()
                 }
+                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
+                    //Android 12及以上不允许添加过滤器
+                    val bleScanRuleConfig = BleScanRuleConfig.Builder()
+                        .setScanTimeOut(10_000L)
+                        .apply {
+                            if (ISCSConfig.needDeviceName) {
+                                setDeviceName(BleConst.BLE_LOCAL_NAME)
+                            }
+                            setScanSettings(
+                                ScanSettings.Builder()
+                                    .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
+                                    .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
+                                    .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
+                                    .setReportDelay(0) // 立刻回调,不做批处理
+                                    .build()
+                            )
+                        }.build()
+                    BleManager.bleScanRuleConfig = bleScanRuleConfig
+                }
                 BleManager.scan(object : CustomBleScanCallback() {
                     override fun onPrompt(promptStr: String?) {
                         bleScanCallback.onPrompt(promptStr)

+ 59 - 47
ui-base/src/main/java/com/grkj/ui_base/business/BleBusinessManager.kt

@@ -935,62 +935,74 @@ object BleBusinessManager {
     /**
      * 分配钥匙
      */
+    @SuppressLint("MissingPermission")
     fun handleGiveKey(deviceTakeUpdateBO: DeviceTakeUpdate) {
+        val mac = HardwareMode.getCurrentHardwareMode().getKeyMacByRfid(deviceTakeUpdateBO.nfc)
         logger.info("取完锁开始发钥匙2:${BleConnectionManager.deviceList.toJson()}")
-        BleConnectionManager.getBleDeviceByMac(
-            HardwareMode.getCurrentHardwareMode().getKeyMacByRfid(deviceTakeUpdateBO.nfc)
-        )
-            ?.let {
-                BleConnectionManager.getCurrentStatus(
-                    2, BleConnectionManager.getBleDeviceByMac(
+        mac?.let {
+            if (BleSendDispatcher.isConnected(it)) {
+                BleSendDispatcher.submit(it) {
+                    BleConnectionManager.getBleDeviceByMac(
                         HardwareMode.getCurrentHardwareMode()
                             .getKeyMacByRfid(deviceTakeUpdateBO.nfc)
-                    )!!.bleDevice
-                ) {
-                    if (!it) {
-                        return@getCurrentStatus
-                    }
-                    logger.warn("handleGiveKey timeout")
-                    HardwareBusinessManager.removeDeviceTake(
-                        DeviceConst.DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc
                     )
-                    HardwareBusinessManager.checkEquipCount(
-                        deviceTakeUpdateBO.ticketId,
-                        0,
-                        true
-                    ) { keyPair, lockMap ->
-                        if (keyPair == null) {
-                            TipDialog.show(
-                                msg = CommonUtils.getStr("key_take_error_tip").toString(),
-                                onConfirmClick = {
-                                    DeviceExceptionEvent.sendDeviceExceptionEvent(
-                                        DeviceConst.DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc
-                                    )
-                                })
-                        } else {
-                            HardwareBusinessManager.addDeviceTake(
-                                DeviceConst.DEVICE_TYPE_KEY,
-                                deviceTakeUpdateBO.ticketId,
-                                keyPair.second
-                            )
-                            handleGiveKey(
-                                DeviceTakeUpdate(
-                                    DeviceConst.DEVICE_TYPE_KEY,
+                        ?.let {
+                            BleConnectionManager.getCurrentStatus(
+                                2, BleConnectionManager.getBleDeviceByMac(
+                                    HardwareMode.getCurrentHardwareMode()
+                                        .getKeyMacByRfid(deviceTakeUpdateBO.nfc)
+                                )!!.bleDevice
+                            ) {
+                                if (!it) {
+                                    return@getCurrentStatus
+                                }
+                                logger.warn("handleGiveKey timeout")
+                                HardwareBusinessManager.removeDeviceTake(
+                                    DeviceConst.DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc
+                                )
+                                HardwareBusinessManager.checkEquipCount(
                                     deviceTakeUpdateBO.ticketId,
-                                    keyPair.second ?: ""
+                                    0,
+                                    true
+                                ) { keyPair, lockMap ->
+                                    if (keyPair == null) {
+                                        TipDialog.show(
+                                            msg = CommonUtils.getStr("key_take_error_tip")
+                                                .toString(),
+                                            onConfirmClick = {
+                                                DeviceExceptionEvent.sendDeviceExceptionEvent(
+                                                    DeviceConst.DEVICE_TYPE_KEY,
+                                                    deviceTakeUpdateBO.nfc
+                                                )
+                                            })
+                                    } else {
+                                        HardwareBusinessManager.addDeviceTake(
+                                            DeviceConst.DEVICE_TYPE_KEY,
+                                            deviceTakeUpdateBO.ticketId,
+                                            keyPair.second
+                                        )
+                                        handleGiveKey(
+                                            DeviceTakeUpdate(
+                                                DeviceConst.DEVICE_TYPE_KEY,
+                                                deviceTakeUpdateBO.ticketId,
+                                                keyPair.second ?: ""
+                                            )
+                                        )
+                                    }
+                                }
+                            }
+                        } ?: run {
+                        logger.info("根据钥匙的rfid没找到钥匙")
+                        TipDialog.show(
+                            msg = CommonUtils.getStr("key_take_error_tip").toString(),
+                            onConfirmClick = {
+                                DeviceExceptionEvent.sendDeviceExceptionEvent(
+                                    DeviceConst.DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc
                                 )
-                            )
-                        }
+                            })
                     }
                 }
-            } ?: run {
-            logger.info("根据钥匙的rfid没找到钥匙")
-            TipDialog.show(
-                msg = CommonUtils.getStr("key_take_error_tip").toString(), onConfirmClick = {
-                    DeviceExceptionEvent.sendDeviceExceptionEvent(
-                        DeviceConst.DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc
-                    )
-                })
+            }
         }
     }