diff --git a/android/build.gradle b/android/build.gradle index 6a2ce0de..27b2555b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -40,7 +40,7 @@ android { protobuf { protoc { - artifact = 'com.google.protobuf:protoc:3.11.4' + artifact = 'com.google.protobuf:protoc:3.17.3' } generateProtoTasks { all().each { task -> @@ -54,5 +54,5 @@ protobuf { } dependencies { - implementation 'com.google.protobuf:protobuf-javalite:3.11.4' + implementation 'com.google.protobuf:protobuf-javalite:3.17.3' } \ No newline at end of file diff --git a/lib/src/bluetooth_device.dart b/lib/src/bluetooth_device.dart index ca3bb9ba..e0e2021e 100644 --- a/lib/src/bluetooth_device.dart +++ b/lib/src/bluetooth_device.dart @@ -26,37 +26,30 @@ class BluetoothDevice { ..remoteId = id.toString() ..androidAutoConnect = autoConnect; - Timer? timer; - if (timeout != null) { - timer = Timer(timeout, () { + await FlutterBlue.instance._channel.invokeMethod('connect', request.writeToBuffer()); + + if (timeout == null) { + await state.firstWhere((s) => s == BluetoothDeviceState.connected); + } else { + await state.firstWhere((s) => s == BluetoothDeviceState.connected).timeout(timeout, onTimeout: () { disconnect(); throw TimeoutException('Failed to connect in time.', timeout); }); } - await FlutterBlue.instance._channel - .invokeMethod('connect', request.writeToBuffer()); - - await state.firstWhere((s) => s == BluetoothDeviceState.connected); - - timer?.cancel(); - return; } /// Cancels connection to the Bluetooth Device - Future disconnect() => - FlutterBlue.instance._channel.invokeMethod('disconnect', id.toString()); + Future disconnect() => FlutterBlue.instance._channel.invokeMethod('disconnect', id.toString()); - BehaviorSubject> _services = - BehaviorSubject.seeded([]); + BehaviorSubject> _services = BehaviorSubject.seeded([]); /// Discovers services offered by the remote device as well as their characteristics and descriptors Future> discoverServices() async { final s = await state.first; if (s != BluetoothDeviceState.connected) { - return Future.error(new Exception( - 'Cannot discoverServices while device is not connected. State == $s')); + return Future.error(new Exception('Cannot discoverServices while device is not connected. State == $s')); } var response = FlutterBlue.instance._methodStream .where((m) => m.method == "DiscoverServicesResult") @@ -72,8 +65,7 @@ class BluetoothDevice { return list; }); - await FlutterBlue.instance._channel - .invokeMethod('discoverServices', id.toString()); + await FlutterBlue.instance._channel.invokeMethod('discoverServices', id.toString()); _isDiscoveringServices.add(true); @@ -85,8 +77,7 @@ class BluetoothDevice { Stream> get services async* { yield await FlutterBlue.instance._channel .invokeMethod('services', id.toString()) - .then((buffer) => - new protos.DiscoverServicesResult.fromBuffer(buffer).services) + .then((buffer) => new protos.DiscoverServicesResult.fromBuffer(buffer).services) .then((i) => i.map((s) => new BluetoothService.fromProto(s)).toList()); yield* _services.stream; } @@ -128,20 +119,15 @@ class BluetoothDevice { ..remoteId = id.toString() ..mtu = desiredMtu; - return FlutterBlue.instance._channel - .invokeMethod('requestMtu', request.writeToBuffer()); + return FlutterBlue.instance._channel.invokeMethod('requestMtu', request.writeToBuffer()); } /// Indicates whether the Bluetooth Device can send a write without response - Future get canSendWriteWithoutResponse => - new Future.error(new UnimplementedError()); + Future get canSendWriteWithoutResponse => new Future.error(new UnimplementedError()); @override bool operator ==(Object other) => - identical(this, other) || - other is BluetoothDevice && - runtimeType == other.runtimeType && - id == other.id; + identical(this, other) || other is BluetoothDevice && runtimeType == other.runtimeType && id == other.id; @override int get hashCode => id.hashCode;