Skip to content

Commit 835f6c9

Browse files
committed
局域网探测(UI 入口)
http://tapd.oa.com/NEW_IOT/prong/stories/view/1020393192869350657 Change-Id: Iff738760c1e451fd04aec8f13404ed2dbdf66abe
1 parent e6b0b69 commit 835f6c9

File tree

18 files changed

+710
-39
lines changed

18 files changed

+710
-39
lines changed

sdk/video-link-android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ dependencies {
4848
// api('com.tencent.iot.thirdparty.android:xp2p-sdk:latest.integration') {
4949
// changing = true
5050
// }
51-
api 'com.tencent.iot.thirdparty.android:xp2p-sdk:2.3.4-SNAPSHOT'
51+
api 'com.tencent.iot.thirdparty.android:xp2p-sdk:2.3.6-SNAPSHOT'
52+
api 'com.tencent.iot.thirdparty.android:media-server:1.0.0-SNAPSHOT'
5253
}
5354

5455
configurations.all {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.tencent.iot.video.link.callback
2+
3+
import com.tencent.iot.video.link.entity.WlanRespBody
4+
5+
interface OnWlanDevicesDetectedCallback {
6+
fun onMessage(version: String, resp: WlanRespBody): Boolean
7+
}

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/consts/VideoConst.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.tencent.iot.video.link.consts
33
object VideoConst {
44
/***********video**********/
55
const val VIDEO_CONFIG = "VideoConfig"
6+
const val VIDEO_WLAN_CONFIG = "VideoWlanConfig"
67
const val VIDEO_SECRET_ID = "VideoSecretId"
78
const val VIDEO_SECRET_KEY = "VideoSecretKey"
89
const val VIDEO_PRODUCT_ID = "VideoProductId"

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/entity/DeviceServerInfo.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ class DeviceServerInfo {
2222

2323
return true
2424
}
25+
26+
override fun equals(other: Any?): Boolean {
27+
if (other is DeviceServerInfo) {
28+
if (this.deviceName == other.deviceName && other.address == this.address
29+
&& other.port == this.port) {
30+
return true
31+
}
32+
}
33+
return false
34+
}
2535
}

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/service/DetectService.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.util.Log
55
import com.alibaba.fastjson.JSON
66
import com.alibaba.fastjson.JSONObject
77
import com.tencent.iot.video.link.callback.DetectMesssageCallback
8+
import com.tencent.iot.video.link.callback.OnWlanDevicesDetectedCallback
89
import com.tencent.iot.video.link.consts.VideoConst
910
import com.tencent.iot.video.link.entity.VideoMessageType
1011
import com.tencent.iot.video.link.entity.WlanDetectBody
@@ -43,18 +44,16 @@ class DetectService private constructor(): CoroutineScope by MainScope() {
4344
var groupAddress = "255.255.255.255"
4445
private val BUFFER_SIZE = 2048
4546
private val sdkVarsion = XP2P.getVersion()
46-
var detectMesssageCallback: DetectMesssageCallback? = null
47-
var adapterCallback: DetectMesssageCallback = object: DetectMesssageCallback {
47+
@Volatile
48+
private var started = true
49+
var onWlanDevicesDetectedCallback: OnWlanDevicesDetectedCallback? = null
50+
private var adapterCallback: DetectMesssageCallback = object: DetectMesssageCallback {
4851
override fun onMessage(version: String, message: String): Boolean {
4952
Log.e(TAG, "version $version, message $message")
50-
if (detectMesssageCallback?.onMessage(version, message) == true) { // 使用集成 sdk 方的处理逻辑
51-
52-
} else { // 使用内部处理逻辑
53-
var resp = JSONObject.parseObject(message, WlanRespBody::class.java)
54-
resp.let {
55-
if (it.method == "probeMatch" && it.params != null && it.params.isReady()) {
56-
// cancel() // 查询到设备的 IP 地址信息,停止广播发送和接收内容
57-
}
53+
var resp = JSONObject.parseObject(message, WlanRespBody::class.java)
54+
resp.let {
55+
if (it.method == "probeMatch" && it.params != null && it.params.isReady()) {
56+
onWlanDevicesDetectedCallback?.onMessage(version, it)
5857
}
5958
}
6059
return true
@@ -71,6 +70,10 @@ class DetectService private constructor(): CoroutineScope by MainScope() {
7170
resetSocket()
7271
}
7372

73+
fun clearAllTask() {
74+
started = false
75+
}
76+
7477
fun resetSocket() {
7578
socket?.let { // 尝试关闭 socket
7679
it.close()
@@ -80,13 +83,17 @@ class DetectService private constructor(): CoroutineScope by MainScope() {
8083

8184
// 尝试指定次数的广播
8285
fun startSendBroadcast(body: WlanDetectBody, times: Int) {
86+
started = true
8387
Log.e(TAG, "startSendBroadcast times $times")
8488
resetSocket()
8589

8690
socket = DatagramSocket(port)
8791
openReceiver()
8892
launch(Dispatchers.IO) {
8993
for (i in 0 until times) { // 尝试在协程中发送指定次数的广播
94+
if (!started) {
95+
break
96+
}
9097
sendBroadcast(body)
9198
delay(1000) // 每秒发一次广播
9299
}

sdkdemo/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<activity android:name=".core.activity.ShowAllDeviceActivity" />
5555
<activity android:name=".video.VideoOptionsActivity" />
5656
<activity android:name=".video.VideoInputAuthorizeActivity" />
57+
<activity android:name=".video.VideoWlanDetectActivity" />
58+
<activity android:name=".video.preview.WlanVideoPreviewActivity"
59+
android:configChanges="orientation|keyboardHidden|screenSize"/>
5760
<activity android:name=".video.VideoMainActivity" />
5861
<activity android:name=".video.nvr.VideoNvrActivity" />
5962
<activity

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/video/DevInfo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class DevInfo {
1111
var Version = ""
1212
var channel = 0
1313
var online = 0
14+
var address = ""
15+
var port = 0
1416
set(value) {
1517
field = value
1618
Status = field

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/video/VideoInputAuthorizeActivity.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import android.view.View
88
import android.widget.Toast
99
import com.alibaba.fastjson.JSONArray
1010
import com.alibaba.fastjson.JSONObject
11+
import com.tencent.iot.explorer.link.core.utils.SharePreferenceUtil
1112
import com.tencent.iot.explorer.link.demo.BuildConfig
1213
import com.tencent.iot.explorer.link.demo.R
13-
import com.tencent.iot.explorer.link.demo.BaseActivity
14-
import com.tencent.iot.explorer.link.core.utils.SharePreferenceUtil
1514
import com.tencent.iot.explorer.link.demo.VideoBaseActivity
1615
import com.tencent.iot.video.link.consts.VideoConst
17-
import com.tencent.iot.video.link.entity.WlanDetectBody
18-
import com.tencent.iot.video.link.service.DetectService
1916
import kotlinx.android.synthetic.main.activity_video_input_authorize.*
2017
import kotlinx.android.synthetic.main.blue_title_layout.*
2118
import kotlinx.android.synthetic.main.input_item_layout.view.*

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/video/VideoOptionsActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class VideoOptionsActivity : VideoBaseActivity() {
1414

1515
override fun setListener() {
1616
btn_video.setOnClickListener { jumpActivity(VideoInputAuthorizeActivity::class.java) }
17+
btn_video_wlan.setOnClickListener { jumpActivity(VideoWlanDetectActivity::class.java) }
1718
}
1819

1920
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.tencent.iot.explorer.link.demo.video
2+
3+
import android.text.InputType
4+
import android.text.TextUtils
5+
import android.util.Log
6+
import android.view.View
7+
import android.widget.Toast
8+
import androidx.recyclerview.widget.LinearLayoutManager
9+
import com.alibaba.fastjson.JSONArray
10+
import com.tencent.iot.explorer.link.core.utils.SharePreferenceUtil
11+
import com.tencent.iot.explorer.link.demo.App
12+
import com.tencent.iot.explorer.link.demo.R
13+
import com.tencent.iot.explorer.link.demo.VideoBaseActivity
14+
import com.tencent.iot.explorer.link.demo.video.preview.WlanVideoPreviewActivity
15+
import com.tencent.iot.video.link.callback.OnWlanDevicesDetectedCallback
16+
import com.tencent.iot.video.link.consts.VideoConst
17+
import com.tencent.iot.video.link.entity.DeviceServerInfo
18+
import com.tencent.iot.video.link.entity.WlanDetectBody
19+
import com.tencent.iot.video.link.entity.WlanRespBody
20+
import com.tencent.iot.video.link.service.DetectService
21+
import kotlinx.android.synthetic.main.activity_video_detect_devs.*
22+
import kotlinx.android.synthetic.main.activity_video_detect_devs.product_id_layout
23+
import kotlinx.android.synthetic.main.activity_video_input_authorize.*
24+
import kotlinx.android.synthetic.main.blue_title_layout.*
25+
import kotlinx.android.synthetic.main.fragment_video_device.*
26+
import kotlinx.android.synthetic.main.input_item_layout.view.*
27+
import kotlinx.coroutines.*
28+
import java.util.*
29+
import kotlin.collections.ArrayList
30+
31+
class VideoWlanDetectActivity : VideoBaseActivity() , CoroutineScope by MainScope() {
32+
33+
var datas: MutableList<DeviceServerInfo> = ArrayList()
34+
var adapter: WlanDevsAdapter? = null
35+
36+
override fun getContentView(): Int {
37+
return R.layout.activity_video_detect_devs
38+
}
39+
40+
override fun initView() {
41+
42+
tv_title.setText(R.string.video_wlan)
43+
product_id_layout.tv_tip.setText(R.string.product_id)
44+
product_id_layout.ev_content.setHint(R.string.hint_product_id)
45+
product_id_layout.ev_content.inputType = InputType.TYPE_CLASS_TEXT
46+
product_id_layout.iv_more.visibility = View.GONE
47+
48+
client_token_layout.tv_tip.setText(R.string.video_client_token)
49+
client_token_layout.ev_content.setHint(R.string.hint_client_token)
50+
client_token_layout.ev_content.inputType = InputType.TYPE_CLASS_TEXT
51+
client_token_layout.iv_more.visibility = View.GONE
52+
53+
launch (Dispatchers.Main) {
54+
var jsonArrStr = SharePreferenceUtil.getString(this@VideoWlanDetectActivity, VideoConst.VIDEO_WLAN_CONFIG, VideoConst.VIDEO_ACCESS_INFOS)
55+
jsonArrStr?.let {
56+
var accessInfos = JSONArray.parseArray(jsonArrStr, AccessInfo::class.java)
57+
accessInfos?.let {
58+
if (accessInfos.size > 0) {
59+
var accessInfo = accessInfos.get(accessInfos.size - 1)
60+
client_token_layout.ev_content.setText(accessInfo.accessToken)
61+
product_id_layout.ev_content.setText(accessInfo.productId)
62+
}
63+
}
64+
}
65+
}
66+
67+
var layoutManager = LinearLayoutManager(this@VideoWlanDetectActivity)
68+
adapter = WlanDevsAdapter(this@VideoWlanDetectActivity, datas)
69+
devs_lv.setLayoutManager(layoutManager)
70+
devs_lv.setAdapter(adapter)
71+
adapter?.setOnItemClicked(onItemClicked)
72+
}
73+
74+
private var onItemClicked = object : WlanDevsAdapter.OnItemClicked {
75+
override fun onItemClicked(pos: Int) {
76+
App.data.accessInfo = AccessInfo()
77+
App.data.accessInfo?.productId = product_id_layout.ev_content.text.toString()
78+
79+
var dev = DevInfo()
80+
dev.deviceName = datas.get(pos).deviceName
81+
dev.channel = 0
82+
dev.Status = 1
83+
dev.address = datas.get(pos).address
84+
dev.port = datas.get(pos).port
85+
WlanVideoPreviewActivity.startPreviewActivity(this@VideoWlanDetectActivity, dev)
86+
}
87+
}
88+
89+
override fun setListener() {
90+
iv_back.setOnClickListener { finish() }
91+
btn_detect.setOnClickListener(searchClickedListener)
92+
}
93+
94+
override fun onDestroy() {
95+
super.onDestroy()
96+
cancel()
97+
}
98+
99+
override fun onPause() {
100+
super.onPause()
101+
DetectService.getInstance().clearAllTask()
102+
}
103+
104+
var searchClickedListener = object : View.OnClickListener {
105+
override fun onClick(v: View?) {
106+
if (TextUtils.isEmpty(product_id_layout.ev_content.text)) {
107+
Toast.makeText(this@VideoWlanDetectActivity, R.string.hint_product_id, Toast.LENGTH_SHORT).show()
108+
return
109+
}
110+
111+
if (TextUtils.isEmpty(client_token_layout.ev_content.text)) {
112+
Toast.makeText(this@VideoWlanDetectActivity, R.string.hint_client_token, Toast.LENGTH_SHORT).show()
113+
return
114+
}
115+
116+
var accessInfo = AccessInfo()
117+
accessInfo.accessToken = client_token_layout.ev_content.text.toString()
118+
accessInfo.productId = product_id_layout.ev_content.text.toString()
119+
120+
launch (Dispatchers.Main) {
121+
checkAccessInfo(accessInfo)
122+
}
123+
124+
datas.clear()
125+
var detectBody = WlanDetectBody()
126+
detectBody.productId = accessInfo.productId
127+
detectBody.clientToken = accessInfo.accessToken
128+
DetectService.getInstance().onWlanDevicesDetectedCallback = detectMesssageCallback
129+
DetectService.getInstance().startSendBroadcast(detectBody, 30)
130+
}
131+
}
132+
133+
private var detectMesssageCallback = object: OnWlanDevicesDetectedCallback {
134+
override fun onMessage(version: String, resp: WlanRespBody): Boolean {
135+
if (!datas.contains(resp.params)) {
136+
datas.add(resp.params)
137+
runOnUiThread(Runnable {
138+
adapter?.notifyDataSetChanged()
139+
})
140+
}
141+
return true
142+
}
143+
}
144+
145+
private fun checkAccessInfo(accessInfo: AccessInfo) {
146+
var accessInfos: MutableList<AccessInfo> = ArrayList()
147+
accessInfos.add(accessInfo)
148+
SharePreferenceUtil.saveString(this@VideoWlanDetectActivity, VideoConst.VIDEO_WLAN_CONFIG, VideoConst.VIDEO_ACCESS_INFOS, JSONArray.toJSONString(accessInfos))
149+
}
150+
151+
}

0 commit comments

Comments
 (0)