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

On availability change linux #53

Merged
merged 8 commits into from
Jul 11, 2022
Merged
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
12 changes: 6 additions & 6 deletions packages/quick_blue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ A cross-platform (Android/iOS/macOS/Windows/Linux) BluetoothLE plugin for Flutte

| API | Android | iOS | macOS | Windows | Linux |
| :--- | :---: | :---: | :---: | :---: | :---: |
| availabilityChangeStream | | ✔️ | ✔️ | | |
| availabilityChangeStream | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| isBluetoothAvailable | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| startScan/stopScan | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| connect/disconnect | ✔️ | ✔️ | ✔️ | ✔️ | |
| discoverServices | ✔️ | ✔️ | ✔️ | ✔️ | |
| setNotifiable | ✔️ | ✔️ | ✔️ | ✔️ | |
| readValue | ✔️ | ✔️ | ✔️ | ✔️ | |
| writeValue | ✔️ | ✔️ | ✔️ | ✔️ | |
| connect/disconnect | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| discoverServices | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| setNotifiable | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| readValue | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| writeValue | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| requestMtu | ✔️ | ✔️ | ✔️ | ✔️ | |

> * Windows' APIs are little different on `discoverServices`: https://github.com/woodemi/quick_blue/issues/76
Expand Down
24 changes: 11 additions & 13 deletions packages/quick_blue/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
StreamSubscription<BlueScanResult>? _scanResultSubscription;
StreamSubscription<AvailabilityState>? _availabilitySubscription;
bool isAvailabilityChangeStreamSupported = !Platform.isLinux;

@override
void initState() {
super.initState();
if (isAvailabilityChangeStreamSupported) {
QuickBlue.availabilityChangeStream.listen((state) {
debugPrint('Bluetooth state: ${state.toString()}');
});
}

QuickBlue.availabilityChangeStream.listen((state) {
debugPrint('Bluetooth state: ${state.toString()}');
});

if (kDebugMode) {
QuickBlue.setLogger(Logger('quick_blue_example'));
}
Expand All @@ -58,13 +57,12 @@ class _MyAppState extends State<MyApp> {
),
body: Column(
children: [
if (isAvailabilityChangeStreamSupported)
StreamBuilder<AvailabilityState>(
stream: QuickBlue.availabilityChangeStream,
builder: (context, snapshot) {
return Text('Bluetooth state: ${snapshot.data?.toString()}');
},
),
StreamBuilder<AvailabilityState>(
stream: QuickBlue.availabilityChangeStream,
builder: (context, snapshot) {
return Text('Bluetooth state: ${snapshot.data?.toString()}');
},
),
FutureBuilder(
future: QuickBlue.isBluetoothAvailable(),
builder: (context, snapshot) {
Expand Down
9 changes: 1 addition & 8 deletions packages/quick_blue/lib/quick_blue.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'src/quick_blue_platform_interface.dart';
Expand All @@ -23,13 +22,7 @@ class QuickBlue {
static Future<bool> isBluetoothAvailable() =>
_platform.isBluetoothAvailable();

static Stream<AvailabilityState> get availabilityChangeStream {
if (Platform.isLinux) {
throw UnimplementedError('setAvailabilityHandler not implemented on this platform');
}
return _platform.availabilityChangeStream
.map((availabilityStateValue) => AvailabilityState.parse(availabilityStateValue));
}
static Stream<AvailabilityState> get availabilityChangeStream => _platform.availabilityChangeStream.map(AvailabilityState.parse);

static Future<void> startScan() => _platform.startScan();

Expand Down
25 changes: 18 additions & 7 deletions packages/quick_blue/lib/src/quick_blue_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class QuickBlueLinux extends QuickBluePlatform {
}
_client.deviceAdded.listen(_onDeviceAdd);

_activeAdapter?.propertiesChanged.listen((List<String> properties) {
if (properties.contains('Powered')) {
_availabilityStateController.add(availabilityState);
}
});
_availabilityStateController.add(availabilityState);
isInitialized = true;
}
}
Expand All @@ -53,13 +59,18 @@ class QuickBlueLinux extends QuickBluePlatform {
Future<bool> isBluetoothAvailable() async {
await _ensureInitialized();
_log('isBluetoothAvailable invoke success');

return _activeAdapter != null;
return _activeAdapter!.powered;
}

// FIXME Close
final StreamController<AvailabilityState> _availabilityStateController = StreamController.broadcast();

@override
// TODO: implement availabilityChangeStream
Stream<int> get availabilityChangeStream => throw UnimplementedError();
Stream<int> get availabilityChangeStream =>_availabilityStateController.stream.map((state) => state.value);

AvailabilityState get availabilityState {
return _activeAdapter!.powered ? AvailabilityState.poweredOn : AvailabilityState.poweredOff;
}

@override
Future<void> startScan() async {
Expand Down Expand Up @@ -145,7 +156,7 @@ class QuickBlueLinux extends QuickBluePlatform {
return c;
}

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

@override
Future<void> setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async {
Expand All @@ -159,10 +170,10 @@ class QuickBlueLinux extends QuickBluePlatform {
onValueChanged?.call(deviceId, characteristic, Uint8List.fromList(c.value));
}
}
_characteristicPropertiesSubcriptions[characteristic] ??= c.propertiesChanged.listen(onPropertiesChanged);
_characteristicPropertiesSubscriptions[characteristic] ??= c.propertiesChanged.listen(onPropertiesChanged);
} else {
c.stopNotify();
_characteristicPropertiesSubcriptions.remove(characteristic)?.cancel();
_characteristicPropertiesSubscriptions.remove(characteristic)?.cancel();
}
}

Expand Down