Skip to content

Commit

Permalink
dartfmt on src folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldemarco committed Dec 22, 2018
1 parent 5b34e20 commit 1300f23
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 96 deletions.
17 changes: 9 additions & 8 deletions lib/flutter_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ library flutter_blue;

import 'dart:async';

import 'package:flutter/services.dart';
import 'gen/flutterblue.pb.dart' as protos;
import 'package:meta/meta.dart';
import 'package:collection/collection.dart';
import 'package:convert/convert.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

import 'gen/flutterblue.pb.dart' as protos;

part 'src/flutter_blue.dart';
part 'src/constants.dart';
part 'src/bluetooth_device.dart';
part 'src/bluetooth_service.dart';
part 'src/bluetooth_characteristic.dart';
part 'src/bluetooth_descriptor.dart';
part 'src/guid.dart';
part 'src/bluetooth_device.dart';
part 'src/bluetooth_service.dart';
part 'src/constants.dart';
part 'src/flutter_blue.dart';
part 'src/guid.dart';
17 changes: 10 additions & 7 deletions lib/src/bluetooth_characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class BluetoothCharacteristic {
final List<BluetoothDescriptor> descriptors;
bool get isNotifying {
try {
var cccd = descriptors.singleWhere((d) =>
d.uuid == BluetoothDescriptor.CCCD);
var cccd =
descriptors.singleWhere((d) => d.uuid == BluetoothDescriptor.CCCD);
return ((cccd.value[0] & 0x01) > 0 || (cccd.value[0] & 0x02) > 0);
} catch(e) {
} catch (e) {
return false;
}
}

List<int> value;

BluetoothCharacteristic(
Expand All @@ -32,17 +33,19 @@ class BluetoothCharacteristic {
BluetoothCharacteristic.fromProto(protos.BluetoothCharacteristic p)
: uuid = new Guid(p.uuid),
serviceUuid = new Guid(p.serviceUuid),
secondaryServiceUuid = (p.secondaryServiceUuid.length > 0) ? new Guid(p.secondaryServiceUuid): null,
secondaryServiceUuid = (p.secondaryServiceUuid.length > 0)
? new Guid(p.secondaryServiceUuid)
: null,
descriptors = p.descriptors
.map((d) => new BluetoothDescriptor.fromProto(d))
.toList(),
properties = new CharacteristicProperties.fromProto(p.properties),
value = p.value;

void updateDescriptors(List<BluetoothDescriptor> newDescriptors) {
for(var d in descriptors) {
for(var newD in newDescriptors){
if(d.uuid == newD.uuid) {
for (var d in descriptors) {
for (var newD in newDescriptors) {
if (d.uuid == newD.uuid) {
d.value = newD.value;
}
}
Expand Down
20 changes: 12 additions & 8 deletions lib/src/bluetooth_descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ class BluetoothDescriptor {

final Guid uuid;
final Guid serviceUuid; // The service that this descriptor belongs to.
final Guid characteristicUuid; // The characteristic that this descriptor belongs to.
final Guid
characteristicUuid; // The characteristic that this descriptor belongs to.
List<int> value;

BluetoothDescriptor({@required this.uuid, @required this.serviceUuid, @required this.characteristicUuid});
BluetoothDescriptor(
{@required this.uuid,
@required this.serviceUuid,
@required this.characteristicUuid});

BluetoothDescriptor.fromProto(protos.BluetoothDescriptor p) :
uuid = new Guid(p.uuid),
serviceUuid = new Guid(p.serviceUuid),
characteristicUuid = new Guid(p.characteristicUuid),
value = p.value;
}
BluetoothDescriptor.fromProto(protos.BluetoothDescriptor p)
: uuid = new Guid(p.uuid),
serviceUuid = new Guid(p.serviceUuid),
characteristicUuid = new Guid(p.characteristicUuid),
value = p.value;
}
92 changes: 50 additions & 42 deletions lib/src/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class BluetoothDevice {
..characteristicUuid = characteristic.uuid.toString()
..serviceUuid = characteristic.serviceUuid.toString();

print('remoteId: ${id.toString()} characteristicUuid: ${characteristic.uuid.toString()} serviceUuid: ${characteristic.serviceUuid.toString()}');
print(
'remoteId: ${id.toString()} characteristicUuid: ${characteristic.uuid.toString()} serviceUuid: ${characteristic.serviceUuid.toString()}');

await FlutterBlue.instance._channel
.invokeMethod('readCharacteristic', request.writeToBuffer());
Expand Down Expand Up @@ -82,8 +83,7 @@ class BluetoothDevice {

return await FlutterBlue.instance._descriptorReadChannel
.receiveBroadcastStream()
.map((buffer) =>
new protos.ReadDescriptorResponse.fromBuffer(buffer))
.map((buffer) => new protos.ReadDescriptorResponse.fromBuffer(buffer))
.where((p) =>
(p.request.remoteId == request.remoteId) &&
(p.request.descriptorUuid == request.descriptorUuid) &&
Expand All @@ -99,40 +99,46 @@ class BluetoothDevice {
/// guaranteed and will return immediately with success.
/// [CharacteristicWriteType.withResponse]: the method will return after the
/// write operation has either passed or failed.
Future<Null> writeCharacteristic(BluetoothCharacteristic characteristic, List<int> value,
Future<Null> writeCharacteristic(
BluetoothCharacteristic characteristic, List<int> value,
{CharacteristicWriteType type =
CharacteristicWriteType.withoutResponse}) async {
var request = protos.WriteCharacteristicRequest.create()
..remoteId = id.toString()
..characteristicUuid = characteristic.uuid.toString()
..serviceUuid = characteristic.serviceUuid.toString()
..writeType = protos.WriteCharacteristicRequest_WriteType.valueOf(type.index)
..writeType =
protos.WriteCharacteristicRequest_WriteType.valueOf(type.index)
..value = value;

var result = await FlutterBlue.instance._channel
.invokeMethod('writeCharacteristic', request.writeToBuffer());

if(type == CharacteristicWriteType.withoutResponse) {
if (type == CharacteristicWriteType.withoutResponse) {
return result;
}

return await FlutterBlue.instance._methodStream
.where((m) => m.method == "WriteCharacteristicResponse")
.map((m) => m.arguments)
.map((buffer) => new protos.WriteCharacteristicResponse.fromBuffer(buffer))
.where((p) =>
(p.request.remoteId == request.remoteId) &&
(p.request.characteristicUuid == request.characteristicUuid) &&
(p.request.serviceUuid == request.serviceUuid))
.first
.then((w) => w.success)
.then((success) => (!success) ? throw new Exception('Failed to write the characteristic') : null)
.then((_) => characteristic.value = value)
.then((_) => null);
.where((m) => m.method == "WriteCharacteristicResponse")
.map((m) => m.arguments)
.map((buffer) =>
new protos.WriteCharacteristicResponse.fromBuffer(buffer))
.where((p) =>
(p.request.remoteId == request.remoteId) &&
(p.request.characteristicUuid == request.characteristicUuid) &&
(p.request.serviceUuid == request.serviceUuid))
.first
.then((w) => w.success)
.then((success) => (!success)
? throw new Exception('Failed to write the characteristic')
: null)
.then((_) => characteristic.value = value)
.then((_) => null);
}

/// Writes the value of a descriptor
Future<Null> writeDescriptor(BluetoothDescriptor descriptor, List<int> value) async {
Future<Null> writeDescriptor(
BluetoothDescriptor descriptor, List<int> value) async {
var request = protos.WriteDescriptorRequest.create()
..remoteId = id.toString()
..descriptorUuid = descriptor.uuid.toString()
Expand All @@ -148,13 +154,15 @@ class BluetoothDevice {
.map((m) => m.arguments)
.map((buffer) => new protos.WriteDescriptorResponse.fromBuffer(buffer))
.where((p) =>
(p.request.remoteId == request.remoteId) &&
(p.request.descriptorUuid == request.descriptorUuid) &&
(p.request.characteristicUuid == request.characteristicUuid) &&
(p.request.serviceUuid == request.serviceUuid))
(p.request.remoteId == request.remoteId) &&
(p.request.descriptorUuid == request.descriptorUuid) &&
(p.request.characteristicUuid == request.characteristicUuid) &&
(p.request.serviceUuid == request.serviceUuid))
.first
.then((w) => w.success)
.then((success) => (!success) ? throw new Exception('Failed to write the descriptor') : null)
.then((success) => (!success)
? throw new Exception('Failed to write the descriptor')
: null)
.then((_) => descriptor.value = value)
.then((_) => null);
}
Expand All @@ -168,24 +176,24 @@ class BluetoothDevice {
..characteristicUuid = characteristic.uuid.toString()
..enable = notify;

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

return await FlutterBlue.instance._methodStream
.where((m) => m.method == "SetNotificationResponse")
.map((m) => m.arguments)
.map((buffer) =>
new protos.SetNotificationResponse.fromBuffer(buffer))
.map((buffer) => new protos.SetNotificationResponse.fromBuffer(buffer))
.where((p) =>
(p.remoteId == request.remoteId) &&
(p.characteristic.uuid == request.characteristicUuid) &&
(p.characteristic.serviceUuid == request.serviceUuid))
(p.remoteId == request.remoteId) &&
(p.characteristic.uuid == request.characteristicUuid) &&
(p.characteristic.serviceUuid == request.serviceUuid))
.first
.then((p) => new BluetoothCharacteristic.fromProto(p.characteristic))
.then((c) {
characteristic.updateDescriptors(c.descriptors);
characteristic.value = c.value;
return (c.isNotifying == notify);
});
characteristic.updateDescriptors(c.descriptors);
characteristic.value = c.value;
return (c.isNotifying == notify);
});
}

/// Notifies when the characteristic's value has changed.
Expand All @@ -199,17 +207,17 @@ class BluetoothDevice {
.map((p) => new BluetoothCharacteristic.fromProto(p.characteristic))
.where((c) => c.uuid == characteristic.uuid)
.map((c) {
characteristic.updateDescriptors(c.descriptors);
characteristic.value = c.value;
return c.value;
});
characteristic.updateDescriptors(c.descriptors);
characteristic.value = c.value;
return c.value;
});
}

/// The current connection state of the device
Future<BluetoothDeviceState> get state =>
FlutterBlue.instance._channel.invokeMethod('deviceState', id.toString())
.then((buffer) => new protos.DeviceStateResponse.fromBuffer(buffer))
.then((p) => BluetoothDeviceState.values[p.state.value]);
Future<BluetoothDeviceState> get state => FlutterBlue.instance._channel
.invokeMethod('deviceState', id.toString())
.then((buffer) => new protos.DeviceStateResponse.fromBuffer(buffer))
.then((p) => BluetoothDeviceState.values[p.state.value]);

/// Notifies when the device connection state has changed
Stream<BluetoothDeviceState> onStateChanged() {
Expand Down
8 changes: 5 additions & 3 deletions lib/src/bluetooth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class BluetoothService {
deviceId = new DeviceIdentifier(p.remoteId),
isPrimary = p.isPrimary,
characteristics = p.characteristics
.map((c) => new BluetoothCharacteristic.fromProto(c)).toList(),
includedServices =
p.includedServices.map((s) => new BluetoothService.fromProto(s)).toList();
.map((c) => new BluetoothCharacteristic.fromProto(c))
.toList(),
includedServices = p.includedServices
.map((s) => new BluetoothService.fromProto(s))
.toList();
}
2 changes: 1 addition & 1 deletion lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

part of flutter_blue;

const NAMESPACE = 'plugins.pauldemarco.com/flutter_blue';
const NAMESPACE = 'plugins.pauldemarco.com/flutter_blue';
Loading

0 comments on commit 1300f23

Please sign in to comment.