Skip to content

Commit 30ecf83

Browse files
committed
绑定设备
1 parent a0b11d6 commit 30ecf83

File tree

2 files changed

+213
-142
lines changed

2 files changed

+213
-142
lines changed

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/core/activity/AddDeviceActivity.kt

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import android.widget.Toast
99
import androidx.core.app.ActivityCompat
1010
import androidx.core.content.ContextCompat
1111
import com.alibaba.fastjson.JSON
12+
import com.tencent.iot.explorer.link.core.auth.IoTAuth
13+
import com.tencent.iot.explorer.link.core.auth.callback.MyCallback
14+
import com.tencent.iot.explorer.link.core.auth.response.BaseResponse
15+
import com.tencent.iot.explorer.link.demo.App
1216
import com.tencent.iot.explorer.link.demo.BaseActivity
17+
import com.tencent.iot.explorer.link.demo.common.log.L
1318
import com.tencent.iot.explorer.link.demo.core.entity.DeviceInfo
1419
import com.tencent.iot.explorer.link.demo.databinding.ActivityAddDeviceBinding
1520

@@ -98,47 +103,39 @@ class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
98103
startActivityForResult(intent, REQUEST_SCAN_QR_CODE)
99104
}
100105

101-
/**
102-
* 模拟二维码扫描结果
103-
*/
104-
private fun simulateQRCodeScan() {
105-
// 模拟扫描到产品ID
106-
val simulatedProductId = "PRODUCT_123456789"
107-
binding.etProductId.setText(simulatedProductId)
108-
109-
Toast.makeText(this, "二维码扫描成功", Toast.LENGTH_SHORT).show()
110-
}
111-
112106
/**
113107
* 添加设备
114108
*/
115109
private fun addDevice() {
116-
// 显示加载状态
110+
if (deviceInfo?.signature.isNullOrEmpty()) {
111+
L.e("signature is empty")
112+
return
113+
}
117114
showLoading("正在添加设备...")
115+
IoTAuth.deviceImpl.scanBindDevice(
116+
App.data.getCurrentFamily().FamilyId,
117+
deviceInfo?.signature!!,
118+
object : MyCallback {
119+
override fun fail(msg: String?, reqCode: Int) {
120+
hideLoading()
121+
L.e(msg ?: "")
122+
show(msg ?: "绑定失败")
123+
}
118124

119-
// 模拟网络请求
120-
Thread {
121-
Thread.sleep(2000)
122-
123-
runOnUiThread {
124-
hideLoading()
125-
126-
// 模拟添加成功
127-
val success = true
128-
129-
if (success) {
130-
Toast.makeText(this, "设备添加成功", Toast.LENGTH_SHORT).show()
131-
132-
// 返回设备列表页面
133-
setResult(RESULT_OK, Intent().apply {
134-
putExtra("deviceInfo", JSON.toJSONString(deviceInfo))
135-
})
136-
finish()
137-
} else {
138-
Toast.makeText(this, "设备添加失败,请重试", Toast.LENGTH_SHORT).show()
125+
override fun success(response: BaseResponse, reqCode: Int) {
126+
hideLoading()
127+
L.e("绑定成功:"+JSON.toJSONString(response))
128+
if (response.isSuccess()) {
129+
show("绑定成功")
130+
setResult(RESULT_OK)
131+
finish()
132+
} else {
133+
show(response.msg)
134+
}
139135
}
140-
}
141-
}.start()
136+
})
137+
138+
// 返回设备列表页面
142139
}
143140

144141
/**

0 commit comments

Comments
 (0)