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

Add streams for getting updates #86

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion lib/src/universal_ble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ class UniversalBle {
}

/// Connection stream of a device
Stream<bool> connectionStream(String deviceId) =>
static Stream<BleDevice> get scanStream => _platform.scanStream();

static Stream<bool> connectionStream(String deviceId) =>
_platform.connectionStream(deviceId);

static Stream<Uint8List> characteristicStream(
String deviceId, String characteristicId) =>
_platform.characteristicStream(deviceId, characteristicId);

/// Connect to a device.
/// It is advised to stop scanning before connecting.
/// It might throw errors if device is not connectable.
Expand Down
115 changes: 82 additions & 33 deletions lib/src/universal_ble_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import 'dart:typed_data';
import 'package:universal_ble/universal_ble.dart';

abstract class UniversalBlePlatform {
StreamController? _connectionStreamController;
StreamController<BleDevice>? _scanStreamController;
StreamController<({String deviceId, bool isConnected})>?
_connectionStreamController;
StreamController<
({String deviceId, String characteristicId, Uint8List value})>?
_characteristicStreamController;

final Map<String, bool> _pairStateMap = {};

OnScanResult? onScanResult;
OnConnectionChange? onConnectionChange;
OnValueChange? onValueChange;
OnAvailabilityChange? onAvailabilityChange;
OnPairingStateChange? onPairingStateChange;

Future<AvailabilityState> getBluetoothAvailabilityState();

Future<bool> enableBluetooth();
Expand Down Expand Up @@ -51,33 +63,49 @@ abstract class UniversalBlePlatform {

Future<BleConnectionState> getConnectionState(String deviceId);

Future<List<BleDevice>> getSystemDevices(
List<String>? withServices,
);
Future<List<BleDevice>> getSystemDevices(List<String>? withServices);

bool receivesAdvertisements(String deviceId) => true;

Stream<bool> connectionStream(String deviceId) {
_setupConnectionStreamIfRequired();
return _connectionStreamController!.stream
.where((event) => event.deviceId == deviceId)
.map((event) => event.isConnected);
}
Stream<BleDevice> scanStream() => _getScanStreamController().stream;

Stream<bool> connectionStream(String deviceId) =>
_getConnectionStreamController()
.stream
.where((event) => event.deviceId == deviceId)
.map((event) => event.isConnected);

Stream<Uint8List> characteristicStream(
String deviceId, String characteristicId) =>
_getCharacteristicStreamController()
.stream
.where((event) =>
event.deviceId == deviceId &&
event.characteristicId == characteristicId)
.map((event) => event.value);

void updateScanResult(BleDevice bleDevice) {
onScanResult?.call(bleDevice);
_scanStreamController?.add(bleDevice);
}

void updateConnection(String deviceId, bool isConnected) {
onConnectionChange?.call(deviceId, isConnected);
_connectionStreamController
?.add((deviceId: deviceId, isConnected: isConnected));
_connectionStreamController?.add((
deviceId: deviceId,
isConnected: isConnected,
));
}

void updateCharacteristicValue(
String deviceId, String characteristicId, Uint8List value) {
onValueChange?.call(
deviceId, BleUuidParser.string(characteristicId), value);
_characteristicStreamController?.add((
deviceId: deviceId,
characteristicId: BleUuidParser.string(characteristicId),
value: value,
));
}

void updateAvailability(AvailabilityState state) {
Expand All @@ -90,31 +118,52 @@ abstract class UniversalBlePlatform {
onPairingStateChange?.call(deviceId, isPaired);
}

// Do not use these directly to push updates
OnScanResult? onScanResult;
OnConnectionChange? onConnectionChange;
OnValueChange? onValueChange;
OnAvailabilityChange? onAvailabilityChange;
OnPairingStateChange? onPairingStateChange;

static void logInfo(String message, {bool isError = false}) {
if (isError) message = '\x1B[31m$message\x1B[31m';
log(message, name: 'UniversalBle');
/// Setup autoDisposable StreamControllers
StreamController<({String deviceId, bool isConnected})>
_getConnectionStreamController() {
if (_connectionStreamController != null) {
_connectionStreamController =
StreamController<({String deviceId, bool isConnected})>.broadcast();
_connectionStreamController?.onCancel = () {
_connectionStreamController?.close();
_connectionStreamController = null;
};
}
return _connectionStreamController!;
}

/// Creates an auto disposable streamController
void _setupConnectionStreamIfRequired() {
if (_connectionStreamController != null) return;
StreamController<
({String deviceId, String characteristicId, Uint8List value})>
_getCharacteristicStreamController() {
if (_characteristicStreamController == null) {
_characteristicStreamController = StreamController<
({
String deviceId,
String characteristicId,
Uint8List value
})>.broadcast();
_characteristicStreamController?.onCancel = () {
_characteristicStreamController?.close();
_characteristicStreamController = null;
};
}
return _characteristicStreamController!;
}

_connectionStreamController =
StreamController<({String deviceId, bool isConnected})>.broadcast();
StreamController<BleDevice> _getScanStreamController() {
if (_scanStreamController == null) {
_scanStreamController = StreamController<BleDevice>.broadcast();
_scanStreamController?.onCancel = () {
_scanStreamController?.close();
_scanStreamController = null;
};
}
return _scanStreamController!;
}

// Auto dispose if no more subscribers
_connectionStreamController?.onCancel = () {
// logInfo('Disposing Connection Stream');
_connectionStreamController?.close();
_connectionStreamController = null;
};
static void logInfo(String message, {bool isError = false}) {
if (isError) message = '\x1B[31m$message\x1B[31m';
log(message, name: 'UniversalBle');
}
}

Expand Down
Loading