@@ -8,7 +8,9 @@ import android.util.Log
88import android.widget.Toast
99import androidx.core.app.ActivityCompat
1010import androidx.core.content.ContextCompat
11+ import com.alibaba.fastjson.JSON
1112import com.tencent.iot.explorer.link.demo.BaseActivity
13+ import com.tencent.iot.explorer.link.demo.core.entity.DeviceInfo
1214import com.tencent.iot.explorer.link.demo.databinding.ActivityAddDeviceBinding
1315
1416/* *
@@ -17,10 +19,12 @@ import com.tencent.iot.explorer.link.demo.databinding.ActivityAddDeviceBinding
1719class AddDeviceActivity : BaseActivity <ActivityAddDeviceBinding >() {
1820
1921 companion object {
20- private const val REQUEST_CAMERA_PERMISSION = 100
22+ private const val REQUEST_CAMERA_PERMISSION = 102
2123 private const val REQUEST_SCAN_QR_CODE = 101
2224 }
2325
26+ private var deviceInfo: DeviceInfo ? = null
27+
2428 override fun getViewBinding (): ActivityAddDeviceBinding =
2529 ActivityAddDeviceBinding .inflate(layoutInflater)
2630
@@ -43,6 +47,10 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
4347
4448 // 添加设备按钮
4549 btnAddDevice.setOnClickListener {
50+ if (deviceInfo == null ) {
51+ Toast .makeText(this @AddDeviceActivity, " 请扫描二维码" , Toast .LENGTH_SHORT ).show()
52+ return @setOnClickListener
53+ }
4654 addDevice()
4755 }
4856 }
@@ -55,7 +63,7 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
5563 with (binding) {
5664 // 设置默认设备名称
5765 etDeviceName.setText(" 智能摄像机" )
58-
66+
5967 // 设置默认设备位置
6068 etDeviceLocation.setText(" 客厅" )
6169 }
@@ -66,8 +74,9 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
6674 */
6775 private fun startQRCodeScan () {
6876 // 检查相机权限
69- if (ContextCompat .checkSelfPermission(this , Manifest .permission.CAMERA )
70- != PackageManager .PERMISSION_GRANTED ) {
77+ if (ContextCompat .checkSelfPermission(this , Manifest .permission.CAMERA )
78+ != PackageManager .PERMISSION_GRANTED
79+ ) {
7180 // 请求相机权限
7281 ActivityCompat .requestPermissions(
7382 this ,
@@ -96,62 +105,34 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
96105 // 模拟扫描到产品ID
97106 val simulatedProductId = " PRODUCT_123456789"
98107 binding.etProductId.setText(simulatedProductId)
99-
108+
100109 Toast .makeText(this , " 二维码扫描成功" , Toast .LENGTH_SHORT ).show()
101110 }
102111
103112 /* *
104113 * 添加设备
105114 */
106115 private fun addDevice () {
107- with (binding) {
108- val productId = etProductId.text.toString().trim()
109- val deviceName = etDeviceName.text.toString().trim()
110- val deviceLocation = etDeviceLocation.text.toString().trim()
111-
112- // 验证输入
113- if (TextUtils .isEmpty(productId)) {
114- Toast .makeText(this @AddDeviceActivity, " 请输入产品ID" , Toast .LENGTH_SHORT ).show()
115- etProductId.requestFocus()
116- return
117- }
118-
119- if (TextUtils .isEmpty(deviceName)) {
120- Toast .makeText(this @AddDeviceActivity, " 请输入设备名称" , Toast .LENGTH_SHORT ).show()
121- etDeviceName.requestFocus()
122- return
123- }
124-
125- // 添加设备到系统
126- addDeviceToSystem(productId, deviceName, deviceLocation)
127- }
128- }
129-
130- /* *
131- * 添加设备到系统
132- */
133- private fun addDeviceToSystem (productId : String , deviceName : String , deviceLocation : String ) {
134- // TODO: 实现添加设备到系统的逻辑
135- // 这里应该调用相应的API接口来添加设备
136-
137116 // 显示加载状态
138117 showLoading(" 正在添加设备..." )
139-
118+
140119 // 模拟网络请求
141120 Thread {
142121 Thread .sleep(2000 )
143-
122+
144123 runOnUiThread {
145124 hideLoading()
146-
125+
147126 // 模拟添加成功
148127 val success = true
149-
128+
150129 if (success) {
151130 Toast .makeText(this , " 设备添加成功" , Toast .LENGTH_SHORT ).show()
152-
131+
153132 // 返回设备列表页面
154- setResult(RESULT_OK )
133+ setResult(RESULT_OK , Intent ().apply {
134+ putExtra(" deviceInfo" , JSON .toJSONString(deviceInfo))
135+ })
155136 finish()
156137 } else {
157138 Toast .makeText(this , " 设备添加失败,请重试" , Toast .LENGTH_SHORT ).show()
@@ -163,31 +144,30 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
163144 /* *
164145 * 处理权限请求结果
165146 */
166- // override fun onRequestPermissionsResult(
167- // requestCode: Int,
168- // permissions: Array<out String>,
169- // grantResults: IntArray
170- // ) {
171- // super.onRequestPermissionsResult(requestCode, permissions, grantResults)
172- //
173- // when (requestCode) {
174- // REQUEST_CAMERA_PERMISSION -> {
175- // if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
176- // // 权限已授予,启动二维码扫描
177- // launchQRCodeScanner()
178- // } else {
179- // Toast.makeText(this, "需要相机权限才能扫描二维码", Toast.LENGTH_SHORT).show()
180- // }
181- // }
182- // }
183- // }
147+ override fun handleRequestPermissionsResult (
148+ requestCode : Int ,
149+ permissions : Array <out String >,
150+ grantResults : IntArray
151+ ) {
152+
153+ when (requestCode) {
154+ REQUEST_CAMERA_PERMISSION -> {
155+ if (grantResults.isNotEmpty() && grantResults[0 ] == PackageManager .PERMISSION_GRANTED ) {
156+ // 权限已授予,启动二维码扫描
157+ launchQRCodeScanner()
158+ } else {
159+ Toast .makeText(this , " 需要相机权限才能扫描二维码" , Toast .LENGTH_SHORT ).show()
160+ }
161+ }
162+ }
163+ }
184164
185165 /* *
186166 * 处理Activity结果
187167 */
188168 override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
189169 super .onActivityResult(requestCode, resultCode, data)
190-
170+
191171 when (requestCode) {
192172 REQUEST_SCAN_QR_CODE -> {
193173 if (resultCode == RESULT_OK ) {
@@ -204,7 +184,7 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
204184 private fun handleQRCodeResult (data : Intent ? ) {
205185 // 从Intent中获取二维码扫描结果
206186 val qrCodeResult = data?.getStringExtra(" QR_CODE_RESULT" ) ? : " "
207-
187+
208188 if (qrCodeResult.isNotEmpty()) {
209189 // 解析二维码内容并填充到表单
210190 parseQRCodeContent(qrCodeResult)
@@ -219,9 +199,27 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
219199 */
220200 private fun parseQRCodeContent (qrContent : String ) {
221201 Log .d(" TAG" , " parseQRCodeContent: $qrContent " )
222-
202+ try {
203+ deviceInfo = DeviceInfo ()
204+ val jsonObject = JSON .parseObject(qrContent)
205+
206+ // 设置DeviceEntity的基本属性
207+ if (jsonObject.containsKey(" DeviceName" )) {
208+ deviceInfo?.deviceName = jsonObject.getString(" DeviceName" )
209+ }
210+
211+ if (jsonObject.containsKey(" ProductId" )) {
212+ deviceInfo?.productId = jsonObject.getString(" ProductId" )
213+ }
214+ if (jsonObject.containsKey(" ProductId" )) {
215+ deviceInfo?.signature = jsonObject.getString(" Signature" )
216+ }
217+ } catch (e: Exception ) {
218+ e.printStackTrace()
219+ }
223220 // 临时实现:假设二维码内容就是产品ID
224- binding.etProductId.setText(qrContent)
221+ binding.etProductId.setText(deviceInfo?.productId)
222+ binding.etDeviceName.setText(deviceInfo?.deviceName)
225223 }
226224
227225 /* *
@@ -257,8 +255,8 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
257255 private fun hasUnsavedInput (): Boolean {
258256 with (binding) {
259257 return etProductId.text.isNotEmpty() ||
260- etDeviceName.text.isNotEmpty() ||
261- etDeviceLocation.text.isNotEmpty()
258+ etDeviceName.text.isNotEmpty() ||
259+ etDeviceLocation.text.isNotEmpty()
262260 }
263261 }
264262
@@ -268,7 +266,7 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
268266 private fun showUnsavedChangesDialog () {
269267 // TODO: 实现未保存更改确认对话框
270268 // 这里应该询问用户是否放弃已输入的内容
271-
269+
272270 // 临时实现:直接返回
273271 super .onBackPressed()
274272 }
0 commit comments