Skip to content

Commit c436a62

Browse files
sevenhheSundoggyNew
authored andcommitted
调试与 c 端设备的蓝牙配网流程
http://tapd.oa.com/NEW_IOT/prong/stories/view/1020393192863935819# Change-Id: I1312776ceafd87702682d08fa73d97658b8da7d1
1 parent fdf901b commit c436a62

File tree

5 files changed

+59
-42
lines changed

5 files changed

+59
-42
lines changed

app/src/main/java/com/tencent/iot/explorer/link/kitlink/activity/ConnectProgressActivity.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ class ConnectProgressActivity : PActivity(), ConnectView {
9090
closePopup?.onKeyListener = object : CommonPopupWindow.OnKeyListener {
9191
override fun confirm(popupWindow: CommonPopupWindow) {
9292
quit = true
93-
if (type == ConfigType.SmartConfig.id) {
94-
backTo(3)
95-
} else {
96-
backTo(4)
97-
}
93+
backToDeviceCategoryActivity()
9894
}
9995

10096
override fun cancel(popupWindow: CommonPopupWindow) {

app/src/main/java/com/tencent/iot/explorer/link/kitlink/activity/DeviceCategoryActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class DeviceCategoryActivity : PActivity(), MyCallback, CRecyclerView.RecyclerI
140140
override fun onBleSendWifiInfoResult(success: Boolean) {}
141141
override fun onBleWifiConnectedInfo(wifiConnectInfo: BleWifiConnectInfo) {}
142142
override fun onBlePushTokenResult(success: Boolean) {}
143+
override fun onMtuChanged(mtu: Int, status: Int) {}
143144
}
144145

145146
private val runnable = Runnable {

app/src/main/java/com/tencent/iot/explorer/link/mvp/model/ConnectModel.kt

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.tencent.iot.explorer.link.mvp.model
22

3-
import android.bluetooth.BluetoothDevice
43
import android.bluetooth.BluetoothGatt
54
import android.content.Context
65
import android.location.Location
7-
import android.text.TextUtils
8-
import android.util.Log
96
import com.alibaba.fastjson.JSON
107
import com.espressif.iot.esptouch.IEsptouchResult
118
import com.tencent.iot.explorer.link.App
129
import com.tencent.iot.explorer.link.R
10+
import com.tencent.iot.explorer.link.T
11+
import com.tencent.iot.explorer.link.core.auth.callback.MyCallback
1312
import com.tencent.iot.explorer.link.core.auth.http.ConnectionListener
1413
import com.tencent.iot.explorer.link.core.auth.http.Reconnect
14+
import com.tencent.iot.explorer.link.core.auth.response.BaseResponse
1515
import com.tencent.iot.explorer.link.core.auth.response.DeviceBindTokenStateResponse
16-
import com.tencent.iot.explorer.link.core.link.service.SmartConfigService
17-
import com.tencent.iot.explorer.link.core.link.service.SoftAPService
1816
import com.tencent.iot.explorer.link.core.link.entity.*
1917
import com.tencent.iot.explorer.link.core.link.exception.TCLinkException
18+
import com.tencent.iot.explorer.link.core.link.listener.BleDeviceConnectionListener
2019
import com.tencent.iot.explorer.link.core.link.listener.SmartConfigListener
2120
import com.tencent.iot.explorer.link.core.link.listener.SoftAPListener
21+
import com.tencent.iot.explorer.link.core.link.service.BleConfigService
22+
import com.tencent.iot.explorer.link.core.link.service.SmartConfigService
23+
import com.tencent.iot.explorer.link.core.link.service.SoftAPService
2224
import com.tencent.iot.explorer.link.core.log.L
25+
import com.tencent.iot.explorer.link.kitlink.entity.ConfigType
2326
import com.tencent.iot.explorer.link.kitlink.util.*
2427
import com.tencent.iot.explorer.link.mvp.ParentModel
2528
import com.tencent.iot.explorer.link.mvp.view.ConnectView
26-
import com.tencent.iot.explorer.link.T
27-
import com.tencent.iot.explorer.link.core.auth.callback.MyCallback
28-
import com.tencent.iot.explorer.link.core.auth.response.BaseResponse
29-
import com.tencent.iot.explorer.link.core.link.listener.BleDeviceConnectionListener
30-
import com.tencent.iot.explorer.link.core.link.service.BleConfigService
31-
import com.tencent.iot.explorer.link.kitlink.entity.ConfigType
3229
import kotlinx.coroutines.*
33-
import java.lang.Runnable
3430

3531
/**
3632
* 配网进度、绑定设备
@@ -63,7 +59,7 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
6359
smartConfig = SmartConfigService(context.applicationContext)
6460
} else if (type == ConfigType.SoftAp.id) {
6561
softAP = SoftAPService(context.applicationContext)
66-
} else {} // 蓝牙服务时单例,在 app 中已经初始化过,无需再处理
62+
} else {} // 蓝牙服务是单例,在 app 中已经初始化过,无需再处理
6763
}
6864

6965
/**
@@ -99,7 +95,6 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
9995
return
10096
}
10197

102-
deviceInfo = DeviceInfo(bleDevice?.productId, bleDevice?.devName)
10398
BleConfigService.get().connetionListener = getBleListener(bleDevice!!)
10499

105100
// 快速连接,避免多次的扫描设备带来的耗时
@@ -152,23 +147,28 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
152147
override fun onBleDeviceDisconnected(exception: TCLinkException) {
153148
bleFailed(exception.errorMessage)
154149
}
155-
override fun onBleDeviceInfo(bleDeviceInfo: BleDeviceInfo) {}
156-
157-
override fun onBleDeviceConnected() {
158-
L.d(TAG, "onBleDeviceConnected")
150+
override fun onBleDeviceInfo(bleDeviceInfo: BleDeviceInfo) {
151+
L.d(TAG, "onBleDeviceInfo ${JSON.toJSONString(bleDeviceInfo)}")
152+
deviceInfo = DeviceInfo(bleDevice?.productId, bleDeviceInfo.devName)
159153
launch {
160-
delay(2000)
161-
bluetoothGatt?.let {
162-
var mtuRet = BleConfigService.get().setMtuSize(it, App.data.bindDeviceToken.length * 2 + 20)
163-
L.d(TAG, "mtuRet ${mtuRet}")
164-
}
165-
delay(1000)
166154
bluetoothGatt?.let {
155+
delay(500)
167156
if (BleConfigService.get().setWifiMode(it, BleDeviceWifiMode.STA)) {
168157
return@launch // 设置成功则直接退出
169158
}
170159
}
171-
bleFailed("connect ble dev failed")
160+
bleFailed("request device info failed")
161+
}
162+
}
163+
164+
override fun onBleDeviceConnected() {
165+
L.d(TAG, "onBleDeviceConnected")
166+
launch {
167+
bluetoothGatt?.let {
168+
delay(3000)
169+
if (BleConfigService.get().setMtuSize(it, 512)) return@launch
170+
}
171+
bleFailed("config mtu failed")
172172
}
173173
}
174174

@@ -180,7 +180,7 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
180180
}
181181

182182
launch {
183-
delay(1000)
183+
delay(500)
184184
bluetoothGatt?.let {
185185
var bleDeviceWifiInfo = BleDeviceWifiInfo(ssid, password)
186186
L.d(TAG, "bleDeviceWifiInfo ${JSON.toJSONString(bleDeviceWifiInfo)}")
@@ -199,7 +199,7 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
199199
}
200200

201201
launch {
202-
delay(1000)
202+
delay(500)
203203
bluetoothGatt?.let {
204204
if (BleConfigService.get().requestConnectWifi(it)) return@launch
205205
bleFailed("connect wifi failed")
@@ -215,10 +215,9 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
215215
}
216216

217217
launch {
218-
delay(1000)
218+
delay(500)
219219
bluetoothGatt?.let {
220220
this@ConnectModel.view?.connectStep(BleConfigStep.STEP_SEND_TOKEN.ordinal)
221-
//L.d(TAG, "send token ${App.data.bindDeviceToken}")
222221
if (BleConfigService.get().configToken(it, App.data.bindDeviceToken)) return@launch
223222
bleFailed("send token failed")
224223
}
@@ -234,6 +233,24 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
234233
}
235234
}
236235

236+
override fun onMtuChanged(mtu: Int, status: Int) {
237+
L.d(TAG, "onMtuChanged mtu $mtu status $status")
238+
if (BluetoothGatt.GATT_SUCCESS != status) {
239+
bleFailed("config mtu $mtu failed")
240+
return
241+
}
242+
243+
launch {
244+
bluetoothGatt?.let {
245+
delay(500)
246+
if (BleConfigService.get().requestDevInfo(it)) {
247+
return@launch
248+
}
249+
}
250+
bleFailed("request device info failed")
251+
}
252+
}
253+
237254
}
238255
}
239256

@@ -317,9 +334,9 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
317334
var currentNo = 0
318335

319336
// 开启线程做网络请求
320-
Thread(Runnable {
337+
Thread{
321338
checkDeviceBindTokenState(currentNo, maxTimes2Try, interval)
322-
}).start()
339+
}.start()
323340
checkDeviceBindTokenStateStarted = true
324341
}
325342

@@ -347,15 +364,15 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
347364
2 -> {
348365
Reconnect.instance.stop(connectionListener)
349366
checkDeviceBindTokenStateStarted = false
350-
Thread(Runnable {
367+
Thread{
351368
wifiBindDevice(deviceInfo!!)
352-
}).start()
369+
}.start()
353370
} else -> {
354371
// 主线程回调,子线程开启新的网络请求,避免阻塞主线程
355-
Thread(Runnable{
372+
Thread{
356373
Thread.sleep(interval.toLong() * 1000)
357374
checkDeviceBindTokenState(nextNo, maxTimes, interval)
358-
}).start()
375+
}.start()
359376
}
360377
}
361378
}

sdk/explorer-link-android/src/main/java/com/tencent/iot/explorer/link/core/link/listener/BleDeviceConnectionListener.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ interface BleDeviceConnectionListener {
1414
fun onBleSendWifiInfoResult(success: Boolean)
1515
fun onBleWifiConnectedInfo(wifiConnectInfo: BleWifiConnectInfo)
1616
fun onBlePushTokenResult(success: Boolean)
17+
fun onMtuChanged(mtu: Int, status: Int)
1718
}

sdk/explorer-link-android/src/main/java/com/tencent/iot/explorer/link/core/link/service/BleConfigService.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class BleConfigService private constructor() {
101101
return@let
102102
}
103103

104-
L.d(TAG, "productID ${dev?.productId}")
104+
L.d(TAG, "productID ${bleDev?.productId} devName ${bleDev.devName}")
105105
var founded = foundedSet.get(bleDev.productId + bleDev.devName)
106106
founded?:let { // 不存在对应的元素,第一次发现该设备
107107
connetionListener?.onBleDeviceFounded(bleDev)
@@ -201,7 +201,9 @@ class BleConfigService private constructor() {
201201
override fun onCharacteristicWrite(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) {}
202202
override fun onCharacteristicRead(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) {}
203203
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {}
204-
override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) {}
204+
override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) {
205+
connetionListener?.onMtuChanged(mtu, status)
206+
}
205207
override fun onDescriptorWrite(gatt: BluetoothGatt?, descriptor: BluetoothGattDescriptor?, status: Int) {}
206208
})
207209
}

0 commit comments

Comments
 (0)