Skip to content
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

Handle some common descriptors #896

Open
wants to merge 6 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
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ android {
}
}
}
buildTypes {
release {
consumerProguardFiles 'proguard-rules.pro'
}
}
}

protobuf {
Expand Down
2 changes: 2 additions & 0 deletions android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Retain Flutter Blue protocol buffer files
-keep class com.pauldemarco.flutter_blue.** { *; }
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BluetoothOffScreen extends StatelessWidget {
'Bluetooth Adapter is ${state != null ? state.toString().substring(15) : 'not available'}.',
style: Theme.of(context)
.primaryTextTheme
.subhead
.subtitle1
?.copyWith(color: Colors.white),
),
],
Expand Down
10 changes: 5 additions & 5 deletions example/lib/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ServiceTile extends StatelessWidget {
children: <Widget>[
Text('Service'),
Text('0x${service.uuid.toString().toUpperCase().substring(4, 8)}',
style: Theme.of(context).textTheme.body1?.copyWith(
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color: Theme.of(context).textTheme.caption?.color))
],
),
Expand Down Expand Up @@ -183,7 +183,7 @@ class CharacteristicTile extends StatelessWidget {
Text('Characteristic'),
Text(
'0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}',
style: Theme.of(context).textTheme.body1?.copyWith(
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color: Theme.of(context).textTheme.caption?.color))
],
),
Expand Down Expand Up @@ -245,7 +245,7 @@ class DescriptorTile extends StatelessWidget {
Text('0x${descriptor.uuid.toString().toUpperCase().substring(4, 8)}',
style: Theme.of(context)
.textTheme
.body1
.bodyText1
?.copyWith(color: Theme.of(context).textTheme.caption?.color))
],
),
Expand Down Expand Up @@ -289,11 +289,11 @@ class AdapterStateTile extends StatelessWidget {
child: ListTile(
title: Text(
'Bluetooth adapter is ${state.toString().substring(15)}',
style: Theme.of(context).primaryTextTheme.subhead,
style: Theme.of(context).primaryTextTheme.subtitle1,
),
trailing: Icon(
Icons.error,
color: Theme.of(context).primaryTextTheme.subhead?.color,
color: Theme.of(context).primaryTextTheme.subtitle1?.color,
),
),
);
Expand Down
25 changes: 23 additions & 2 deletions ios/Classes/FlutterBluePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,29 @@ - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDes
}
ProtosReadDescriptorResponse *result = [[ProtosReadDescriptorResponse alloc] init];
[result setRequest:q];
int value = [descriptor.value intValue];
[result setValue:[NSData dataWithBytes:&value length:sizeof(value)]];
// Descriptors returning NSNumber (UInt16)
if ([descriptor.UUID.UUIDString isEqualToString: CBUUIDCharacteristicExtendedPropertiesString] ||
[descriptor.UUID.UUIDString isEqualToString: CBUUIDClientCharacteristicConfigurationString] ||
[descriptor.UUID.UUIDString isEqualToString: CBUUIDServerCharacteristicConfigurationString] )
{
int value = [descriptor.value unsignedShortValue];
[result setValue:[NSData dataWithBytes:&value length:sizeof(value)]];
}

// Descriptors returning NSString
else if ([descriptor.UUID.UUIDString isEqualToString: CBUUIDCharacteristicUserDescriptionString])
{
NSData *value = [descriptor.value dataUsingEncoding: NSUTF8StringEncoding];
[result setValue: value];
}

// Descriptors returning NSData
else if ([descriptor.UUID.UUIDString isEqualToString: CBUUIDCharacteristicFormatString] ||
[descriptor.UUID.UUIDString isEqualToString: CBUUIDCharacteristicAggregateFormatString])
{
[result setValue:descriptor.value];
}

[_channel invokeMethod:@"ReadDescriptorResponse" arguments:[self toFlutterData:result]];

// If descriptor is CCCD, send a SetNotificationResponse in case anything is awaiting
Expand Down
43 changes: 38 additions & 5 deletions lib/src/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,55 @@ class BluetoothDevice {
..remoteId = id.toString()
..androidAutoConnect = autoConnect;

Completer<void> _completer = Completer();

Timer? timer;
if (timeout != null) {
timer = Timer(timeout, () {
disconnect();
throw TimeoutException('Failed to connect in time.', timeout);
if (!_completer.isCompleted) {
_completer.completeError(
TimeoutException('Failed to connect in time.', timeout));
}
});
}

await FlutterBlue.instance._channel
.invokeMethod('connect', request.writeToBuffer());

await state.firstWhere((s) => s == BluetoothDeviceState.connected);

timer?.cancel();
state.firstWhere((_state) {
bool connected = _state == BluetoothDeviceState.connected;
if (connected) {
timer?.cancel();
if (!_completer.isCompleted) {
_completer.complete();
}
}
return connected;
}, orElse: () {
if (!_completer.isCompleted) {
timer?.cancel();
disconnect();
_completer.completeError(
TimeoutException('Failed to connect in time.', timeout));
}
return BluetoothDeviceState.disconnected;
}).then((_state) {
timer?.cancel();
if (_state == BluetoothDeviceState.connected) {
if (!_completer.isCompleted) {
_completer.complete();
}
}
}).catchError((error) {
timer?.cancel();
disconnect();
if (!_completer.isCompleted) {
_completer.completeError(error);
}
});

return;
return _completer.future;
}

/// Cancels connection to the Bluetooth Device
Expand Down