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

Advanced linux support #30

Merged
merged 3 commits into from
Jun 19, 2022
Merged
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
102 changes: 83 additions & 19 deletions packages/quick_blue/lib/src/quick_blue_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:typed_data';

import 'package:bluez/bluez.dart';
import 'package:collection/collection.dart';
import 'package:convert/convert.dart';
import 'package:logging/logging.dart';

import 'quick_blue_platform_interface.dart';
Expand All @@ -24,7 +25,12 @@ class QuickBlueLinux extends QuickBluePlatform {
await _client.connect();

_activeAdapter ??= _client.adapters.firstWhereOrNull((adapter) => adapter.powered);

if (_activeAdapter == null && _client.adapters.isNotEmpty) {
await _client.adapters.first.setPowered(true);
_activeAdapter = _client.adapters.first;
} else {
throw Exception('Bluetooth adapter unavailable');
}
_client.deviceAdded.listen(_onDeviceAdd);

isInitialized = true;
Expand Down Expand Up @@ -55,16 +61,20 @@ class QuickBlueLinux extends QuickBluePlatform {
await _ensureInitialized();
_log('startScan invoke success');

_activeAdapter!.startDiscovery();
_client.devices.forEach(_onDeviceAdd);
if (!_activeAdapter!.discovering) {
_activeAdapter!.startDiscovery();
_client.devices.forEach(_onDeviceAdd);
}
}

@override
Future<void> stopScan() async {
await _ensureInitialized();
_log('stopScan invoke success');

_activeAdapter!.stopDiscovery();
if (!_activeAdapter!.discovering) {
_activeAdapter!.stopDiscovery();
}
}

// FIXME Close
Expand All @@ -82,40 +92,94 @@ class QuickBlueLinux extends QuickBluePlatform {
});
}

BlueZDevice _findDeviceById(String deviceId) {
var device = _client.devices.firstWhereOrNull((device) => device.address == deviceId);
if (device == null) {
throw Exception('Unknown deviceId:$deviceId');
}
return device;
}

@override
void connect(String deviceId) {
// TODO: implement connect
throw UnimplementedError();
_findDeviceById(deviceId).connect().then((_) {
onConnectionChanged?.call(deviceId, BlueConnectionState.connected);
});
}

@override
void disconnect(String deviceId) {
// TODO: implement disconnect
throw UnimplementedError();
_findDeviceById(deviceId).disconnect().then((_) {
onConnectionChanged?.call(deviceId, BlueConnectionState.disconnected);
});
}

@override
void discoverServices(String deviceId) {
// TODO: implement discoverServices
throw UnimplementedError();
var device = _findDeviceById(deviceId);

for (var service in device.gattServices) {
_log("Service ${service.uuid}");
for (var characteristic in service.characteristics) {
_log(" Characteristic ${characteristic.uuid}");
}

var characteristics = service.characteristics.map((e) => e.uuid.toString()).toList();
onServiceDiscovered?.call(deviceId, service.uuid.toString(), characteristics);
}
}

BlueZGattCharacteristic _getCharacteristic(String deviceId, String service, String characteristic) {
var device = _findDeviceById(deviceId);
var s = device.gattServices.firstWhereOrNull((s) => s.uuid.toString() == service);
var c = s?.characteristics.firstWhereOrNull((c) => c.uuid.toString() == characteristic);

if (c == null) {
throw Exception('Unknown characteristic:$characteristic');
}
return c;
}

final Map<String, StreamSubscription<List<String>>> _characteristicPropertiesSubcriptions = {};

@override
Future<void> setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) {
// TODO: implement setNotifiable
throw UnimplementedError();
Future<void> setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async {
var c = _getCharacteristic(deviceId, service, characteristic);

if (bleInputProperty != BleInputProperty.disabled) {
c.startNotify();
void onPropertiesChanged(properties) {
if (properties.contains('Value')) {
_log('onCharacteristicPropertiesChanged $characteristic, ${hex.encode(c.value)}');
onValueChanged?.call(deviceId, characteristic, Uint8List.fromList(c.value));
}
}
_characteristicPropertiesSubcriptions[characteristic] ??= c.propertiesChanged.listen(onPropertiesChanged);
} else {
c.stopNotify();
_characteristicPropertiesSubcriptions.remove(characteristic)?.cancel();
}
}

@override
Future<void> readValue(String deviceId, String service, String characteristic) {
// TODO: implement readValue
throw UnimplementedError();
Future<void> readValue(String deviceId, String service, String characteristic) async {
var c = _getCharacteristic(deviceId, service, characteristic);

var data = await c.readValue();
_log('readValue $characteristic, ${hex.encode(data)}');
onValueChanged?.call(deviceId, characteristic, Uint8List.fromList(data));
}

@override
Future<void> writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty) {
// TODO: implement writeValue
throw UnimplementedError();
Future<void> writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty) async {
var c = _getCharacteristic(deviceId, service, characteristic);

if (bleOutputProperty == BleOutputProperty.withResponse) {
await c.writeValue(value, type: BlueZGattCharacteristicWriteType.request);
} else {
await c.writeValue(value, type: BlueZGattCharacteristicWriteType.command);
}
_log('writeValue $characteristic, ${hex.encode(value)}');
}

@override
Expand Down