11package com.tencent.iot.explorer.link.mvp.model
22
3- import android.bluetooth.BluetoothDevice
43import android.bluetooth.BluetoothGatt
54import android.content.Context
65import android.location.Location
7- import android.text.TextUtils
8- import android.util.Log
96import com.alibaba.fastjson.JSON
107import com.espressif.iot.esptouch.IEsptouchResult
118import com.tencent.iot.explorer.link.App
129import 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
1312import com.tencent.iot.explorer.link.core.auth.http.ConnectionListener
1413import com.tencent.iot.explorer.link.core.auth.http.Reconnect
14+ import com.tencent.iot.explorer.link.core.auth.response.BaseResponse
1515import 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
1816import com.tencent.iot.explorer.link.core.link.entity.*
1917import com.tencent.iot.explorer.link.core.link.exception.TCLinkException
18+ import com.tencent.iot.explorer.link.core.link.listener.BleDeviceConnectionListener
2019import com.tencent.iot.explorer.link.core.link.listener.SmartConfigListener
2120import 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
2224import com.tencent.iot.explorer.link.core.log.L
25+ import com.tencent.iot.explorer.link.kitlink.entity.ConfigType
2326import com.tencent.iot.explorer.link.kitlink.util.*
2427import com.tencent.iot.explorer.link.mvp.ParentModel
2528import 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
3229import 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 }
0 commit comments