@@ -8,7 +8,6 @@ import android.bluetooth.le.ScanSettings
88import android.content.Context
99import android.os.Build
1010import android.text.TextUtils
11- import android.util.Log
1211import androidx.annotation.RequiresApi
1312import com.alibaba.fastjson.JSON
1413import com.tencent.iot.explorer.link.core.link.entity.*
@@ -24,6 +23,7 @@ import kotlin.collections.ArrayList
2423import kotlin.collections.HashMap
2524import kotlin.experimental.xor
2625
26+
2727class BleConfigService private constructor() {
2828
2929 private val TAG = this .javaClass.simpleName
@@ -370,7 +370,9 @@ class BleConfigService private constructor() {
370370 }
371371 override fun onCharacteristicWrite (gatt : BluetoothGatt ? , characteristic : BluetoothGattCharacteristic ? , status : Int ) {}
372372 override fun onCharacteristicRead (gatt : BluetoothGatt ? , characteristic : BluetoothGattCharacteristic ? , status : Int ) {}
373- override fun onServicesDiscovered (gatt : BluetoothGatt ? , status : Int ) {}
373+ override fun onServicesDiscovered (gatt : BluetoothGatt ? , status : Int ) {
374+ enableCharacteristicNotificationWithUUid(gatt, " FFF0" )
375+ }
374376 override fun onMtuChanged (gatt : BluetoothGatt ? , mtu : Int , status : Int ) {
375377 connetionListener?.onMtuChanged(mtu, status)
376378 }
@@ -408,11 +410,21 @@ class BleConfigService private constructor() {
408410 if (service.uuid.toString().substring(4 , 8 ).toUpperCase().equals(serviceUuid)) {
409411 for (characteristic in service.characteristics) {
410412 if (characteristic.uuid.toString().substring(4 , 8 ).toUpperCase().equals(" FFE3" )) {
411- return connection.setCharacteristicNotification(characteristic, true )
413+ val success = connection.setCharacteristicNotification(characteristic, true )
414+ if (success) {
415+ val descriptorList = characteristic.descriptors
416+ if (descriptorList != null && descriptorList.size > 0 ) {
417+ for (descriptor in descriptorList) {
418+ descriptor.value = BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE
419+ connection.writeDescriptor(descriptor)
420+ }
421+ }
422+ }
412423 }
413424 }
414425 }
415426 }
427+ return true
416428 }
417429 return false
418430 }
@@ -460,12 +472,10 @@ class BleConfigService private constructor() {
460472 }
461473
462474 fun requestConnectWifi (connection : BluetoothGatt ? ): Boolean {
463- if (! enableCharacteristicNotification(connection)) return false
464475 return setCharacteristicValue(connection, ByteArray (1 ){0xE3 .toByte()})
465476 }
466477
467478 fun requestDevInfo (connection : BluetoothGatt ? ): Boolean {
468- if (! enableCharacteristicNotification(connection)) return false
469479 currentConnectBleDevice?.let {
470480 if (it.type == 1 ) {
471481 L .e(" 纯蓝牙协议流程中,不支持调用该接口" )
@@ -476,22 +486,18 @@ class BleConfigService private constructor() {
476486 }
477487
478488 fun setWifiMode (connection : BluetoothGatt ? , mode : BleDeviceWifiMode ): Boolean {
479- if (! enableCharacteristicNotification(connection)) return false
480489 var byteArr = ByteArray (2 )
481490 byteArr[0 ] = 0xE1 .toByte()
482491 byteArr[1 ] = mode.getValue()
483492 return setCharacteristicValue(connection, byteArr)
484493 }
485494
486495 fun sendWifiInfo (connection : BluetoothGatt ? , bleDeviceWifiInfo : BleDeviceWifiInfo ): Boolean {
487- if (! enableCharacteristicNotification(connection)) return false
488496 return setCharacteristicValue(connection, bleDeviceWifiInfo.formatByteArr())
489497 }
490498
491499 fun configToken (connection : BluetoothGatt ? , token : String ): Boolean {
492500 if (TextUtils .isEmpty(token)) return false
493- if (! enableCharacteristicNotification(connection)) return false
494-
495501 var byteArr = ByteArray (3 + token.toByteArray().size)
496502 byteArr[0 ] = 0xE4 .toByte()
497503 byteArr[1 ] = (token.toByteArray().size / Math .pow(2.0 , 8.0 ).toInt()).toByte()
0 commit comments