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

Unit tests for peripheral #442

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
14 changes: 8 additions & 6 deletions lib/characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Characteristic extends InternalCharacteristic {
Service service;

ManagerForCharacteristic _manager;
TransactionIdGenerator _transactionIdGenerator;

/// The UUID of this characteristic.
String uuid;
Expand All @@ -41,9 +42,10 @@ class Characteristic extends InternalCharacteristic {
bool isIndicatable;

Characteristic.fromJson(Map<String, dynamic> jsonObject, Service service,
ManagerForCharacteristic manager)
ManagerForCharacteristic manager, {TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()})
mikolak marked this conversation as resolved.
Show resolved Hide resolved
: super(jsonObject[_CharacteristicMetadata.id]) {
_manager = manager;
_transactionIdGenerator = transactionIdGenerator;
this.service = service;
uuid = jsonObject[_CharacteristicMetadata.uuid];
isReadable = jsonObject[_CharacteristicMetadata.isReadable];
Expand All @@ -62,7 +64,7 @@ class Characteristic extends InternalCharacteristic {
_manager.readCharacteristicForIdentifier(
service.peripheral,
this,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes to the value of this characteristic.
Expand All @@ -80,7 +82,7 @@ class Characteristic extends InternalCharacteristic {
this,
value,
withResponse,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a [Stream] of notifications/indications emitted by this
Expand All @@ -95,7 +97,7 @@ class Characteristic extends InternalCharacteristic {
_manager.monitorCharacteristicForIdentifier(
service.peripheral,
this,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a list of [Descriptor]s of this characteristic.
Expand All @@ -110,7 +112,7 @@ class Characteristic extends InternalCharacteristic {
_manager.readDescriptorForCharacteristic(
this,
descriptorUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes the [value] of a [Descriptor] identified by [descriptorUuid].
Expand All @@ -123,7 +125,7 @@ class Characteristic extends InternalCharacteristic {
this,
descriptorUuid,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
7 changes: 5 additions & 2 deletions lib/descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@ abstract class _DescriptorMetadata {

class Descriptor extends InternalDescriptor {
ManagerForDescriptor _manager;
TransactionIdGenerator _transactionIdGenerator;
Characteristic characteristic;
String uuid;

Descriptor.fromJson(
Map<String, dynamic> jsonObject,
Characteristic characteristic,
ManagerForDescriptor manager,
{TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()}
mikolak marked this conversation as resolved.
Show resolved Hide resolved
) : super(jsonObject[_DescriptorMetadata.id]) {
_manager = manager;
_transactionIdGenerator = transactionIdGenerator;
this.characteristic = characteristic;
uuid = jsonObject[_DescriptorMetadata.uuid];
}

Future<Uint8List> read({String transactionId}) =>
_manager.readDescriptorForIdentifier(
this,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

Future<void> write(Uint8List value, {String transactionId}) =>
_manager.writeDescriptorForIdentifier(
this,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
22 changes: 12 additions & 10 deletions lib/peripheral.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ abstract class _PeripheralMetadata {
class Peripheral {
static const int NO_MTU_NEGOTIATION = 0;
ManagerForPeripheral _manager;
TransactionIdGenerator _transactionIdGenerator;

String name;
String identifier;

Peripheral.fromJson(Map<String, dynamic> json, ManagerForPeripheral manager)
Peripheral.fromJson(Map<String, dynamic> json, ManagerForPeripheral manager, {TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()})
mikolak marked this conversation as resolved.
Show resolved Hide resolved
: _manager = manager,
name = json[_PeripheralMetadata.name],
identifier = json[_PeripheralMetadata.identifier];
identifier = json[_PeripheralMetadata.identifier],
_transactionIdGenerator = transactionIdGenerator;

/// Connects to the peripheral.
///
Expand Down Expand Up @@ -82,7 +84,7 @@ class Peripheral {
/// Optional [transactionId] could be used to cancel operation.
Future<void> discoverAllServicesAndCharacteristics({String transactionId}) =>
_manager.discoverAllServicesAndCharacteristics(
this, transactionId ?? TransactionIdGenerator.getNextId());
this, transactionId ?? _transactionIdGenerator.getNextId());

/// Returns a list of [Service]s of this peripheral.
///
Expand All @@ -103,7 +105,7 @@ class Peripheral {
///
/// Optional [transactionId] could be used to cancel operation.
Future<int> rssi({String transactionId}) =>
_manager.rssi(this, transactionId ?? TransactionIdGenerator.getNextId());
_manager.rssi(this, transactionId ?? _transactionIdGenerator.getNextId());

/// Requests new MTU value for current connection and return the negotiation
/// result on Android, reads MTU on iOS.
Expand All @@ -118,7 +120,7 @@ class Peripheral {
/// If MTU has been requested in [connect()] this method will end with [BleError].
Future<int> requestMtu(int mtu, {String transactionId}) =>
_manager.requestMtu(
this, mtu, transactionId ?? TransactionIdGenerator.getNextId());
this, mtu, transactionId ?? _transactionIdGenerator.getNextId());

/// Reads value of [Characteristic] matching specified UUIDs.
///
Expand All @@ -135,7 +137,7 @@ class Peripheral {
this,
serviceUuid,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes value of [Characteristic] matching specified UUIDs.
Expand All @@ -157,7 +159,7 @@ class Peripheral {
characteristicUuid,
value,
withResponse,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a list of [Descriptor]s for [Characteristic] matching specified UUIDs.
Expand Down Expand Up @@ -191,7 +193,7 @@ class Peripheral {
serviceUuid,
characteristicUuid,
descriptorUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes value of [Descriptor] matching specified UUIDs.
Expand All @@ -214,7 +216,7 @@ class Peripheral {
characteristicUuid,
descriptorUuid,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a stream of notifications/indications from [Characteristic]
Expand All @@ -236,7 +238,7 @@ class Peripheral {
this,
serviceUuid,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
13 changes: 8 additions & 5 deletions lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Service extends InternalService {
Peripheral peripheral;

ManagerForService _manager;
TransactionIdGenerator _transactionIdGenerator;

/// The UUID of this service.
String uuid;
Expand All @@ -19,10 +20,12 @@ class Service extends InternalService {
Map<String, dynamic> jsonObject,
Peripheral peripheral,
ManagerForService managerForService,
{TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()}
mikolak marked this conversation as resolved.
Show resolved Hide resolved
) : super(jsonObject[_ServiceMetadata.id]) {
this.peripheral = peripheral;
uuid = jsonObject[_ServiceMetadata.uuid];
_manager = managerForService;
_transactionIdGenerator = transactionIdGenerator;
}

/// Returns a list of [Characteristic]s of this service.
Expand Down Expand Up @@ -51,7 +54,7 @@ class Service extends InternalService {
characteristicUuid,
value,
withResponse,
transactionId ?? TransactionIdGenerator.getNextId());
transactionId ?? _transactionIdGenerator.getNextId());

/// Reads the value of a [Characteristic] identified by [characteristicUuid].
///
Expand All @@ -67,7 +70,7 @@ class Service extends InternalService {
peripheral,
this,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a [Stream] of values emitted by a [Characteristic] identified by
Expand All @@ -86,7 +89,7 @@ class Service extends InternalService {
peripheral,
this,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a list of [Descriptor]s of a [Characteristic] identified by
Expand Down Expand Up @@ -114,7 +117,7 @@ class Service extends InternalService {
this,
characteristicUuid,
descriptorUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes the [value] of a [Descriptor] identified by [descriptorUuid]
Expand All @@ -132,7 +135,7 @@ class Service extends InternalService {
characteristicUuid,
descriptorUuid,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/_managers_for_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class ManagerForPeripheral {
String transactionId,
);

Future<void> requestMtu(
Future<int> requestMtu(
Peripheral peripheral,
int mtu,
String transactionId,
Expand Down
10 changes: 6 additions & 4 deletions lib/src/internal_ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class InternalBleManager
ManagerForCharacteristic,
ManagerForDescriptor {
FlutterBleLib _bleLib;
TransactionIdGenerator _transactionIdGenerator;

InternalBleManager() {
InternalBleManager({TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()}) {
mikolak marked this conversation as resolved.
Show resolved Hide resolved
_bleLib = FlutterBleLib();
_bleLib.registerManager(this);
_transactionIdGenerator = transactionIdGenerator;
}

@override
Expand All @@ -36,11 +38,11 @@ class InternalBleManager

@override
Future<void> enableRadio({String transactionId}) =>
_bleLib.enableRadio(transactionId ?? TransactionIdGenerator.getNextId());
_bleLib.enableRadio(transactionId ?? _transactionIdGenerator.getNextId());

@override
Future<void> disableRadio({String transactionId}) =>
_bleLib.disableRadio(transactionId ?? TransactionIdGenerator.getNextId());
_bleLib.disableRadio(transactionId ?? _transactionIdGenerator.getNextId());

@override
Future<BluetoothState> bluetoothState() => _bleLib.state();
Expand Down Expand Up @@ -172,7 +174,7 @@ class InternalBleManager
}

@override
Future<void> requestMtu(
Future<int> requestMtu(
Peripheral peripheral, int mtu, String transactionId) {
return _bleLib.requestMtu(peripheral, mtu, transactionId);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/src/util/_transaction_id_generator.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
abstract class TransactionIdGenerator {
class TransactionIdGenerator {
static int _id = 0;

static String getNextId() {

const TransactionIdGenerator();
mikolak marked this conversation as resolved.
Show resolved Hide resolved

String getNextId() {
_id++;
return _id.toString();
}
Expand Down
Loading