修复读取 CCCD 报错的问题,写入时更新特征值 (#50)

* 修复 CCCD 无法读取的问题

* 优化代码

* 调整部分必需参数为可空参数

* 调整接口

* 写入时更新特征值

* 调整接口

* 适配新接口

* 调整依赖项
This commit is contained in:
iAMD
2024-02-01 19:16:42 +08:00
committed by GitHub
parent 63bbc1a732
commit 44efce78df
32 changed files with 521 additions and 231 deletions

View File

@ -203,17 +203,13 @@ extension MyGattDescriptorX on MyGattDescriptor {
}
extension MyGattCharacteristicX on MyGattCharacteristic {
MyGattCharacteristicArgs toArgs() {
MyGattCharacteristicArgs toArgs(List<MyGattDescriptorArgs> descriptorsArgs) {
final hashCodeArgs = hashCode;
final uuidArgs = uuid.toArgs();
final propertyNumbersArgs = properties.map((property) {
final propertyArgs = property.toArgs();
return propertyArgs.index;
}).toList();
final descriptorsArgs = descriptors
.cast<MyGattDescriptor>()
.map((descriptor) => descriptor.toArgs())
.toList();
return MyGattCharacteristicArgs(
hashCodeArgs: hashCodeArgs,
uuidArgs: uuidArgs,
@ -224,13 +220,9 @@ extension MyGattCharacteristicX on MyGattCharacteristic {
}
extension MyGattServiceX on MyGattService {
MyGattServiceArgs toArgs() {
MyGattServiceArgs toArgs(List<MyGattCharacteristicArgs> characteristicsArgs) {
final hashCodeArgs = hashCode;
final uuidArgs = uuid.toArgs();
final characteristicsArgs = characteristics
.cast<MyGattCharacteristic>()
.map((characteristic) => characteristic.toArgs())
.toList();
return MyGattServiceArgs(
hashCodeArgs: hashCodeArgs,
uuidArgs: uuidArgs,

View File

@ -65,14 +65,22 @@ class MyPeripheralManager extends PeripheralManager
if (service is! MyGattService) {
throw TypeError();
}
final serviceArgs = service.toArgs();
final hashCodeArgs = serviceArgs.hashCodeArgs;
logger.info('addService: $hashCodeArgs');
final characteristics = <int, MyGattCharacteristic>{};
final characteristicsArgs = <MyGattCharacteristicArgs>[];
for (var characteristic in service.characteristics) {
final descriptorsArgs = <MyGattDescriptorArgs>[];
for (var descriptor in characteristic.descriptors) {
final descriptorArgs = descriptor.toArgs();
descriptorsArgs.add(descriptorArgs);
}
final characteristicArgs = characteristic.toArgs(descriptorsArgs);
characteristicsArgs.add(characteristicArgs);
characteristics[characteristicArgs.hashCodeArgs] = characteristic;
}
final serviceArgs = service.toArgs(characteristicsArgs);
logger.info('addService: $serviceArgs');
await _api.addService(serviceArgs);
_characteristics[hashCodeArgs] = {
for (var characteristics in service.characteristics)
characteristics.hashCode: characteristics
};
_characteristics[serviceArgs.hashCodeArgs] = characteristics;
}
@override
@ -304,7 +312,8 @@ class MyPeripheralManager extends PeripheralManager
MyGattCharacteristic characteristic,
Uint8List value,
) async {
final trimmedValue = value.trimGATT();
characteristic.value = value;
final trimmedValue = characteristic.value;
final eventArgs = GattCharacteristicWrittenEventArgs(
central,
characteristic,