Skip to content

Commit 4adbd99

Browse files
author
archurtan
committed
修复'蓝牙辅助配网成功率低'的问题
对同一个设备发起了两次蓝牙连接导致的 Change-Id: I110d593d757fb7dc7169a35c3e0f408d8ce2a7ba
1 parent 7249646 commit 4adbd99

File tree

2 files changed

+18
-15
lines changed
  • app/src/main/java/com/tencent/iot/explorer/link/mvp/model

2 files changed

+18
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
158158
return@launch // 设置成功则直接退出
159159
}
160160
}
161-
bleFailed("set wifi mode failed")
161+
bleFailed("onBleDeviceInfo set wifi mode failed")
162162
}
163163
}
164164

@@ -176,7 +176,7 @@ class ConnectModel(view: ConnectView) : ParentModel<ConnectView>(view), MyCallba
176176
override fun onBleSetWifiModeResult(success: Boolean) {
177177
L.d(TAG, "onBleSetWifiModeResult ${success}")
178178
if (!success) {
179-
bleFailed("set wifi mode failed")
179+
bleFailed("onBleSetWifiModeResult set wifi mode failed")
180180
return
181181
}
182182

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.tencent.iot.explorer.link.core.log.L
1717
import java.security.InvalidKeyException
1818
import java.security.MessageDigest
1919
import java.util.*
20+
import java.util.concurrent.ConcurrentHashMap
2021
import javax.crypto.Mac
2122
import javax.crypto.spec.SecretKeySpec
2223
import kotlin.collections.ArrayList
@@ -121,7 +122,7 @@ class BleConfigService private constructor() {
121122

122123
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
123124
private fun getScanCallback(): ScanCallback {
124-
var foundedSet: HashMap<String, Boolean> = HashMap() // 用于去重
125+
var foundedSet: ConcurrentHashMap<String, Boolean> = ConcurrentHashMap() // 用于去重
125126
scanCallback = object : ScanCallback() {
126127
override fun onBatchScanResults(results: List<ScanResult?>?) {
127128
L.d("onBatchScanResults: " + results.toString())
@@ -154,13 +155,14 @@ class BleConfigService private constructor() {
154155

155156
dev?.let { bleDev ->
156157
if (TextUtils.isEmpty(bleDev.productId) && TextUtils.isEmpty(bleDev.bindTag)) return@let
157-
158158
L.d(TAG, "productID ${bleDev?.productId} devName ${bleDev.devName} bindTag ${bleDev.bindTag}")
159-
var founded = foundedSet.get(bleDev.productId + bleDev.devName + bleDev.bindTag)
160-
founded?:let { // 不存在对应的元素,第一次发现该设备
161-
connetionListener?.onBleDeviceFounded(bleDev)
162-
L.d(TAG, "onScanResults productId ${bleDev.productId} devName ${bleDev.devName}")
163-
foundedSet.set(bleDev.productId + bleDev.devName, true)
159+
if (!TextUtils.isEmpty(bleDev.devName) && bleDev.devName != "null") {
160+
val founded = foundedSet[bleDev.productId + bleDev.devName + bleDev.bindTag]
161+
founded?:let { // 不存在对应的元素,第一次发现该设备
162+
connetionListener?.onBleDeviceFounded(bleDev)
163+
L.d(TAG, "onScanResults productId ${bleDev.productId} devName ${bleDev.devName}")
164+
foundedSet[bleDev.productId + bleDev.devName] = true
165+
}
164166
}
165167
}
166168
}
@@ -250,11 +252,10 @@ class BleConfigService private constructor() {
250252
gatt?.discoverServices()
251253
gatt?.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)
252254
connetionListener?.onBleDeviceConnected()
253-
return
255+
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
256+
L.d(TAG, "onBleDeviceDisconnected")
257+
connetionListener?.onBleDeviceDisconnected(TCLinkException("diconnected"))
254258
}
255-
256-
L.d(TAG, "onBleDeviceDisconnected")
257-
connetionListener?.onBleDeviceDisconnected(TCLinkException("diconnected"))
258259
}
259260

260261
override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) {
@@ -437,17 +438,19 @@ class BleConfigService private constructor() {
437438
connection?.let {
438439
for (service in it.services) {
439440
if (service.uuid.toString().substring(4, 8).toUpperCase().equals(serviceUuid)) {
440-
441441
for (characteristic in service.characteristics) {
442442
if (characteristic.uuid.toString().substring(4, 8).toUpperCase().equals(uuid)) {
443443
characteristic.value = byteArr
444-
return connection.writeCharacteristic(characteristic)
444+
val success = connection.writeCharacteristic(characteristic)
445+
L.d("setCharacteristicValue success = ${success}")
446+
return success
445447
}
446448
}
447449
break
448450
}
449451
}
450452
}
453+
L.e("setCharacteristicValue error")
451454
return false
452455
}
453456

0 commit comments

Comments
 (0)