Skip to content

Use .timeout() for timeout #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -54,5 +54,5 @@ protobuf {
}

dependencies {
implementation 'com.google.protobuf:protobuf-javalite:3.11.4'
implementation 'com.google.protobuf:protobuf-javalite:3.17.3'
}
42 changes: 14 additions & 28 deletions lib/src/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<BluetoothService>> _services =
BehaviorSubject.seeded([]);
BehaviorSubject<List<BluetoothService>> _services = BehaviorSubject.seeded([]);

/// Discovers services offered by the remote device as well as their characteristics and descriptors
Future<List<BluetoothService>> 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")
Expand All @@ -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);

Expand All @@ -85,8 +77,7 @@ class BluetoothDevice {
Stream<List<BluetoothService>> 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;
}
Expand Down Expand Up @@ -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<bool> get canSendWriteWithoutResponse =>
new Future.error(new UnimplementedError());
Future<bool> 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;
Expand Down