diff --git a/app/src/main/java/com/casic/detector/base/BaseApplication.kt b/app/src/main/java/com/casic/detector/base/BaseApplication.kt index 87ef650..b8434f6 100644 --- a/app/src/main/java/com/casic/detector/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/detector/base/BaseApplication.kt @@ -14,7 +14,6 @@ class BaseApplication : Application() { private val kTag = "BaseApplication" - private val suffix = "/dev/ttysWK" private var serialPorts: ArrayList? = null @Throws(SecurityException::class, IOException::class, InvalidParameterException::class) @@ -24,9 +23,8 @@ * Open the serial port * */ serialPorts?.apply { - add(SerialPort(File("${suffix}1"), 9600, 0)) - add(SerialPort(File("${suffix}2"), 9600, 0)) - add(SerialPort(File("${suffix}0"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK1"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK2"), 9600, 0)) } return serialPorts } diff --git a/app/src/main/java/com/casic/detector/base/BaseApplication.kt b/app/src/main/java/com/casic/detector/base/BaseApplication.kt index 87ef650..b8434f6 100644 --- a/app/src/main/java/com/casic/detector/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/detector/base/BaseApplication.kt @@ -14,7 +14,6 @@ class BaseApplication : Application() { private val kTag = "BaseApplication" - private val suffix = "/dev/ttysWK" private var serialPorts: ArrayList? = null @Throws(SecurityException::class, IOException::class, InvalidParameterException::class) @@ -24,9 +23,8 @@ * Open the serial port * */ serialPorts?.apply { - add(SerialPort(File("${suffix}1"), 9600, 0)) - add(SerialPort(File("${suffix}2"), 9600, 0)) - add(SerialPort(File("${suffix}0"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK1"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK2"), 9600, 0)) } return serialPorts } diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index 7564ae9..eaff0e5 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -4,13 +4,12 @@ import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import androidx.viewbinding.ViewBinding -import com.casic.detector.uart.SerialPort import com.pengxh.kt.lite.extensions.show import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.IOException -import java.io.InputStream import java.io.OutputStream import java.security.InvalidParameterException @@ -19,12 +18,7 @@ protected lateinit var binding: VB - private var serialPort: SerialPort? = null - var outStream: OutputStream? = null - - private var inStream: InputStream? = null - private var inStream1: InputStream? = null - private var inStream2: InputStream? = null + lateinit var out: OutputStream override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -37,21 +31,14 @@ try { val serialPorts = BaseApplication.get().getSerialPorts() - if (serialPorts.isNotEmpty()) { - outStream = serialPorts[0].outputStream - - inStream = serialPorts[0].inputStream - inStream1 = serialPorts[1].inputStream - inStream2 = serialPorts[2].inputStream - - //多协程读取串口数据 - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int + //读 + lifecycleScope.launch(Dispatchers.IO) { + serialPorts?.apply { + val stream = this[0].inputStream + while (isActive) { try { - val buffer = ByteArray(72) - if (inStream == null) return@launch - size = inStream!!.read(buffer) + val buffer = ByteArray(64) + val size = stream.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { onDataReceived(buffer) @@ -59,18 +46,18 @@ } } catch (e: IOException) { e.printStackTrace() - return@launch } } } + } - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int + lifecycleScope.launch(Dispatchers.IO) { + serialPorts?.apply { + val stream = this[1].inputStream + while (isActive) { try { - val buffer = ByteArray(72) - if (inStream1 == null) return@launch - size = inStream1!!.read(buffer) + val buffer = ByteArray(64) + val size = stream.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { onDataReceived(buffer) @@ -78,29 +65,14 @@ } } catch (e: IOException) { e.printStackTrace() - return@launch } } } + } - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int - try { - val buffer = ByteArray(72) - if (inStream2 == null) return@launch - size = inStream2!!.read(buffer) - if (size > 0) { - withContext(Dispatchers.Main) { - onDataReceived(buffer) - } - } - } catch (e: IOException) { - e.printStackTrace() - return@launch - } - } - } + //写 + serialPorts?.apply { + out = this[0].outputStream } } catch (e: SecurityException) { "您没有串口的读写权限!".show(this) @@ -137,12 +109,12 @@ abstract fun initEvent() /** - * 串口读数 + * 串口读数,已经切回主线程 * */ - abstract fun onDataReceived(buffer: ByteArray?) + abstract fun onDataReceived(buffer: ByteArray) override fun onDestroy() { - super.onDestroy() BaseApplication.get().closeSerialPort() + super.onDestroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/BaseApplication.kt b/app/src/main/java/com/casic/detector/base/BaseApplication.kt index 87ef650..b8434f6 100644 --- a/app/src/main/java/com/casic/detector/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/detector/base/BaseApplication.kt @@ -14,7 +14,6 @@ class BaseApplication : Application() { private val kTag = "BaseApplication" - private val suffix = "/dev/ttysWK" private var serialPorts: ArrayList? = null @Throws(SecurityException::class, IOException::class, InvalidParameterException::class) @@ -24,9 +23,8 @@ * Open the serial port * */ serialPorts?.apply { - add(SerialPort(File("${suffix}1"), 9600, 0)) - add(SerialPort(File("${suffix}2"), 9600, 0)) - add(SerialPort(File("${suffix}0"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK1"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK2"), 9600, 0)) } return serialPorts } diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index 7564ae9..eaff0e5 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -4,13 +4,12 @@ import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import androidx.viewbinding.ViewBinding -import com.casic.detector.uart.SerialPort import com.pengxh.kt.lite.extensions.show import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.IOException -import java.io.InputStream import java.io.OutputStream import java.security.InvalidParameterException @@ -19,12 +18,7 @@ protected lateinit var binding: VB - private var serialPort: SerialPort? = null - var outStream: OutputStream? = null - - private var inStream: InputStream? = null - private var inStream1: InputStream? = null - private var inStream2: InputStream? = null + lateinit var out: OutputStream override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -37,21 +31,14 @@ try { val serialPorts = BaseApplication.get().getSerialPorts() - if (serialPorts.isNotEmpty()) { - outStream = serialPorts[0].outputStream - - inStream = serialPorts[0].inputStream - inStream1 = serialPorts[1].inputStream - inStream2 = serialPorts[2].inputStream - - //多协程读取串口数据 - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int + //读 + lifecycleScope.launch(Dispatchers.IO) { + serialPorts?.apply { + val stream = this[0].inputStream + while (isActive) { try { - val buffer = ByteArray(72) - if (inStream == null) return@launch - size = inStream!!.read(buffer) + val buffer = ByteArray(64) + val size = stream.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { onDataReceived(buffer) @@ -59,18 +46,18 @@ } } catch (e: IOException) { e.printStackTrace() - return@launch } } } + } - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int + lifecycleScope.launch(Dispatchers.IO) { + serialPorts?.apply { + val stream = this[1].inputStream + while (isActive) { try { - val buffer = ByteArray(72) - if (inStream1 == null) return@launch - size = inStream1!!.read(buffer) + val buffer = ByteArray(64) + val size = stream.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { onDataReceived(buffer) @@ -78,29 +65,14 @@ } } catch (e: IOException) { e.printStackTrace() - return@launch } } } + } - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int - try { - val buffer = ByteArray(72) - if (inStream2 == null) return@launch - size = inStream2!!.read(buffer) - if (size > 0) { - withContext(Dispatchers.Main) { - onDataReceived(buffer) - } - } - } catch (e: IOException) { - e.printStackTrace() - return@launch - } - } - } + //写 + serialPorts?.apply { + out = this[0].outputStream } } catch (e: SecurityException) { "您没有串口的读写权限!".show(this) @@ -137,12 +109,12 @@ abstract fun initEvent() /** - * 串口读数 + * 串口读数,已经切回主线程 * */ - abstract fun onDataReceived(buffer: ByteArray?) + abstract fun onDataReceived(buffer: ByteArray) override fun onDestroy() { - super.onDestroy() BaseApplication.get().closeSerialPort() + super.onDestroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/utils/SerialPortManager.kt b/app/src/main/java/com/casic/detector/utils/SerialPortManager.kt deleted file mode 100644 index 904719b..0000000 --- a/app/src/main/java/com/casic/detector/utils/SerialPortManager.kt +++ /dev/null @@ -1,116 +0,0 @@ -package com.casic.detector.utils - -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.LifecycleRegistry -import androidx.lifecycle.lifecycleScope -import com.casic.detector.base.BaseApplication -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import java.io.IOException - -class SerialPortManager(private val listener: OnSerialPortListener) : LifecycleOwner { - - private val kTag = "SerialPortManager" - private val registry = LifecycleRegistry(this) - private val serialPorts = BaseApplication.get().getSerialPorts() - - init { - //读取串口数据 - lifecycleScope.launch(Dispatchers.IO) { - val buffer = ByteArray(72) - var size: Int - serialPorts?.forEach { - val stream = it.inputStream - while (true) { - try { - size = stream.read(buffer) - if (size > 0) { - withContext(Dispatchers.Main) { - listener.onDataReceived(buffer) - } - } - } catch (e: IOException) { - e.printStackTrace() - return@launch - } - } - } - } - } - - /** - * 探测 - * */ - fun detectMarker() { - lifecycleScope.launch(Dispatchers.IO) { - serialPorts?.apply { - while (size >= 1) { - val outStream = this[0].outputStream - try { - // 发送读标识器ID指令 - outStream?.write('2'.code) - outStream?.flush() - delay(100) - - outStream.write('6'.code) - outStream.flush() - delay(100) - } catch (e: IOException) { - e.printStackTrace() - } - } - } - } - } - - /** - * 发送探测深度指令 - * */ - fun detectDepth(markerTag: Char) { - lifecycleScope.launch(Dispatchers.IO) { - serialPorts?.apply { - val outStream = this[0].outputStream - try { - // 发送测距指令 - outStream?.write(markerTag.code) - outStream?.flush() - } catch (e: IOException) { - e.printStackTrace() - } - } - } - } - - /** - * 发送读标识器ID指令 - * */ - fun searchMarkerSignal() { - lifecycleScope.launch(Dispatchers.IO) { - serialPorts?.apply { - val outStream = this[0].outputStream - try { - // 发送读标识器ID指令 - outStream?.write('2'.code) - outStream?.flush() - } catch (e: IOException) { - e.printStackTrace() - } - } - } - } - - fun closeSerialPort() { - BaseApplication.get().closeSerialPort() - } - - interface OnSerialPortListener { - fun onDataReceived(buffer: ByteArray) - } - - override fun getLifecycle(): Lifecycle { - return registry - } -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/BaseApplication.kt b/app/src/main/java/com/casic/detector/base/BaseApplication.kt index 87ef650..b8434f6 100644 --- a/app/src/main/java/com/casic/detector/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/detector/base/BaseApplication.kt @@ -14,7 +14,6 @@ class BaseApplication : Application() { private val kTag = "BaseApplication" - private val suffix = "/dev/ttysWK" private var serialPorts: ArrayList? = null @Throws(SecurityException::class, IOException::class, InvalidParameterException::class) @@ -24,9 +23,8 @@ * Open the serial port * */ serialPorts?.apply { - add(SerialPort(File("${suffix}1"), 9600, 0)) - add(SerialPort(File("${suffix}2"), 9600, 0)) - add(SerialPort(File("${suffix}0"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK1"), 9600, 0)) + add(SerialPort(File("/dev/ttysWK2"), 9600, 0)) } return serialPorts } diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index 7564ae9..eaff0e5 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -4,13 +4,12 @@ import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import androidx.viewbinding.ViewBinding -import com.casic.detector.uart.SerialPort import com.pengxh.kt.lite.extensions.show import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.IOException -import java.io.InputStream import java.io.OutputStream import java.security.InvalidParameterException @@ -19,12 +18,7 @@ protected lateinit var binding: VB - private var serialPort: SerialPort? = null - var outStream: OutputStream? = null - - private var inStream: InputStream? = null - private var inStream1: InputStream? = null - private var inStream2: InputStream? = null + lateinit var out: OutputStream override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -37,21 +31,14 @@ try { val serialPorts = BaseApplication.get().getSerialPorts() - if (serialPorts.isNotEmpty()) { - outStream = serialPorts[0].outputStream - - inStream = serialPorts[0].inputStream - inStream1 = serialPorts[1].inputStream - inStream2 = serialPorts[2].inputStream - - //多协程读取串口数据 - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int + //读 + lifecycleScope.launch(Dispatchers.IO) { + serialPorts?.apply { + val stream = this[0].inputStream + while (isActive) { try { - val buffer = ByteArray(72) - if (inStream == null) return@launch - size = inStream!!.read(buffer) + val buffer = ByteArray(64) + val size = stream.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { onDataReceived(buffer) @@ -59,18 +46,18 @@ } } catch (e: IOException) { e.printStackTrace() - return@launch } } } + } - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int + lifecycleScope.launch(Dispatchers.IO) { + serialPorts?.apply { + val stream = this[1].inputStream + while (isActive) { try { - val buffer = ByteArray(72) - if (inStream1 == null) return@launch - size = inStream1!!.read(buffer) + val buffer = ByteArray(64) + val size = stream.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { onDataReceived(buffer) @@ -78,29 +65,14 @@ } } catch (e: IOException) { e.printStackTrace() - return@launch } } } + } - lifecycleScope.launch(Dispatchers.IO) { - while (true) { - var size: Int - try { - val buffer = ByteArray(72) - if (inStream2 == null) return@launch - size = inStream2!!.read(buffer) - if (size > 0) { - withContext(Dispatchers.Main) { - onDataReceived(buffer) - } - } - } catch (e: IOException) { - e.printStackTrace() - return@launch - } - } - } + //写 + serialPorts?.apply { + out = this[0].outputStream } } catch (e: SecurityException) { "您没有串口的读写权限!".show(this) @@ -137,12 +109,12 @@ abstract fun initEvent() /** - * 串口读数 + * 串口读数,已经切回主线程 * */ - abstract fun onDataReceived(buffer: ByteArray?) + abstract fun onDataReceived(buffer: ByteArray) override fun onDestroy() { - super.onDestroy() BaseApplication.get().closeSerialPort() + super.onDestroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/utils/SerialPortManager.kt b/app/src/main/java/com/casic/detector/utils/SerialPortManager.kt deleted file mode 100644 index 904719b..0000000 --- a/app/src/main/java/com/casic/detector/utils/SerialPortManager.kt +++ /dev/null @@ -1,116 +0,0 @@ -package com.casic.detector.utils - -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.LifecycleRegistry -import androidx.lifecycle.lifecycleScope -import com.casic.detector.base.BaseApplication -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import java.io.IOException - -class SerialPortManager(private val listener: OnSerialPortListener) : LifecycleOwner { - - private val kTag = "SerialPortManager" - private val registry = LifecycleRegistry(this) - private val serialPorts = BaseApplication.get().getSerialPorts() - - init { - //读取串口数据 - lifecycleScope.launch(Dispatchers.IO) { - val buffer = ByteArray(72) - var size: Int - serialPorts?.forEach { - val stream = it.inputStream - while (true) { - try { - size = stream.read(buffer) - if (size > 0) { - withContext(Dispatchers.Main) { - listener.onDataReceived(buffer) - } - } - } catch (e: IOException) { - e.printStackTrace() - return@launch - } - } - } - } - } - - /** - * 探测 - * */ - fun detectMarker() { - lifecycleScope.launch(Dispatchers.IO) { - serialPorts?.apply { - while (size >= 1) { - val outStream = this[0].outputStream - try { - // 发送读标识器ID指令 - outStream?.write('2'.code) - outStream?.flush() - delay(100) - - outStream.write('6'.code) - outStream.flush() - delay(100) - } catch (e: IOException) { - e.printStackTrace() - } - } - } - } - } - - /** - * 发送探测深度指令 - * */ - fun detectDepth(markerTag: Char) { - lifecycleScope.launch(Dispatchers.IO) { - serialPorts?.apply { - val outStream = this[0].outputStream - try { - // 发送测距指令 - outStream?.write(markerTag.code) - outStream?.flush() - } catch (e: IOException) { - e.printStackTrace() - } - } - } - } - - /** - * 发送读标识器ID指令 - * */ - fun searchMarkerSignal() { - lifecycleScope.launch(Dispatchers.IO) { - serialPorts?.apply { - val outStream = this[0].outputStream - try { - // 发送读标识器ID指令 - outStream?.write('2'.code) - outStream?.flush() - } catch (e: IOException) { - e.printStackTrace() - } - } - } - } - - fun closeSerialPort() { - BaseApplication.get().closeSerialPort() - } - - interface OnSerialPortListener { - fun onDataReceived(buffer: ByteArray) - } - - override fun getLifecycle(): Lifecycle { - return registry - } -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/MainActivity.kt b/app/src/main/java/com/casic/detector/view/MainActivity.kt index 1e1e44c..ae048ce 100644 --- a/app/src/main/java/com/casic/detector/view/MainActivity.kt +++ b/app/src/main/java/com/casic/detector/view/MainActivity.kt @@ -3,8 +3,8 @@ import android.annotation.SuppressLint import android.app.DatePickerDialog import android.app.Dialog +import android.content.Context import android.content.DialogInterface -import android.content.Intent import android.graphics.BitmapFactory import android.graphics.Color import android.graphics.drawable.BitmapDrawable @@ -13,9 +13,6 @@ import android.media.SoundPool import android.os.Bundle import android.os.CountDownTimer -import android.os.Handler -import android.os.Message -import android.provider.Settings import android.util.Log import android.view.KeyEvent import android.view.View @@ -35,6 +32,7 @@ import com.amap.api.maps.model.MyLocationStyle import com.casic.detector.R import com.casic.detector.adapter.EditableImageAdapter +import com.casic.detector.base.SerialPortActivity import com.casic.detector.bean.LabelBean import com.casic.detector.bean.TaskBean import com.casic.detector.callback.ILocationListener @@ -66,7 +64,6 @@ import com.casic.detector.utils.LocaleConstant import com.casic.detector.utils.LocationHub import com.casic.detector.utils.RouteOnMap -import com.casic.detector.utils.SerialPortManager import com.casic.detector.vm.TaskViewModel import com.casic.detector.widgets.QueryMarkerDialog import com.casic.detector.widgets.SamplePopupWindow @@ -76,7 +73,6 @@ import com.luck.picture.lib.config.SelectMimeType import com.luck.picture.lib.entity.LocalMedia import com.luck.picture.lib.interfaces.OnResultCallbackListener -import com.pengxh.kt.lite.base.KotlinBaseActivity import com.pengxh.kt.lite.extensions.appendZero import com.pengxh.kt.lite.extensions.binding import com.pengxh.kt.lite.extensions.convertColor @@ -94,12 +90,10 @@ import com.pengxh.kt.lite.utils.FileDownloadManager import com.pengxh.kt.lite.utils.LoadState import com.pengxh.kt.lite.utils.SaveKeyValues -import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import com.pengxh.kt.lite.widget.dialog.AlertInputDialog import com.pengxh.kt.lite.widget.dialog.AlertMessageDialog import com.pengxh.kt.lite.widget.dialog.BottomActionSheet -import com.pengxh.kt.lite.widget.dialog.NoNetworkDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -107,22 +101,11 @@ import java.text.DecimalFormat import java.util.Calendar import java.util.Date +import java.util.Timer +import java.util.TimerTask - -class MainActivity : KotlinBaseActivity(), - SerialPortManager.OnSerialPortListener { - companion object { - private val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ALARM) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - val soundPool: SoundPool = SoundPool.Builder() - .setMaxStreams(16) - .setAudioAttributes(audioAttributes) - .build() - lateinit var serialPortManager: SerialPortManager - lateinit var taskViewModel: TaskViewModel - } +@SuppressLint("all") +class MainActivity : SerialPortActivity() { private val kTag = "MainActivity" private val context = this@MainActivity @@ -134,7 +117,6 @@ private val backDrawables by lazy { HashMap() } private val installDialog by lazy { InstallMarkerDialog(this) } private val searchDialog by lazy { SearchMarkerDialog(this) } - private var soundResourceId = 0 private var clickTime: Long = 0 private var labelBeans = ArrayList() private var clusterOverlay: ClusterOverlay? = null @@ -145,6 +127,21 @@ private lateinit var aMap: AMap private lateinit var taskBean: TaskBean private lateinit var taskId: String + private lateinit var searchMarkerTimer: Timer + private lateinit var signalTask: TimerTask + + /***inner class 需要用到*****start*/ + private val taskViewModel by lazy { ViewModelProvider(this)[TaskViewModel::class.java] } + private val attr = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + private val soundPool = SoundPool.Builder().setMaxStreams(16).setAudioAttributes(attr).build() + private var soundResourceId = 0 + private var slowSoundResourceId = 0 + private var fastSoundResourceId = 0 + + /***inner class 需要用到*****end*/ override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) @@ -155,19 +152,13 @@ } override fun initOnCreate(savedInstanceState: Bundle?) { - //调高串口电位 - gpioManager.setGpioHigh("18") + soundResourceId = soundPool.load(this, R.raw.ring3, 1) + slowSoundResourceId = soundPool.load(this, R.raw.ring4, 1) + fastSoundResourceId = soundPool.load(this, R.raw.ring2, 1) //地图初始化 initMapConfig(savedInstanceState) - //初始化 - serialPortManager = SerialPortManager(this) - taskViewModel = ViewModelProvider(this)[TaskViewModel::class.java] - - //音效资源 - soundResourceId = soundPool.load(this, R.raw.ring3, 1) - //右上角菜单 samplePopupWindow.setPopupMenuItem(LocaleConstant.POPUP_TITLES) samplePopupWindow.setBackgroundDrawable(null) @@ -181,10 +172,8 @@ val companyId = SaveKeyValues.getValue(LocaleConstant.USER_COMPANY_ID, "") val downloadUrl = companyId.toString().appendDownloadUrl(FileType.EXCEL) Log.d(kTag, "downloadUrl => $downloadUrl") - FileDownloadManager.Builder() - .setDownloadFileSource(downloadUrl) - .setFileSuffix("xls") - .setFileSaveDirectory(createDownloadFileDir()) + FileDownloadManager.Builder().setDownloadFileSource(downloadUrl) + .setFileSuffix("xls").setFileSaveDirectory(createDownloadFileDir()) .setOnFileDownloadListener(object : FileDownloadManager.OnFileDownloadListener { override fun onDownloadEnd(file: File) { lifecycleScope.launch(Dispatchers.Main) { @@ -206,9 +195,7 @@ override fun onProgressChanged(progress: Int) { } - }) - .build() - .start() + }).build().start() } } taskViewModel.taskResult.observe(this) { @@ -264,7 +251,6 @@ /** * 改为Dialog方式,避免频繁打开/关闭串口 * */ -// navigatePageTo() installDialog.show() } @@ -357,9 +343,7 @@ } }).build().show() } else { - AlertMessageDialog.Builder() - .setContext(this) - .setTitle("温馨提示") + AlertMessageDialog.Builder().setContext(this).setTitle("温馨提示") .setMessage("现有任务已完成,请点击右上角菜单下载新工单或者开始自由巡检") .setPositiveButton("开始自由巡检").setOnDialogButtonClickListener(object : AlertMessageDialog.OnDialogButtonClickListener { @@ -375,22 +359,21 @@ /** * 改为Dialog方式,避免频繁打开/关闭串口 * */ -// navigatePageTo() searchDialog.show() } //自由巡检 binding.stopFreeTaskButton.setOnLongClickListener { - AlertMessageDialog.Builder() - .setContext(this) - .setTitle("温馨提示") - .setMessage("是否确定结束此次自由巡检任务?") - .setPositiveButton("是") + AlertMessageDialog.Builder().setContext(this).setTitle("温馨提示") + .setMessage("是否确定结束此次自由巡检任务?").setPositiveButton("是") .setOnDialogButtonClickListener(object : AlertMessageDialog.OnDialogButtonClickListener { override fun onConfirmClick() { isFreeTask = false - soundPool.stop(soundResourceId) + soundPool.autoPause() + + //降低串口电位 + gpioManager.setGpioLow("18") taskViewModel.uploadTaskMarker(taskId, ids) binding.stopFreeTaskButton.visibility = View.GONE @@ -401,21 +384,28 @@ } private fun startFreeTask() { - AlertInputDialog.Builder() - .setContext(this) - .setTitle("新建自由巡检任务") - .setHintMessage("请输入自由巡检任务简要描述") - .setNegativeButton("取消") + AlertInputDialog.Builder().setContext(this).setTitle("新建自由巡检任务") + .setHintMessage("请输入自由巡检任务简要描述").setNegativeButton("取消") .setPositiveButton("确定") .setOnDialogButtonClickListener(object : AlertInputDialog.OnDialogButtonClickListener { override fun onConfirmClick(value: String) { val userId = SaveKeyValues.getValue(LocaleConstant.USER_ID, "") as String taskViewModel.createFreeTask(userId, value) + //调高串口电位 + gpioManager.setGpioHigh("18") + soundPool.play(soundResourceId, 1f, 1f, 0, -1, 1f) isFreeTask = true //自由巡检 - serialPortManager.searchMarkerSignal() + signalTask = object : TimerTask() { + override fun run() { + out.write('2'.code) + out.flush() + } + } + searchMarkerTimer = Timer() + searchMarkerTimer.schedule(signalTask, 0, 100) binding.stopFreeTaskButton.visibility = View.VISIBLE } @@ -428,17 +418,11 @@ val hex = buffer.toHex() Log.d(kTag, "$kTag => $hex") if (searchDialog.isDetectMarker) { - val message = searchDialog.weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SEARCH_MARKER_CODE - message.obj = hex - searchDialog.weakReferenceHandler.sendMessage(message) + searchDialog.bindingValue(hex) } else if (installDialog.isReadMarker) { val markerId = hex.take(20).hexToString() if (markerId.isNumber()) { - val message = installDialog.weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.INSTALL_MARKER_CODE - message.obj = markerId - installDialog.weakReferenceHandler.sendMessage(message) + installDialog.bindingValue(markerId) } } else if (isFreeTask) { val markerId = hex.take(20).hexToString() @@ -646,13 +630,9 @@ val latLngBounds = builder.build() aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 0)) - AlertControlDialog.Builder() - .setContext(this) - .setTitle("提示") - .setMessage("请选择操作方式") - .setNegativeButton("标识器信息") - .setPositiveButton("到这里去") - .setOnDialogButtonClickListener(object : + AlertControlDialog.Builder().setContext(this).setTitle("提示") + .setMessage("请选择操作方式").setNegativeButton("标识器信息") + .setPositiveButton("到这里去").setOnDialogButtonClickListener(object : AlertControlDialog.OnDialogButtonClickListener { override fun onConfirmClick() { RouteOnMap.startNavigation(context, item.title, latLng) @@ -701,13 +681,7 @@ val userId = SaveKeyValues.getValue(LocaleConstant.USER_ID, "") as String taskViewModel.uploadTask(userId, taskId, state) } else { - NoNetworkDialog.Builder().setContext(context).setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - val intent = Intent(Settings.ACTION_SETTINGS) - startActivity(intent) - } - }).build().show() + "网络连接已断开,请检查".show(this) } } @@ -719,21 +693,15 @@ AlertControlDialog.OnDialogButtonClickListener { override fun onConfirmClick() { if (isNetworkConnected()) { - val userId = SaveKeyValues.getValue(LocaleConstant.USER_ID, "") val companyId = SaveKeyValues.getValue(LocaleConstant.USER_COMPANY_ID, "") LoadingDialogHub.show(this@MainActivity, "获取标识器信息中,请稍后") - //TODO 设计如此,都传companyId + /** + * 后台设计如此,都传companyId + * */ taskViewModel.getMarkerFile(companyId.toString(), companyId.toString()) } else { - NoNetworkDialog.Builder().setContext(context) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - val intent = Intent(Settings.ACTION_SETTINGS) - startActivity(intent) - } - }).build().show() + "网络连接已断开,请检查".show(context) } } @@ -749,13 +717,7 @@ val userName = SaveKeyValues.getValue(LocaleConstant.USER_NAME, "") taskViewModel.getTask(userName.toString()) } else { - NoNetworkDialog.Builder().setContext(context).setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - val intent = Intent(Settings.ACTION_SETTINGS) - startActivity(intent) - } - }).build().show() + "网络连接已断开,请检查".show(this) } } @@ -856,26 +818,24 @@ } override fun onDestroy() { - locationHub.stopLocation() - serialPortManager.closeSerialPort() - //降低串口电位 - gpioManager.setGpioLow("18") super.onDestroy() binding.mapView.onDestroy() + signalTask.cancel() + searchMarkerTimer.cancel() + soundPool.release() + locationHub.stopLocation() + //降低串口电位 + gpioManager.setGpioLow("18") } /**安装标识器对话框******************************************************************************/ - @SuppressLint("SetTextI18n") - class InstallMarkerDialog(private val activity: MainActivity) : Dialog(activity), - Handler.Callback { + inner class InstallMarkerDialog(context: Context) : Dialog(context) { private val binding: DialogInstallMarkerBinding by binding() private val calendar by lazy { Calendar.getInstance() } private val realPaths = ArrayList() //真实图片路径 - private var soundResourceId = 0 private lateinit var imageAdapter: EditableImageAdapter - lateinit var weakReferenceHandler: WeakReferenceHandler var isReadMarker = false var isInstallMarker = false @@ -885,17 +845,12 @@ setCancelable(false) setCanceledOnTouchOutside(false) - weakReferenceHandler = WeakReferenceHandler(this) - - //初始化音频资源 - soundResourceId = soundPool.load(activity, R.raw.ring3, 1) - //初始化数据 initDefaultData() //返回 binding.titleInclude.leftBackView.setOnClickListener { - soundPool.stop(soundResourceId) + soundPool.autoPause() dismiss() } @@ -905,77 +860,66 @@ val objectId = SaveKeyValues.getValue(LocaleConstant.OBJECT_ID, "") as String if (binding.objectInclude.markerObjectTypeView.text.isNullOrBlank()) { when (binding.objectInclude.objectTypeSpinner.selectedItem.toString()) { - "管线" -> { - "请输入管线种类!".show(activity) - } - - "管线附属物" -> { - "请输入附属物名称!".show(activity) - } - - "管线特征管点" -> { - "请输入特征管点!".show(activity) - } - - "交叉穿越点" -> { - "请输入上层管种类!".show(activity) - } + "管线" -> "请输入管线种类!".show(context) + "管线附属物" -> "请输入附属物名称!".show(context) + "管线特征管点" -> "请输入特征管点!".show(context) + "交叉穿越点" -> "请输入上层管种类!".show(context) } return@setOnClickListener } if (binding.objectInclude.pipelineDiameterView.text.isNullOrBlank()) { - "请输入管径".show(activity) + "请输入管径".show(context) return@setOnClickListener } if (binding.objectInclude.buryDeepView.text.isNullOrBlank()) { - "请输入埋深".show(activity) + "请输入埋深".show(context) return@setOnClickListener } if (binding.objectInclude.downPipeDiameterView.text.isNullOrBlank()) { - "请输入下层管管径".show(activity) + "请输入下层管管径".show(context) return@setOnClickListener } if (binding.objectInclude.areaView.text.isNullOrBlank()) { - "请输入所属区域".show(activity) + "请输入所属区域".show(context) return@setOnClickListener } if (binding.objectInclude.lineView.text.isNullOrBlank()) { - "请输入所属线路".show(activity) + "请输入所属线路".show(context) return@setOnClickListener } if (binding.objectInclude.roadView.text.isNullOrBlank()) { - "请输入所属道路".show(activity) + "请输入所属道路".show(context) return@setOnClickListener } if (binding.objectInclude.constructDateView.text.isNullOrBlank()) { - "请选择建设年代".show(activity) + "请选择建设年代".show(context) return@setOnClickListener } if (binding.objectInclude.ownerView.text.isNullOrBlank()) { - "请输入权属单位".show(activity) + "请输入权属单位".show(context) return@setOnClickListener } if (binding.identifierInclude.identifierIdView.text.isNullOrBlank()) { - "请先读取标识器获取ID".show(activity) + "请先读取标识器获取ID".show(context) return@setOnClickListener } if (binding.identifierInclude.identifierDeepView.text.isNullOrBlank()) { - "请输入标识器埋深".show(activity) + "请输入标识器埋深".show(context) return@setOnClickListener } if (binding.identifierInclude.personDeptView.text.isNullOrBlank()) { - "请输入标识器安装部门".show(activity) + "请输入标识器安装部门".show(context) return@setOnClickListener } @@ -1029,31 +973,38 @@ //读标识器 binding.readMarkerButton.setOnClickListener { - LoadingDialogHub.show(activity, "标识器读取中,请稍后...") + LoadingDialogHub.show(this@MainActivity, "标识器读取中,请稍后...") + binding.readMarkerButton.isEnabled = false isReadMarker = true - serialPortManager.searchMarkerSignal() + + //调高串口电位 + gpioManager.setGpioHigh("18") + soundPool.play(soundResourceId, 1f, 1f, 0, -1, 1f) + + out.write('2'.code) + out.flush() } } - override fun handleMessage(msg: Message): Boolean { - if (msg.what == LocaleConstant.INSTALL_MARKER_CODE) { - LoadingDialogHub.dismiss() - soundPool.stop(soundResourceId) + fun bindingValue(markerId: String) { + LoadingDialogHub.dismiss() + soundPool.autoPause() + binding.readMarkerButton.isEnabled = true - val markerId = msg.obj as String - isReadMarker = false - binding.identifierInclude.identifierIdView.text = markerId - } - return true + //降低串口电位 + gpioManager.setGpioLow("18") + + isReadMarker = false + binding.identifierInclude.identifierIdView.text = markerId } private fun initDefaultData() { binding.titleInclude.titleView.text = "安装新标识器" - binding.titleInclude.titleView.setTextColor(R.color.themeColor.convertColor(activity)) + binding.titleInclude.titleView.setTextColor(R.color.themeColor.convertColor(context)) - imageAdapter = EditableImageAdapter(activity, 3, 3) + imageAdapter = EditableImageAdapter(context, 3, 3) binding.cameraInclude.addImageRecyclerView.adapter = imageAdapter //设置默认值 @@ -1071,24 +1022,25 @@ /**************************************************************************************/ binding.objectInclude.objectTypeSpinner.show( - activity, LocaleConstant.POINT_TYPE_ARRAY, 0 + this@MainActivity, LocaleConstant.POINT_TYPE_ARRAY, 0 ) binding.objectInclude.materialSpinner.show( - activity, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 + this@MainActivity, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 ) binding.objectInclude.downPipeTypeSpinner.show( - activity, LocaleConstant.DOWN_PIPE_TYPE_ARRAY, 0 + this@MainActivity, LocaleConstant.DOWN_PIPE_TYPE_ARRAY, 0 ) binding.objectInclude.downPipeMaterialSpinner.show( - activity, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 + this@MainActivity, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 ) binding.objectInclude.buryMethodSpinner.show( - activity, LocaleConstant.BURY_METHOD_ARRAY, 0 + this@MainActivity, LocaleConstant.BURY_METHOD_ARRAY, 0 ) binding.objectInclude.constructDateView.setOnClickListener { val datePicker = DatePickerDialog( - activity, null, + context, + null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) @@ -1107,7 +1059,7 @@ val current = System.currentTimeMillis().timestampToTime() val today = "$selectedDate $current".dateToTimestamp() if (Date(today).after(Date())) { - "建设年代不能早于当前日期".show(activity) + "建设年代不能早于当前日期".show(context) } else { datePicker.dismiss() binding.objectInclude.constructDateView.text = selectedDate @@ -1116,23 +1068,25 @@ } binding.identifierInclude.identifierTypeSpinner.show( - activity, LocaleConstant.IDENTIFIER_TYPE_ARRAY, 0 + this@MainActivity, LocaleConstant.IDENTIFIER_TYPE_ARRAY, 0 ) binding.identifierInclude.installTimeView.text = System.currentTimeMillis().timestampToCompleteDate() - activity.locationHub.getCurrentLocation(true, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { binding.identifierInclude.lngView.text = location.longitude.toString() binding.identifierInclude.latView.text = location.latitude.toString() } else { - "当前位置信号差,无法获取定位".show(activity) + "当前位置信号差,无法获取定位".show(context) } } }) - binding.identifierInclude.colorSpinner.show(activity, LocaleConstant.COLOR_ARRAY, 0) + binding.identifierInclude.colorSpinner.show( + this@MainActivity, LocaleConstant.COLOR_ARRAY, 0 + ) imageAdapter.setOnItemClickListener(object : EditableImageAdapter.OnItemClickListener { override fun onAddImageClick() { @@ -1141,9 +1095,9 @@ override fun onItemClick(position: Int) { if (realPaths[position].isEmpty()) { - "图片加载失败,无法查看大图".show(activity) + "图片加载失败,无法查看大图".show(context) } else { - activity.navigatePageTo(position, realPaths) + context.navigatePageTo(position, realPaths) } } @@ -1193,11 +1147,11 @@ } private fun takePicture() { - PictureSelector.create(activity).openCamera(SelectMimeType.ofImage()) + PictureSelector.create(context).openCamera(SelectMimeType.ofImage()) .forResult(object : OnResultCallbackListener { override fun onResult(result: java.util.ArrayList?) { if (result == null) { - "拍照失败,请重试".show(activity) + "拍照失败,请重试".show(context) return } analyticalSelectResults(result[0]) @@ -1211,7 +1165,7 @@ private fun analyticalSelectResults(result: LocalMedia) { //压缩图片 - result.realPath.compressImage(activity, object : OnImageCompressListener { + result.realPath.compressImage(context, object : OnImageCompressListener { override fun onSuccess(file: File) { realPaths.add(file.absolutePath) imageAdapter.setupImage(realPaths) @@ -1225,38 +1179,30 @@ } /**探测标识器对话框******************************************************************************/ - class SearchMarkerDialog(private val activity: MainActivity) : Dialog(activity), - Handler.Callback { + inner class SearchMarkerDialog(context: Context) : Dialog(context) { private val binding: DialogSearchMarkerBinding by binding() private val decimal by lazy { DecimalFormat("0.0") } private val degreeCache by lazy { HashMap() } - private var slowSoundResourceId = 0 - private var fastSoundResourceId = 0 private var isPlaying = false private var markerId = "" + private lateinit var searchMarkerTimer: Timer + private lateinit var signalTask: TimerTask + private lateinit var energyTask: TimerTask + private lateinit var countDownTimer: CountDownTimer - lateinit var weakReferenceHandler: WeakReferenceHandler var isDetectMarker = false var taskBean: TaskBean? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.initDialogLayoutParams(1f) - - weakReferenceHandler = WeakReferenceHandler(this) - - //初始化音频资源 - slowSoundResourceId = soundPool.load(activity, R.raw.ring4, 1) - fastSoundResourceId = soundPool.load(activity, R.raw.ring2, 1) - - isDetectMarker = true - serialPortManager.detectMarker() - - //角度 - degreeCache["lastDegree"] = 0f - binding.depthButton.setOnClickListener { + signalTask.cancel() + energyTask.cancel() + searchMarkerTimer.cancel() + + out.write('3'.code) val result = DataBaseManager.get.queryLabelById(markerId) if (result.isNotEmpty()) { val tag = when (result.first().identifierType) { @@ -1266,24 +1212,27 @@ else -> '1' } if (tag == '1') { - "此标识器无法读取埋深!".show(activity) + "此标识器无法读取埋深!".show(context) } else { // 发送读取标识器埋设深度指令 - LoadingDialogHub.show(activity, "正在测距,请稍后...") - serialPortManager.detectDepth(tag) - object : CountDownTimer(15 * 1000, 1000) { + LoadingDialogHub.show(this@MainActivity, "正在测距,请稍后...") + out.write(tag.code) + out.flush() + countDownTimer = object : CountDownTimer(15 * 1000, 1000) { override fun onTick(millisUntilFinished: Long) { } override fun onFinish() { LoadingDialogHub.dismiss() - "探测此标识器深度超时,请重试".show(activity) + "探测此标识器深度超时,请重试".show(context) + initTimer() } - }.start() + } + countDownTimer.start() } } else { - "标识器未安装,安装成功后才可读取埋深!".show(activity) + "标识器未安装,安装成功后才可读取埋深!".show(context) } } @@ -1291,15 +1240,27 @@ //查库 val result = DataBaseManager.get.queryLabelById(markerId) if (result.isNotEmpty()) { - activity.navigatePageTo(result.first().toJson()) + context.navigatePageTo(result.first().toJson()) } else { - activity.navigatePageTo(markerId) + context.navigatePageTo(markerId) } } } - override fun handleMessage(msg: Message): Boolean { - val hex = msg.obj as String + override fun onStart() { + super.onStart() + //调高串口电位 + gpioManager.setGpioHigh("18") + + initTimer() + + isDetectMarker = true + + //角度 + degreeCache["lastDegree"] = 0f + } + + fun bindingValue(hex: String) { if (hex.startsWith("4E")) { try { //4E转为String为N,代表能量值 @@ -1311,7 +1272,7 @@ soundPool.play(fastSoundResourceId, 1f, 1f, 0, 0, 1f) true } else { - soundPool.stop(fastSoundResourceId) + soundPool.pause(fastSoundResourceId) false } } else { @@ -1319,7 +1280,7 @@ soundPool.play(slowSoundResourceId, 1f, 1f, 0, 0, 1f) true } else { - soundPool.stop(slowSoundResourceId) + soundPool.pause(slowSoundResourceId) false } } @@ -1332,9 +1293,12 @@ degreeCache["lastDegree"]?.apply { val animation = RotateAnimation( - this, degree, - Animation.RELATIVE_TO_SELF, 0.5f, - Animation.RELATIVE_TO_SELF, 0.5f + this, + degree, + Animation.RELATIVE_TO_SELF, + 0.5f, + Animation.RELATIVE_TO_SELF, + 0.5f ) animation.duration = 0 animation.fillAfter = true @@ -1373,18 +1337,16 @@ } } else if (hex.startsWith("53")) { LoadingDialogHub.dismiss() + countDownTimer.cancel() try { val depthResponse = hex.take(10).hexToString() val depth = depthResponse.drop(4) - AlertMessageDialog.Builder() - .setContext(activity) - .setTitle("温馨提示") - .setMessage("标识器埋深:${depth}厘米") - .setPositiveButton("知道了") + AlertMessageDialog.Builder().setContext(context).setTitle("温馨提示") + .setMessage("标识器埋深:${depth}厘米").setPositiveButton("知道了") .setOnDialogButtonClickListener(object : AlertMessageDialog.OnDialogButtonClickListener { override fun onConfirmClick() { - + initTimer() } }).build().show() } catch (e: NumberFormatException) { @@ -1416,13 +1378,38 @@ } } } - return true + } + + private fun initTimer() { + searchMarkerTimer = Timer() + + signalTask = object : TimerTask() { + override fun run() { + out.write('2'.code) + out.flush() + } + } + + energyTask = object : TimerTask() { + override fun run() { + out.write('6'.code) + out.flush() + } + } + + //错开信号和能量定时器 + searchMarkerTimer.schedule(signalTask, 0, 201) + searchMarkerTimer.schedule(energyTask, 0, 251) } override fun dismiss() { + signalTask.cancel() + energyTask.cancel() + searchMarkerTimer.cancel() + //降低串口电位 + gpioManager.setGpioLow("18") isDetectMarker = false - soundPool.stop(slowSoundResourceId) - soundPool.stop(fastSoundResourceId) + soundPool.autoPause() super.dismiss() } }