fix: 修复安卓平台由于协商 MTU 失败导致发现服务抛出异常的问题 #22 (#23)

This commit is contained in:
iAMD
2023-10-21 14:04:22 +08:00
committed by GitHub
parent 728402ac16
commit 3c10caa5dd
7 changed files with 47 additions and 14 deletions

View File

@ -1,3 +1,7 @@
## 3.0.3
* `Android` Fix the issue [android device: requestMtu issue #22](https://github.com/yanshouwang/bluetooth_low_energy/issues/22)
## 3.0.2
* `Android` `iOS` Fix the issue that `getMaximumWriteLength` is wrong and coerce the value from 20 to 512.

View File

@ -23,15 +23,15 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.2"
version: "3.0.3"
bluetooth_low_energy_android:
dependency: transitive
description:
name: bluetooth_low_energy_android
sha256: "0fa5c4625ac01b6d4bbf78b10d0389d8e9907ae7c3fbc9601b481ddd4eaa86b6"
sha256: "86e10541532c96607a0525edba1fd75516543e9beac2e391ac8a50c467aebf88"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.4"
bluetooth_low_energy_darwin:
dependency: transitive
description:
@ -193,6 +193,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy
description: A Flutter plugin for controlling the bluetooth low energy.
version: 3.0.2
version: 3.0.3
homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment:
@ -11,7 +11,7 @@ dependencies:
flutter:
sdk: flutter
bluetooth_low_energy_platform_interface: ^3.0.0
bluetooth_low_energy_android: ^3.0.3
bluetooth_low_energy_android: ^3.0.4
bluetooth_low_energy_darwin: ^3.0.2
bluetooth_low_energy_linux: ^3.0.0

View File

@ -1,3 +1,7 @@
## 3.0.4
* Fix the issue [android device: requestMtu issue #22](https://github.com/yanshouwang/bluetooth_low_energy/issues/22)
## 3.0.3
* Fix the issue that `getMaximumWriteLength` is wrong and coerce the value from 20 to 512.

View File

@ -15,7 +15,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.3"
version: "3.0.4"
bluetooth_low_energy_platform_interface:
dependency: "direct main"
description:
@ -137,6 +137,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:typed_data';
import 'package:bluetooth_low_energy_platform_interface/bluetooth_low_energy_platform_interface.dart';
import 'package:logging/logging.dart';
import 'my_api.dart';
import 'my_bluetooth_low_energy_manager.dart';
@ -10,6 +11,7 @@ import 'my_gatt_descriptor2.dart';
class MyCentralManager extends MyBluetoothLowEnergyManager
implements CentralManager, MyCentralManagerFlutterApi {
final Logger _logger;
final MyCentralManagerHostApi _api;
final StreamController<DiscoveredEventArgs> _discoveredController;
final StreamController<PeripheralStateChangedEventArgs>
@ -18,7 +20,8 @@ class MyCentralManager extends MyBluetoothLowEnergyManager
_characteristicValueChangedController;
MyCentralManager()
: _api = MyCentralManagerHostApi(),
: _logger = Logger('MyCentralManager'),
_api = MyCentralManagerHostApi(),
_discoveredController = StreamController.broadcast(),
_peripheralStateChangedController = StreamController.broadcast(),
_characteristicValueChangedController = StreamController.broadcast();
@ -100,12 +103,6 @@ class MyCentralManager extends MyBluetoothLowEnergyManager
}
final peripheralHashCodeArgs = peripheral.hashCode;
final servicesArgs = await _api.discoverGATT(peripheralHashCodeArgs);
// 部分外围设备连接后会触发 onMtuChanged 回调,若在此之前调用协商 MTU 的方法,会在协商完成前返回,
// 此时如果继续调用其他方法(如发现服务)会导致回调无法触发,
// 因此为避免此情况发生,需要延迟到发现服务完成后再协商 MTU。
// TODO: 思考更好的解决方式,可以在连接后立即协商 MTU。
const mtuArgs = 517;
await _api.requestMTU(peripheralHashCodeArgs, mtuArgs);
final services = servicesArgs
.cast<MyGattServiceArgs>()
.map((args) => args.toService2())
@ -119,6 +116,17 @@ class MyCentralManager extends MyBluetoothLowEnergyManager
}
service.peripheral = peripheral;
}
try {
// 部分外围设备连接后会触发 onMtuChanged 回调,若在此之前调用协商 MTU 的方法,会在协商完成前返回,
// 此时如果继续调用其他方法(如发现服务)会导致回调无法触发,
// 因此为避免此情况发生,需要延迟到发现服务完成后再协商 MTU。
// TODO: 思考更好的解决方式,可以在连接后立即协商 MTU。
const mtuArgs = 517;
await _api.requestMTU(peripheralHashCodeArgs, mtuArgs);
} catch (error, stack) {
// 忽略协商 MTU 错误
_logger.warning('requst MTU failed.', error, stack);
}
return services;
}

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_android
description: Android implementation of the bluetooth_low_energy plugin.
version: 3.0.3
version: 3.0.4
homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment:
@ -11,6 +11,7 @@ dependencies:
flutter:
sdk: flutter
bluetooth_low_energy_platform_interface: ^3.0.0
logging: ^1.2.0
dev_dependencies:
flutter_test: