@@ -17,6 +17,7 @@ import com.tencent.iot.explorer.link.core.log.L
1717import java.security.InvalidKeyException
1818import java.security.MessageDigest
1919import java.util.*
20+ import java.util.concurrent.ConcurrentHashMap
2021import javax.crypto.Mac
2122import javax.crypto.spec.SecretKeySpec
2223import 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