fix: 修复写入特征值无法完成和重启蓝牙后调用外围设备方法报错的问题 (#20)

* fix: 修复示例代码问题

* fix: 断开连接时清理缓存

* fix: 修复蓝牙重新打开后 GATT server 失效的问题

* fix: 修复问题

* fix: 修复写入特征值无法完成的问题

* fix: 修改版本号
This commit is contained in:
Mr剑侠客
2023-10-12 18:29:03 +08:00
committed by GitHub
parent 5d87dbc129
commit 4bec1c9f3a
161 changed files with 11133 additions and 23 deletions

View File

@ -431,11 +431,19 @@ class MyCentralManager(private val context: Context, binaryMessenger: BinaryMess
if (discoverGattCallback != null) {
discoverGattCallback(Result.failure(error))
}
val servicesArgs = servicesArgsOfPeripherals[peripheralHashCodeArgs] ?: emptyList()
val servicesArgs = servicesArgsOfPeripherals.remove(peripheralHashCodeArgs)
?: emptyList()
for (serviceArgs in servicesArgs) {
val serviceHashCodeArgs = serviceArgs.hashCodeArgs
val service = services.remove(serviceHashCodeArgs) as BluetoothGattService
val serviceHashCode = service.hashCode()
this.servicesArgs.remove(serviceHashCode)
val characteristicsArgs = serviceArgs.characteristicsArgs.filterNotNull()
for (characteristicArgs in characteristicsArgs) {
val characteristicHashCodeArgs = characteristicArgs.hashCodeArgs
val characteristic = characteristics.remove(characteristicHashCodeArgs) as BluetoothGattCharacteristic
val characteristicHashCode = characteristic.hashCode()
this.characteristicsArgs.remove(characteristicHashCode)
val readCharacteristicCallback = readCharacteristicCallbacks.remove(characteristicHashCodeArgs)
val writeCharacteristicCallback = writeCharacteristicCallbacks.remove(characteristicHashCodeArgs)
if (readCharacteristicCallback != null) {
@ -447,6 +455,9 @@ class MyCentralManager(private val context: Context, binaryMessenger: BinaryMess
val descriptorsArgs = characteristicArgs.descriptorsArgs.filterNotNull()
for (descriptorArgs in descriptorsArgs) {
val descriptorHashCodeArgs = descriptorArgs.hashCodeArgs
val descriptor = descriptors.remove(descriptorHashCodeArgs) as BluetoothGattDescriptor
val descriptorHashCode = descriptor.hashCode()
this.descriptorsArgs.remove(descriptorHashCode)
val readDescriptorCallback = readDescriptorCallbacks.remove(descriptorHashCodeArgs)
val writeDescriptorCallback = writeDescriptorCallbacks.remove(descriptorHashCodeArgs)
if (readDescriptorCallback != null) {

View File

@ -5,6 +5,7 @@ import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattServer
import android.bluetooth.BluetoothGattService
import android.bluetooth.BluetoothStatusCodes
import android.bluetooth.le.AdvertiseSettings
@ -16,7 +17,6 @@ import android.util.Log
import io.flutter.plugin.common.BinaryMessenger
class MyPeripheralManager(private val context: Context, binaryMessenger: BinaryMessenger) : MyBluetoothLowEnergyManager(context), MyPeripheralManagerHostApi {
private val server by lazy { manager.openGattServer(context, bluetoothGattServerCallback) }
private val advertiser get() = adapter.bluetoothLeAdvertiser
private val api = MyPeripheralManagerFlutterApi(binaryMessenger)
@ -35,6 +35,7 @@ class MyPeripheralManager(private val context: Context, binaryMessenger: BinaryM
private val characteristicsArgs = mutableMapOf<Int, MyGattCharacteristicArgs>()
private val descriptorsArgs = mutableMapOf<Int, MyGattDescriptorArgs>()
private lateinit var server: BluetoothGattServer
private var registered = false
private var advertising = false
@ -282,6 +283,9 @@ class MyPeripheralManager(private val context: Context, binaryMessenger: BinaryM
else MyBluetoothLowEnergyStateArgs.UNAUTHORIZED
val stateNumberArgs = stateArgs.raw.toLong()
val args = MyPeripheralManagerArgs(stateNumberArgs)
if (stateArgs == MyBluetoothLowEnergyStateArgs.POWEREDON) {
server = manager.openGattServer(context, bluetoothGattServerCallback)
}
callback(Result.success(args))
if (authorized) {
register()
@ -296,6 +300,9 @@ class MyPeripheralManager(private val context: Context, binaryMessenger: BinaryM
}
val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)
val stateArgs = state.toBluetoothLowEnergyStateArgs()
if (stateArgs == MyBluetoothLowEnergyStateArgs.POWEREDON) {
server = manager.openGattServer(context, bluetoothGattServerCallback)
}
val stateNumberArgs = stateArgs.raw.toLong()
api.onStateChanged(stateNumberArgs) {}
}