diff --git a/app/src/main/java/com/casic/detector/common/view/SearchMarkerActivity.kt b/app/src/main/java/com/casic/detector/common/view/SearchMarkerActivity.kt index 14fe667..6beab28 100644 --- a/app/src/main/java/com/casic/detector/common/view/SearchMarkerActivity.kt +++ b/app/src/main/java/com/casic/detector/common/view/SearchMarkerActivity.kt @@ -46,6 +46,7 @@ import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.widget.dialog.AlertMessageDialog import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.OutputStream @@ -84,7 +85,7 @@ if (iBinder is SerialPortService.ServiceBinder) { serialPortService = iBinder.getSerialPortService() Log.d(kTag, "onServiceConnected: 服务已绑定") - searchMarker() + lifecycleScope.launch(Dispatchers.Main) { searchMarker() } } } @@ -156,7 +157,7 @@ LoadingDialogHub.dismiss() serialPortService?.closeSerialPort() "标识器深度探测超时,请移动位置重试".show(context) - searchMarker() + lifecycleScope.launch(Dispatchers.Main) { searchMarker() } } } @@ -173,7 +174,7 @@ } if (tag == '1') { "此标识器无法读取埋深!".show(this) - searchMarker() + lifecycleScope.launch(Dispatchers.Main) { searchMarker() } } else { LoadingDialogHub.show(this, "正在探测标识器埋深,请稍后...") countDownTimer.start() @@ -190,25 +191,7 @@ if (hex.startsWith("53")) { LoadingDialogHub.dismiss() countDownTimer.cancel() - try { - val depthResponse = hex.take(10).hexToString() - val depth = depthResponse.drop(2).toInt() - AlertMessageDialog.Builder().setContext(context) - .setTitle("温馨提示") - .setMessage("标识器埋深:${depth}厘米") - .setPositiveButton("知道了") - .setOnDialogButtonClickListener(object : - AlertMessageDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - serialPortService?.closeSerialPort() - searchMarker() - } - }).build().show() - } catch (e: WindowManager.BadTokenException) { - e.printStackTrace() - } catch (e: NumberFormatException) { - e.printStackTrace() - } + showDepthDialog(hex) } } }) @@ -216,7 +199,7 @@ } else { LoadingDialogHub.dismiss() "标识器未安装,安装成功后才可读取埋深!".show(this) - searchMarker() + lifecycleScope.launch(Dispatchers.Main) { searchMarker() } } } @@ -238,30 +221,55 @@ } } + private fun showDepthDialog(hex: String) { + try { + val depthResponse = hex.take(10).hexToString() + Log.d(kTag, "showDepthDialog: $depthResponse") + val depth = depthResponse.drop(2).toInt() + AlertMessageDialog.Builder().setContext(context) + .setTitle("温馨提示") + .setMessage("标识器埋深:${depth}厘米") + .setPositiveButton("知道了") + .setOnDialogButtonClickListener(object : + AlertMessageDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + serialPortService?.closeSerialPort() + lifecycleScope.launch(Dispatchers.Main) { searchMarker() } + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } catch (e: NumberFormatException) { + e.printStackTrace() + } + } + /** * 搜索标识器 * */ - private fun searchMarker() { - Thread.sleep(100) - + private suspend fun searchMarker() { + delay(300) + searchMarkerTimer = Timer() serialPortService?.openSerialPort(object : OnSerialPortDataListener { override fun write(outStream: OutputStream) { - searchMarkerTimer = Timer() - searchMarkerTimer.schedule(object : TimerTask() { - override fun run() { - outStream.write('2'.code) - outStream.flush() + runOnUiThread { + searchMarkerTimer.schedule(object : TimerTask() { + override fun run() { + outStream.write('2'.code) + outStream.flush() - Thread.sleep(50) + Thread.sleep(50) - outStream.write('6'.code) - outStream.flush() - } - }, 0, 200) + outStream.write('6'.code) + outStream.flush() + } + }, 0, 200) + } } override fun onDataReceived(buffer: ByteArray) { val hex = buffer.toHex() +// Log.d(kTag, "onDataReceived: $hex") if (hex.startsWith("4E")) { try { //4E转为String为N,代表能量值