|
|
@@ -528,6 +528,17 @@ object BusinessManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 检查是否能进行蓝牙连接准备的下一步,防止未准备完但是已经取消订阅
|
|
|
+ */
|
|
|
+ private fun checkProcess(mac: String?): Boolean {
|
|
|
+ val canProcess = connectListeners.any { it.mac == mac }
|
|
|
+ if (!canProcess) {
|
|
|
+ sendLoadingEventMsg(null, false)
|
|
|
+ }
|
|
|
+ return canProcess
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 连接钥匙,单个mac走完prepare再进行下一个
|
|
|
*/
|
|
|
@@ -548,9 +559,11 @@ object BusinessManager {
|
|
|
isPreparing = false
|
|
|
if (!isDone) {
|
|
|
// 判断是否仍然待连,防止拿走;移到末尾,防止循环影响
|
|
|
- unregisterConnectListener(listener.mac)
|
|
|
- Executor.delayOnMain(2000) {
|
|
|
- registerConnectListener(listener.mac, listener.callBack)
|
|
|
+ if (checkProcess(listener.mac)) {
|
|
|
+ unregisterConnectListener(listener.mac)
|
|
|
+ Executor.delayOnMain(2000) {
|
|
|
+ registerConnectListener(listener.mac, listener.callBack)
|
|
|
+ }
|
|
|
}
|
|
|
return@runOnMain
|
|
|
}
|
|
|
@@ -577,6 +590,10 @@ object BusinessManager {
|
|
|
isNeedLoading: Boolean = false,
|
|
|
prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
+ if (!checkProcess(mac)) {
|
|
|
+ LogUtil.e("Prepare is canceled : $mac")
|
|
|
+ return
|
|
|
+ }
|
|
|
Executor.runOnMain {
|
|
|
// 如果是已经连上的设备,从indicate开始往下走,不需要再扫描了
|
|
|
// if (BleManager.getInstance().isConnected(mac)) {
|
|
|
@@ -594,6 +611,10 @@ object BusinessManager {
|
|
|
@AfterPermissionGranted(PERMISSION_REQUEST_CODE)
|
|
|
private fun doScanBle(mac: String, isNeedLoading: Boolean = false, prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?) {
|
|
|
LogUtil.i("doScanBle:$mac")
|
|
|
+ if (!checkProcess(mac)) {
|
|
|
+ LogUtil.e("Prepare is canceled : $mac")
|
|
|
+ return
|
|
|
+ }
|
|
|
if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "正在扫描设备...", null)))
|
|
|
BleUtil.instance?.scan(object : CustomBleScanCallback() {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
@@ -613,16 +634,15 @@ object BusinessManager {
|
|
|
override fun onScanning(bleDevice: BleDevice?) {
|
|
|
LogUtil.i("onScanning:${bleDevice?.mac}")
|
|
|
bleDevice?.let {
|
|
|
- if (connectListeners.any { itData -> itData.mac == bleDevice.mac }) {
|
|
|
- doConnect(it, isNeedLoading, prepareDoneCallBack)
|
|
|
- }
|
|
|
+ doConnect(it, isNeedLoading, prepareDoneCallBack)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
LogUtil.i("onScanFinished: $mac")
|
|
|
if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
|
|
|
- if (connectListeners.any { it.mac == mac }) {
|
|
|
+ // 没有扫描到
|
|
|
+ if (scanResultList?.none { it.mac == mac } == true) {
|
|
|
LogUtil.w("$mac is not scanned")
|
|
|
prepareDoneCallBack?.invoke(false, null)
|
|
|
}
|
|
|
@@ -638,6 +658,10 @@ object BusinessManager {
|
|
|
isNeedLoading: Boolean = false,
|
|
|
prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
+ if (!checkProcess(bleDevice.mac)) {
|
|
|
+ LogUtil.e("Prepare is canceled : ${bleDevice.mac}")
|
|
|
+ return
|
|
|
+ }
|
|
|
if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, CommonUtils.getStr(R.string.ble_connecting), null)))
|
|
|
BleManager.getInstance().disconnect(bleDevice)
|
|
|
BleUtil.instance?.connectBySelect(bleDevice,
|
|
|
@@ -663,6 +687,10 @@ object BusinessManager {
|
|
|
deviceList.add(bleBean)
|
|
|
// 设置MTU
|
|
|
Executor.delayOnMain(200) {
|
|
|
+ if (!checkProcess(bleDevice.mac)) {
|
|
|
+ LogUtil.e("Prepare is canceled : ${bleDevice.mac}")
|
|
|
+ return@delayOnMain
|
|
|
+ }
|
|
|
BleUtil.instance?.setMtu(it)
|
|
|
}
|
|
|
// 监听
|
|
|
@@ -703,8 +731,13 @@ object BusinessManager {
|
|
|
isNeedLoading: Boolean = false,
|
|
|
prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
+ if (!checkProcess(bleBean?.bleDevice?.mac)) {
|
|
|
+ LogUtil.e("Prepare is canceled : ${bleBean?.bleDevice?.mac}")
|
|
|
+ return
|
|
|
+ }
|
|
|
if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始监听...", null)))
|
|
|
bleBean?.let {
|
|
|
+ var isIndicateSuccess = false
|
|
|
BleUtil.instance?.indicate(it.bleDevice, indicateCallback = object : CustomBleIndicateCallback() {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
LogUtil.i("indicate onPrompt : $promptStr")
|
|
|
@@ -722,13 +755,19 @@ object BusinessManager {
|
|
|
LogUtil.i("onIndicateSuccess")
|
|
|
// val testStr = "{\"cardNo\":\"80A8C0F4EA\",\"password\":\"12345678\",\"effectiveTime\":24,\"data\":[{\"taskCode\":\"202401020001\",\"taskId\":\"71b49baa49b343bc84d7e6b829ac1bdc\",\"codeId\":1,\"dataList\":[{\"dataId\":1,\"equipRfidNo\":\"049648B2E31690\",\"infoRfidNo\":\"04E3BCCA201290\",\"target\":1},{\"dataId\":2,\"equipRfidNo\":\"0405982414C563\",\"target\":0,\"prevId\":1}]},{\"taskCode\":\"202401020002\",\"taskId\":\"145b5a4cc38c41e19943f4c8b48d12b0\",\"codeId\":2,\"dataList\":[{\"dataId\":1,\"equipRfidNo\":\"045460F7F4F438\",\"infoRfidNo\":\"04BC6584C65009\",\"target\":1},{\"dataId\":2,\"equipRfidNo\":\"042B99E449E795\",\"target\":0,\"prevId\":1},{\"dataId\":3,\"equipRfidNo\":\"04A312EE848B62\",\"infoRfidNo\":\"04220E86831289\",\"target\":1,\"prevId\":2}]}],\"lockList\":[{\"lockId\":\"1\",\"rfid\":\"040E21443010E9\"},{\"lockId\":\"2\",\"rfid\":\"0457505E5861C2\"}]}"
|
|
|
// sendTicket(testStr, it.bleDevice, loadingCallBack)
|
|
|
+ isIndicateSuccess = true
|
|
|
getToken(bleBean, isNeedLoading, prepareDoneCallBack)
|
|
|
}
|
|
|
|
|
|
override fun onIndicateFailure(exception: BleException?) {
|
|
|
if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
|
|
|
LogUtil.e("onIndicateFailure : ${bleBean.bleDevice.mac} - ${exception?.description}")
|
|
|
- prepareDoneCallBack?.invoke(false, null)
|
|
|
+ Executor.delayOnIO(500) {
|
|
|
+ if (isIndicateSuccess) {
|
|
|
+ return@delayOnIO
|
|
|
+ }
|
|
|
+ prepareDoneCallBack?.invoke(false, null)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onCharacteristicChanged(data: ByteArray?) {
|
|
|
@@ -749,6 +788,10 @@ object BusinessManager {
|
|
|
isNeedLoading: Boolean = false,
|
|
|
prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
+ if (!checkProcess(bleBean?.bleDevice?.mac)) {
|
|
|
+ LogUtil.e("Prepare is canceled : ${bleBean?.bleDevice?.mac}")
|
|
|
+ return
|
|
|
+ }
|
|
|
if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始获取token...", null)))
|
|
|
bleBean?.let {
|
|
|
BleCmdManager.getToken(it.bleDevice.mac, object : CustomBleWriteCallback() {
|