|
|
@@ -1,19 +1,19 @@
|
|
|
package com.narutohuo.xindazhou.vehicle.ui
|
|
|
|
|
|
import android.Manifest
|
|
|
-import android.content.Intent
|
|
|
import android.content.pm.PackageManager
|
|
|
import android.os.Build
|
|
|
import android.os.Bundle
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.view.ViewGroup
|
|
|
import android.widget.Button
|
|
|
import android.widget.EditText
|
|
|
import android.widget.RadioButton
|
|
|
import android.widget.Switch
|
|
|
import android.widget.TextView
|
|
|
import android.widget.Toast
|
|
|
-import androidx.activity.result.contract.ActivityResultContracts
|
|
|
import androidx.core.content.ContextCompat
|
|
|
-import com.google.android.material.bottomnavigation.BottomNavigationView
|
|
|
import com.narutohuo.xindazhou.R
|
|
|
import com.narutohuo.xindazhou.ble.api.IBLE
|
|
|
import com.narutohuo.xindazhou.ble.config.BLEConstants
|
|
|
@@ -21,22 +21,16 @@ import com.narutohuo.xindazhou.ble.BLEKit
|
|
|
import com.narutohuo.xindazhou.ble.model.BLECommand
|
|
|
import com.narutohuo.xindazhou.ble.model.SystemControlInstruction
|
|
|
import com.narutohuo.xindazhou.ble.util.toHexString
|
|
|
-import com.narutohuo.xindazhou.common.ui.BaseActivity
|
|
|
+import com.narutohuo.xindazhou.common.ui.BaseFragment
|
|
|
import com.narutohuo.xindazhou.core.log.ILog
|
|
|
-import com.narutohuo.xindazhou.databinding.ActivityVehicleBinding
|
|
|
-import com.narutohuo.xindazhou.community.ui.CommunityActivity
|
|
|
-import com.narutohuo.xindazhou.service.ui.ServiceActivity
|
|
|
-import com.narutohuo.xindazhou.shop.ui.ShopActivity
|
|
|
-import com.narutohuo.xindazhou.user.ui.UserActivity
|
|
|
+import com.narutohuo.xindazhou.databinding.FragmentVehicleBinding
|
|
|
import kotlin.OptIn
|
|
|
import kotlin.ExperimentalStdlibApi
|
|
|
import java.nio.ByteBuffer
|
|
|
import java.nio.ByteOrder
|
|
|
|
|
|
/**
|
|
|
- * 车辆控制 Activity
|
|
|
- *
|
|
|
- * 架构A:每个页面一个Activity
|
|
|
+ * 车辆控制 Fragment
|
|
|
*
|
|
|
* 主要功能:
|
|
|
* 1. 蓝牙连接管理
|
|
|
@@ -45,7 +39,7 @@ import java.nio.ByteOrder
|
|
|
* 4. 系统控制(各种开关、时间设置、氛围灯等)
|
|
|
* 5. 系统设置(钥匙学码、灵敏度、报警设置等)
|
|
|
*/
|
|
|
-class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
+class VehicleFragment : BaseFragment<FragmentVehicleBinding>() {
|
|
|
|
|
|
// 蓝牙服务
|
|
|
private lateinit var bleService: IBLE
|
|
|
@@ -112,53 +106,39 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
private lateinit var switchAlarmReport: Switch
|
|
|
private lateinit var btnSetTiltAlarmThreshold: Button
|
|
|
|
|
|
- // 权限请求器
|
|
|
- private val permissionLauncher = registerForActivityResult(
|
|
|
- ActivityResultContracts.RequestMultiplePermissions()
|
|
|
- ) { permissions ->
|
|
|
- val allGranted = permissions.all { it.value }
|
|
|
- if (allGranted) {
|
|
|
- ILog.d("VehicleActivity", "✅ 所有权限已授予,开始连接设备")
|
|
|
- performConnectToDevice()
|
|
|
- } else {
|
|
|
- val deniedPermissions = permissions.filter { !it.value }.keys.joinToString(", ")
|
|
|
- ILog.e("VehicleActivity", "❌ 权限被拒绝: $deniedPermissions")
|
|
|
- Toast.makeText(
|
|
|
- this,
|
|
|
- "需要蓝牙权限才能连接设备,请在设置中授予权限",
|
|
|
- Toast.LENGTH_LONG
|
|
|
- ).show()
|
|
|
- btnConnectBluetooth.isEnabled = true
|
|
|
- tvBluetoothStatus.text = "权限被拒绝"
|
|
|
- }
|
|
|
+ override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentVehicleBinding {
|
|
|
+ return FragmentVehicleBinding.inflate(inflater, container, false)
|
|
|
}
|
|
|
|
|
|
- override fun getViewBinding(): ActivityVehicleBinding {
|
|
|
- return ActivityVehicleBinding.inflate(layoutInflater)
|
|
|
+ override fun onCreateView(
|
|
|
+ inflater: LayoutInflater,
|
|
|
+ container: ViewGroup?,
|
|
|
+ savedInstanceState: Bundle?
|
|
|
+ ): View? {
|
|
|
+ // 初始化蓝牙服务
|
|
|
+ bleService = BLEKit.create(requireContext())
|
|
|
+
|
|
|
+ return super.onCreateView(inflater, container, savedInstanceState)
|
|
|
}
|
|
|
|
|
|
- override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
- super.onCreate(savedInstanceState)
|
|
|
-
|
|
|
- // 初始化蓝牙服务
|
|
|
- bleService = BLEKit.create(this)
|
|
|
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
+ super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
initViews()
|
|
|
setupClickListeners()
|
|
|
- setupBottomNavigation()
|
|
|
updateConnectionStatus()
|
|
|
}
|
|
|
|
|
|
- override fun onDestroy() {
|
|
|
- super.onDestroy()
|
|
|
+ override fun onDestroyView() {
|
|
|
+ super.onDestroyView()
|
|
|
// 断开连接
|
|
|
- if (bleService.isConnected()) {
|
|
|
+ if (::bleService.isInitialized && bleService.isConnected()) {
|
|
|
bleService.disconnect()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun initView() {
|
|
|
- // UI初始化在onCreate中完成
|
|
|
+ // UI初始化在onViewCreated中完成
|
|
|
}
|
|
|
|
|
|
private fun initViews() {
|
|
|
@@ -221,46 +201,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置底部导航栏
|
|
|
- */
|
|
|
- private fun setupBottomNavigation() {
|
|
|
- binding.bottomNavView.selectedItemId = R.id.vehicleFragment
|
|
|
- binding.bottomNavView.setOnItemSelectedListener { item ->
|
|
|
- when (item.itemId) {
|
|
|
- R.id.vehicleFragment -> {
|
|
|
- // 当前已在车辆模块,不需要跳转
|
|
|
- true
|
|
|
- }
|
|
|
- R.id.communityFragment -> {
|
|
|
- startActivity(Intent(this, CommunityActivity::class.java))
|
|
|
- finish()
|
|
|
- true
|
|
|
- }
|
|
|
- R.id.serviceFragment -> {
|
|
|
- startActivity(Intent(this, ServiceActivity::class.java))
|
|
|
- finish()
|
|
|
- true
|
|
|
- }
|
|
|
- R.id.shopFragment -> {
|
|
|
- startActivity(Intent(this, ShopActivity::class.java))
|
|
|
- finish()
|
|
|
- true
|
|
|
- }
|
|
|
- R.id.userFragment -> {
|
|
|
- startActivity(Intent(this, UserActivity::class.java))
|
|
|
- finish()
|
|
|
- true
|
|
|
- }
|
|
|
- else -> false
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 更新连接状态显示
|
|
|
*/
|
|
|
private fun updateConnectionStatus() {
|
|
|
- if (bleService.isConnected()) {
|
|
|
+ if (::bleService.isInitialized && bleService.isConnected()) {
|
|
|
tvBluetoothStatus.text = "蓝牙已连接"
|
|
|
btnConnectBluetooth.text = "断开"
|
|
|
} else {
|
|
|
@@ -272,13 +216,13 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
private fun setupClickListeners() {
|
|
|
// 蓝牙连接/断开
|
|
|
btnConnectBluetooth.setOnClickListener {
|
|
|
- Toast.makeText(this, "按钮被点击了", Toast.LENGTH_SHORT).show()
|
|
|
- ILog.e("VehicleActivity", "🔴🔴🔴 按钮被点击了")
|
|
|
+ Toast.makeText(requireContext(), "按钮被点击了", Toast.LENGTH_SHORT).show()
|
|
|
+ ILog.e("VehicleFragment", "🔴🔴🔴 按钮被点击了")
|
|
|
if (bleService.isConnected()) {
|
|
|
// 断开连接
|
|
|
bleService.disconnect()
|
|
|
updateConnectionStatus()
|
|
|
- Toast.makeText(this, "已断开连接", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "已断开连接", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
// 一键扫描并连接
|
|
|
connectToDevice()
|
|
|
@@ -287,22 +231,22 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
// 查询车辆信息
|
|
|
btnQueryVehicleInfo.setOnClickListener {
|
|
|
- ILog.d("VehicleActivity", "🟢 点击查询车辆信息按钮")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击查询车辆信息按钮")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.queryVehicleInfo { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 查询车辆信息响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 查询车辆信息响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
val data = response.data
|
|
|
if (data != null) {
|
|
|
parseVehicleInfo(data)
|
|
|
- Toast.makeText(this@VehicleActivity, "查询成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "查询成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "查询失败: 数据为空", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "查询失败: 数据为空", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "查询失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "查询失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -312,19 +256,19 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
// 设撤防
|
|
|
btnDefense.setOnClickListener {
|
|
|
val enabled = !isDefenseOn
|
|
|
- ILog.d("VehicleActivity", "🟢 点击设撤防按钮: enabled=$enabled")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击设撤防按钮: enabled=$enabled")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
isDefenseOn = enabled
|
|
|
|
|
|
bleService.setDefense(enabled = enabled) { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 设撤防响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 设撤防响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, if (enabled) "设防成功" else "撤防成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), if (enabled) "设防成功" else "撤防成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
isDefenseOn = !enabled // 恢复状态
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -332,20 +276,20 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
// 上下电
|
|
|
btnPower.setOnClickListener {
|
|
|
- ILog.d("VehicleActivity", "🟢 点击上下电按钮: 当前状态 isPowerOn=$isPowerOn")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击上下电按钮: 当前状态 isPowerOn=$isPowerOn")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
if (isPowerOn) {
|
|
|
// 下电
|
|
|
isPowerOn = false
|
|
|
bleService.powerOff { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 下电响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 下电响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "下电成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "下电成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
isPowerOn = true // 恢复状态
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -353,13 +297,13 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
// 上电
|
|
|
isPowerOn = true
|
|
|
bleService.powerOn { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 上电响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 上电响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "上电成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "上电成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
isPowerOn = false // 恢复状态
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -368,16 +312,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
// 寻车
|
|
|
btnFindCar.setOnClickListener {
|
|
|
- ILog.d("VehicleActivity", "🟢 点击寻车按钮")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击寻车按钮")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.findCar { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 寻车响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 寻车响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "寻车成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "寻车成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "寻车失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "寻车失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -385,16 +329,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
// 座桶锁
|
|
|
btnSeatLock.setOnClickListener {
|
|
|
- ILog.d("VehicleActivity", "🟢 点击座桶锁按钮")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击座桶锁按钮")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.unlockSeat { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 座桶锁响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 座桶锁响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "座桶锁已打开", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "座桶锁已打开", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -402,16 +346,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
// 龙头锁
|
|
|
btnHandlebarLock.setOnClickListener {
|
|
|
- ILog.d("VehicleActivity", "🟢 点击车把锁按钮")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击车把锁按钮")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.unlockHandlebar { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 车把锁响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 车把锁响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "龙头锁已打开", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "龙头锁已打开", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -419,16 +363,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
// 尾箱锁
|
|
|
btnTrunkLock.setOnClickListener {
|
|
|
- ILog.d("VehicleActivity", "🟢 点击尾箱锁按钮")
|
|
|
+ ILog.d("VehicleFragment", "🟢 点击尾箱锁按钮")
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.unlockTrunk { response ->
|
|
|
- ILog.d("VehicleActivity", "📥 尾箱锁响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
- runOnUiThread {
|
|
|
+ ILog.d("VehicleFragment", "📥 尾箱锁响应: success=${response.success}, error=${response.errorMessage}")
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "尾箱锁已打开", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "尾箱锁已打开", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -443,10 +387,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
}
|
|
|
|
|
|
bleService.setSensorUnlock(enabled = isChecked) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchInductionUnlock.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -461,10 +405,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.STOLEN_LOCK, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchTheftLock.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -479,10 +423,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.SEAT_SENSOR, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchSeatSensor.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -497,10 +441,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.SOUND_EFFECT, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchVehicleSound.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -515,10 +459,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.AUTO_HEADLIGHT, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchAutoHeadlight.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -532,10 +476,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
}
|
|
|
|
|
|
bleService.setAmbientLight(enabled = isChecked) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchAtmosphereLight.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -550,10 +494,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.VOICE_BROADCAST, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchVoiceBroadcast.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -568,10 +512,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.WALK_ME_HOME, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchFollowMeHome.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -586,10 +530,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.ABS, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchABS.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -604,10 +548,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val action = if (isChecked) 0x01.toByte() else 0x00.toByte()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.TCS, byteArrayOf(action)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchTCS.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -620,16 +564,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val time = etAutoPowerOffTime.text.toString().toIntOrNull() ?: 300
|
|
|
if (time < 0 || time > 60000) {
|
|
|
- Toast.makeText(this, "时间范围:0-60000秒", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "时间范围:0-60000秒", Toast.LENGTH_SHORT).show()
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
|
|
|
bleService.setAutoPowerOffTime(timeSeconds = time) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -641,17 +585,17 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val time = etAutoDefenseTime.text.toString().toIntOrNull() ?: 5
|
|
|
if (time < 0 || time > 60000) {
|
|
|
- Toast.makeText(this, "时间范围:0-60000秒", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "时间范围:0-60000秒", Toast.LENGTH_SHORT).show()
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
|
|
|
val data = ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(time.toShort()).array()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.AUTO_DEFENSE_TIME, data) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -663,17 +607,17 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val time = etEnterPTime.text.toString().toIntOrNull() ?: 300
|
|
|
if (time < 0 || time > 60000) {
|
|
|
- Toast.makeText(this, "时间范围:0-60000秒", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "时间范围:0-60000秒", Toast.LENGTH_SHORT).show()
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
|
|
|
val data = ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(time.toShort()).array()
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.ENTER_P_TIME, data) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -686,16 +630,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val volume = etBluetoothSpeakerVolume.text.toString().toIntOrNull() ?: 50
|
|
|
if (volume < 0 || volume > 100) {
|
|
|
- Toast.makeText(this, "音量范围:0-100%", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "音量范围:0-100%", Toast.LENGTH_SHORT).show()
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
|
|
|
bleService.setSpeakerVolume(volume = volume) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -707,16 +651,16 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
|
|
|
val threshold = etLowBatteryThreshold.text.toString().toIntOrNull() ?: 20
|
|
|
if (threshold < 0 || threshold > 100) {
|
|
|
- Toast.makeText(this, "阈值范围:0-100%", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "阈值范围:0-100%", Toast.LENGTH_SHORT).show()
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.LOW_BATTERY_ALARM, byteArrayOf(threshold.toByte())) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -731,11 +675,11 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
val b = etAtmosphereLightB.text.toString().toIntOrNull()?.coerceIn(0, 255) ?: 255
|
|
|
|
|
|
bleService.setAmbientLightColor(r = r, g = g, b = b) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -746,11 +690,11 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.RSSI_COLLECTION, byteArrayOf(0x01)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "RSSI采集成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "RSSI采集成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "采集失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "采集失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -761,11 +705,11 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
if (!checkConnection()) return@setOnClickListener
|
|
|
|
|
|
bleService.sendSystemControlCommand(SystemControlInstruction.RESET_MILEAGE, byteArrayOf(0x01)) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "里程清零成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "里程清零成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -783,11 +727,11 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
)
|
|
|
|
|
|
bleService.sendCommand(command) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "钥匙学码成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "钥匙学码成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -813,11 +757,11 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
)
|
|
|
|
|
|
bleService.sendCommand(command) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -838,10 +782,10 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
)
|
|
|
|
|
|
bleService.sendCommand(command) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (!response.success) {
|
|
|
switchAlarmReport.isChecked = !isChecked
|
|
|
- Toast.makeText(this@VehicleActivity, "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "操作失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -865,11 +809,11 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
)
|
|
|
|
|
|
bleService.sendCommand(command) { response ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
if (response.success) {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置成功", Toast.LENGTH_SHORT).show()
|
|
|
} else {
|
|
|
- Toast.makeText(this@VehicleActivity, "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "设置失败: ${response.errorMessage}", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -881,7 +825,7 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
*/
|
|
|
private fun checkConnection(): Boolean {
|
|
|
if (!bleService.isConnected()) {
|
|
|
- Toast.makeText(this, "请先连接蓝牙设备", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "请先连接蓝牙设备", Toast.LENGTH_SHORT).show()
|
|
|
return false
|
|
|
}
|
|
|
return true
|
|
|
@@ -891,7 +835,7 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
* 连接到设备(一键完成扫描+连接+握手)
|
|
|
*/
|
|
|
private fun connectToDevice() {
|
|
|
- ILog.d("VehicleActivity", "========== 开始连接设备 ==========")
|
|
|
+ ILog.d("VehicleFragment", "========== 开始连接设备 ==========")
|
|
|
|
|
|
// 检查并请求权限
|
|
|
val requiredPermissions = mutableListOf<String>()
|
|
|
@@ -906,18 +850,37 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
}
|
|
|
|
|
|
val missingPermissions = requiredPermissions.filter { permission ->
|
|
|
- ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED
|
|
|
+ ContextCompat.checkSelfPermission(requireContext(), permission) != PackageManager.PERMISSION_GRANTED
|
|
|
}
|
|
|
|
|
|
if (missingPermissions.isNotEmpty()) {
|
|
|
- ILog.w("VehicleActivity", "缺少权限: ${missingPermissions.joinToString(", ")}")
|
|
|
+ ILog.w("VehicleFragment", "缺少权限: ${missingPermissions.joinToString(", ")}")
|
|
|
btnConnectBluetooth.isEnabled = false
|
|
|
tvBluetoothStatus.text = "请求权限..."
|
|
|
- permissionLauncher.launch(missingPermissions.toTypedArray())
|
|
|
+ // 通过MainActivity请求权限
|
|
|
+ (requireActivity() as? PermissionRequestHandler)?.requestPermissions(
|
|
|
+ missingPermissions.toTypedArray()
|
|
|
+ ) { permissions ->
|
|
|
+ val allGranted = permissions.all { it.value }
|
|
|
+ if (allGranted) {
|
|
|
+ ILog.d("VehicleFragment", "✅ 所有权限已授予,开始连接设备")
|
|
|
+ performConnectToDevice()
|
|
|
+ } else {
|
|
|
+ val deniedPermissions = permissions.filter { !it.value }.keys.joinToString(", ")
|
|
|
+ ILog.e("VehicleFragment", "❌ 权限被拒绝: $deniedPermissions")
|
|
|
+ Toast.makeText(
|
|
|
+ requireContext(),
|
|
|
+ "需要蓝牙权限才能连接设备,请在设置中授予权限",
|
|
|
+ Toast.LENGTH_LONG
|
|
|
+ ).show()
|
|
|
+ btnConnectBluetooth.isEnabled = true
|
|
|
+ tvBluetoothStatus.text = "权限被拒绝"
|
|
|
+ }
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- ILog.d("VehicleActivity", "✅ 所有权限已授予")
|
|
|
+ ILog.d("VehicleFragment", "✅ 所有权限已授予")
|
|
|
performConnectToDevice()
|
|
|
}
|
|
|
|
|
|
@@ -931,33 +894,33 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
val userId = ByteArray(16) { it.toByte() }
|
|
|
val userType = BLEConstants.USER_TYPE_OWNER
|
|
|
|
|
|
- ILog.d("VehicleActivity", "开始扫描并连接设备...")
|
|
|
+ ILog.d("VehicleFragment", "开始扫描并连接设备...")
|
|
|
|
|
|
bleService.initializeAndConnect(
|
|
|
userId = userId,
|
|
|
userType = userType,
|
|
|
onConnected = {
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
btnConnectBluetooth.isEnabled = true
|
|
|
updateConnectionStatus()
|
|
|
- Toast.makeText(this@VehicleActivity, "连接成功", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "连接成功", Toast.LENGTH_SHORT).show()
|
|
|
btnQueryVehicleInfo.performClick()
|
|
|
}
|
|
|
},
|
|
|
onDisconnected = {
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
updateConnectionStatus()
|
|
|
- Toast.makeText(this@VehicleActivity, "连接已断开", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "连接已断开", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
},
|
|
|
onDataReceived = { data ->
|
|
|
handleReceivedData(data)
|
|
|
},
|
|
|
onError = { error ->
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
btnConnectBluetooth.isEnabled = true
|
|
|
updateConnectionStatus()
|
|
|
- Toast.makeText(this@VehicleActivity, "连接失败: $error", Toast.LENGTH_SHORT).show()
|
|
|
+ Toast.makeText(requireContext(), "连接失败: $error", Toast.LENGTH_SHORT).show()
|
|
|
}
|
|
|
}
|
|
|
)
|
|
|
@@ -969,47 +932,47 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
@OptIn(ExperimentalStdlibApi::class)
|
|
|
private fun handleReceivedData(data: ByteArray) {
|
|
|
val hexString = data.toHexString()
|
|
|
- ILog.d("VehicleActivity", "🔔🔔🔔 Android收到设备响应 (${data.size} bytes): $hexString")
|
|
|
+ ILog.d("VehicleFragment", "🔔🔔🔔 Android收到设备响应 (${data.size} bytes): $hexString")
|
|
|
|
|
|
if (data.size == 1) {
|
|
|
val byte = data[0]
|
|
|
- ILog.d("VehicleActivity", "⚠️ 收到单字节响应: 0x${String.format("%02x", byte)}")
|
|
|
+ ILog.d("VehicleFragment", "⚠️ 收到单字节响应: 0x${String.format("%02x", byte)}")
|
|
|
|
|
|
if (byte == 0x33.toByte()) {
|
|
|
- ILog.e("VehicleActivity", "❌ 设备返回认证失败 (0x33 = '3')")
|
|
|
+ ILog.e("VehicleFragment", "❌ 设备返回认证失败 (0x33 = '3')")
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
val jsonString = String(data, Charsets.UTF_8)
|
|
|
- ILog.d("VehicleActivity", "📥 数据内容(JSON): $jsonString")
|
|
|
+ ILog.d("VehicleFragment", "📥 数据内容(JSON): $jsonString")
|
|
|
val jsonObject = org.json.JSONObject(jsonString)
|
|
|
|
|
|
- runOnUiThread {
|
|
|
+ requireActivity().runOnUiThread {
|
|
|
val ty = jsonObject.optInt("TY", 0)
|
|
|
val `in` = jsonObject.optInt("IN", 0)
|
|
|
|
|
|
when {
|
|
|
ty == 3 && `in` == 3 -> {
|
|
|
- ILog.d("VehicleActivity", "收到握手请求")
|
|
|
+ ILog.d("VehicleFragment", "收到握手请求")
|
|
|
}
|
|
|
ty == 3 && `in` == 4 -> {
|
|
|
val pairing = jsonObject.optJSONObject("C")?.optJSONObject("D")?.optInt("Pairing", 0) ?: 0
|
|
|
- ILog.d("VehicleActivity", "📥 收到配对请求,Pairing=$pairing")
|
|
|
+ ILog.d("VehicleFragment", "📥 收到配对请求,Pairing=$pairing")
|
|
|
when (pairing) {
|
|
|
- 1 -> ILog.d("VehicleActivity", "✅ 设备需要配对 (Pairing=1)")
|
|
|
- 2 -> ILog.d("VehicleActivity", "✅ 设备不需要配对 (Pairing=2)")
|
|
|
- 3 -> ILog.e("VehicleActivity", "❌ 认证失败 (Pairing=3)")
|
|
|
+ 1 -> ILog.d("VehicleFragment", "✅ 设备需要配对 (Pairing=1)")
|
|
|
+ 2 -> ILog.d("VehicleFragment", "✅ 设备不需要配对 (Pairing=2)")
|
|
|
+ 3 -> ILog.e("VehicleFragment", "❌ 认证失败 (Pairing=3)")
|
|
|
}
|
|
|
}
|
|
|
else -> {
|
|
|
- ILog.d("VehicleActivity", "收到业务消息: $jsonString")
|
|
|
+ ILog.d("VehicleFragment", "收到业务消息: $jsonString")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (e: Exception) {
|
|
|
- ILog.e("VehicleActivity", "解析接收数据失败", e)
|
|
|
+ ILog.e("VehicleFragment", "解析接收数据失败", e)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1046,3 +1009,15 @@ class VehicleActivity : BaseActivity<ActivityVehicleBinding>() {
|
|
|
switchTCS.isChecked = data.size > 39 && data[39] == 0x01.toByte()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 权限请求接口
|
|
|
+ * 由MainActivity实现,供Fragment使用
|
|
|
+ */
|
|
|
+interface PermissionRequestHandler {
|
|
|
+ fun requestPermissions(
|
|
|
+ permissions: Array<String>,
|
|
|
+ callback: (Map<String, Boolean>) -> Unit
|
|
|
+ )
|
|
|
+}
|
|
|
+
|