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 1a37e6f..92ab97c 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -16,8 +16,8 @@ protected lateinit var binding: VB private var serialPort: SerialPort? = null - var outputStream: OutputStream? = null - private var inputStream: InputStream? = null + var outStream: OutputStream? = null + private var inStream: InputStream? = null private var readThread: ReadThread? = null inner class ReadThread : Thread() { @@ -27,8 +27,8 @@ var size: Int try { val buffer = ByteArray(32) - if (inputStream == null) return - size = inputStream!!.read(buffer) + if (inStream == null) return + size = inStream!!.read(buffer) if (size > 0) { onDataReceived(buffer, size) } @@ -51,8 +51,8 @@ try { serialPort = BaseApplication.get().getSerialPort() - outputStream = serialPort?.outputStream - inputStream = serialPort?.inputStream + outStream = serialPort?.outputStream + inStream = serialPort?.inputStream readThread = ReadThread() readThread?.start() 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 1a37e6f..92ab97c 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -16,8 +16,8 @@ protected lateinit var binding: VB private var serialPort: SerialPort? = null - var outputStream: OutputStream? = null - private var inputStream: InputStream? = null + var outStream: OutputStream? = null + private var inStream: InputStream? = null private var readThread: ReadThread? = null inner class ReadThread : Thread() { @@ -27,8 +27,8 @@ var size: Int try { val buffer = ByteArray(32) - if (inputStream == null) return - size = inputStream!!.read(buffer) + if (inStream == null) return + size = inStream!!.read(buffer) if (size > 0) { onDataReceived(buffer, size) } @@ -51,8 +51,8 @@ try { serialPort = BaseApplication.get().getSerialPort() - outputStream = serialPort?.outputStream - inputStream = serialPort?.inputStream + outStream = serialPort?.outputStream + inStream = serialPort?.inputStream readThread = ReadThread() readThread?.start() diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index 99905ec..fcab886 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -319,8 +319,8 @@ weakReferenceHandler.postDelayed({ try { // 发送读标识器或搜索信号 - outputStream?.write("2".toByteArray()) - outputStream?.flush() + outStream?.write("2".toByteArray()) + outStream?.flush() } catch (e: IOException) { e.printStackTrace() } 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 1a37e6f..92ab97c 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -16,8 +16,8 @@ protected lateinit var binding: VB private var serialPort: SerialPort? = null - var outputStream: OutputStream? = null - private var inputStream: InputStream? = null + var outStream: OutputStream? = null + private var inStream: InputStream? = null private var readThread: ReadThread? = null inner class ReadThread : Thread() { @@ -27,8 +27,8 @@ var size: Int try { val buffer = ByteArray(32) - if (inputStream == null) return - size = inputStream!!.read(buffer) + if (inStream == null) return + size = inStream!!.read(buffer) if (size > 0) { onDataReceived(buffer, size) } @@ -51,8 +51,8 @@ try { serialPort = BaseApplication.get().getSerialPort() - outputStream = serialPort?.outputStream - inputStream = serialPort?.inputStream + outStream = serialPort?.outputStream + inStream = serialPort?.inputStream readThread = ReadThread() readThread?.start() diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index 99905ec..fcab886 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -319,8 +319,8 @@ weakReferenceHandler.postDelayed({ try { // 发送读标识器或搜索信号 - outputStream?.write("2".toByteArray()) - outputStream?.flush() + outStream?.write("2".toByteArray()) + outStream?.flush() } catch (e: IOException) { e.printStackTrace() } diff --git a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt index 4a5eea2..e29f411 100644 --- a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt @@ -10,11 +10,9 @@ import android.view.animation.Animation import android.view.animation.RotateAnimation import androidx.lifecycle.ViewModelProvider -import com.amap.api.location.AMapLocation import com.casic.detector.R import com.casic.detector.base.SerialPortActivity import com.casic.detector.bean.TaskBean -import com.casic.detector.callback.ILocationListener import com.casic.detector.databinding.ActivitySearchLabelBinding import com.casic.detector.extensions.hexToString import com.casic.detector.extensions.initLayoutImmersionBar @@ -56,6 +54,7 @@ private var isDetectDepth = false private lateinit var taskViewModel: TaskViewModel private var taskLabel: TaskBean? = null + private val dataReceivedCode = 2023110301 override fun initOnCreate(savedInstanceState: Bundle?) { gpioManager.setGpioHigh("18") @@ -79,42 +78,50 @@ fastResId = fastSoundPool.load(this, R.raw.ring2, 1) //实时定位 - locationHub.getCurrentLocation(false, object : ILocationListener { - override fun onAMapLocationGet(location: AMapLocation?) { - if (location != null) { - Log.d(kTag, "onAMapLocationGet => ${location.longitude},${location.latitude}") - //116.265904,39.914204 - } - } - }) +// locationHub.getCurrentLocation(false, object : ILocationListener { +// override fun onAMapLocationGet(location: AMapLocation?) { +// if (location != null) { +// if (identifierId != "") { +// val label = DataBaseManager.get.queryLabelById(identifierId).first() +// +// val result = location.calculateAngle( +// LatLng(label.lat.toDouble(), label.lng.toDouble()) +// ) +// +// binding.resultTextView.text = +// "标识器ID ${identifierId},${result.direction} ${result.angle},距离 ${result.distance}" +// } +// } +// } +// }) - //搜索信号 + //搜索信号,发射一次指令即可 detectTimer.schedule(object : TimerTask() { override fun run() { try { if (!isDetectDepth) { - outputStream?.write('2'.code) - outputStream?.flush() - } - } catch (e: IOException) { - e.printStackTrace() - } - } - }, 150, 100) - - //发送搜索能量指令 - detectTimer.schedule(object : TimerTask() { - override fun run() { - try { - if (!isDetectDepth) { - outputStream?.write('6'.code) - outputStream?.flush() + outStream?.write('2'.code) + outStream?.flush() } } catch (e: IOException) { e.printStackTrace() } } }, 100, 100) + + //发送搜索能量指令 + detectTimer.schedule(object : TimerTask() { + override fun run() { + try { + if (!isDetectDepth) { + outStream?.write('6'.code) + outStream?.flush() + } + } catch (e: IOException) { + e.printStackTrace() + } + } + }, 150, 100) } override fun initEvent() { @@ -131,11 +138,11 @@ if (tag == '1') { "此标识器无法读取埋深!".show(this) } else { - // 发送读取标识器埋设深度指令* + // 发送读取标识器埋设深度指令 LoadingDialogHub.show(this, "正在测距,请稍后...") try { - outputStream?.write(tag.code) - outputStream?.flush() + outStream?.write(tag.code) + outStream?.flush() } catch (e: IOException) { e.printStackTrace() } @@ -158,16 +165,16 @@ override fun onDataReceived(buffer: ByteArray?, size: Int) { val message = weakReferenceHandler.obtainMessage() - message.what = 2023061901 + message.what = dataReceivedCode message.obj = buffer weakReferenceHandler.sendMessage(message) } override fun handleMessage(msg: Message): Boolean { - if (msg.what == 2023061901) { + if (msg.what == dataReceivedCode) { val buffer = msg.obj as ByteArray val hex = buffer.toHex() -// Log.d(kTag, "hex => $hex") + Log.d(kTag, "hex => $hex") //能量强度 //4E30363634000000000000000000000000000000000000000000000000000000 @@ -189,7 +196,7 @@ energy = 50 } -// Log.d(kTag, "handleMessage => $energy") + binding.resultTextView.text = "信号能量值:${energyResponse}" //转换为转动的角度 val degree = (energy.toFloat() / 50) * 180 @@ -228,32 +235,12 @@ } } - if (energy <= 13) {//18° + if (energy <= 7) {//18° binding.energyTipsView.text = "标识器信号较弱,可能距离较远" binding.energyTipsView.setTextColor(Color.parseColor("#8D1717")) binding.energyTipsView.setBackgroundResource(R.mipmap.bg_large_text_red) - } else if (energy >= 41) {//148° - binding.energyTipsView.text = "标识器信号极强,可能在正下方" - binding.energyTipsView.setTextColor(Color.parseColor("#428d00")) - binding.energyTipsView.setBackgroundResource(R.mipmap.bg_large_text_green) - } else {//[18°,148°] - binding.energyTipsView.text = "已靠近标识器,请继续移动位置" - binding.energyTipsView.setTextColor(Color.parseColor("#8C5700")) - binding.energyTipsView.setBackgroundResource(R.mipmap.bg_large_text_yellow) - } - if (energy >= 7 && identifierId.isNotEmpty()) { - binding.depthButton.isEnabled = true - binding.depthButton.setTextColor(Color.WHITE) - binding.depthButton.setBackgroundResource(R.mipmap.left_button_enable) - binding.markerInfoButton.isEnabled = true - binding.markerInfoButton.setTextColor(Color.WHITE) - binding.markerInfoButton.setBackgroundResource(R.mipmap.right_button_enable) - binding.searchResultView.text = "已检测到标识器" - binding.searchResultView.setTextColor(Color.parseColor("#428d00")) - binding.searchResultView.setBackgroundResource(R.mipmap.bg_small_text_green) - } else { binding.depthButton.isEnabled = false binding.depthButton.setTextColor(Color.parseColor("#CCCCCC")) binding.depthButton.setBackgroundResource(R.mipmap.left_button_disable) @@ -264,9 +251,14 @@ binding.searchResultView.text = "未检测到标识器" binding.searchResultView.setTextColor(Color.parseColor("#8D1717")) binding.searchResultView.setBackgroundResource(R.mipmap.bg_small_text_red) - - //未探测到就清空标识器ID - identifierId = "" + } else if (energy >= 41) {//148° + binding.energyTipsView.text = "标识器信号极强,可能在正下方" + binding.energyTipsView.setTextColor(Color.parseColor("#428d00")) + binding.energyTipsView.setBackgroundResource(R.mipmap.bg_large_text_green) + } else {//[18°,148°] + binding.energyTipsView.text = "已靠近标识器,请继续移动位置" + binding.energyTipsView.setTextColor(Color.parseColor("#8C5700")) + binding.energyTipsView.setBackgroundResource(R.mipmap.bg_large_text_yellow) } } catch (e: NumberFormatException) { e.printStackTrace() @@ -289,10 +281,21 @@ e.printStackTrace() } } else { - val identifierId = hex.substring(0, 20).hexToString() + val id = hex.substring(0, 20).hexToString() val regex = "^[a-z0-9A-Z]+$" - if (identifierId.matches(Regex(regex))) { - this.identifierId = identifierId + if (id.matches(Regex(regex))) { + this.identifierId = id + + binding.depthButton.isEnabled = true + binding.depthButton.setTextColor(Color.WHITE) + binding.depthButton.setBackgroundResource(R.mipmap.left_button_enable) + binding.markerInfoButton.isEnabled = true + binding.markerInfoButton.setTextColor(Color.WHITE) + binding.markerInfoButton.setBackgroundResource(R.mipmap.right_button_enable) + + binding.searchResultView.text = "已检测到标识器" + binding.searchResultView.setTextColor(Color.parseColor("#428d00")) + binding.searchResultView.setBackgroundResource(R.mipmap.bg_small_text_green) //自动上传标识器 taskLabel = DataBaseManager.get.queryTaskLabelByIdAndState( @@ -332,6 +335,6 @@ super.onDestroy() gpioManager.setGpioLow("18") detectTimer.cancel() - locationHub.stopLocation() +// locationHub.stopLocation() } } \ No newline at end of file