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 1 commit
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
Next Next commit
Notify for availability changes on Linux
  • Loading branch information
Rohit Sangwan authored and Rohit Sangwan committed Jul 9, 2022
commit 8105b384511a5af70704f490845dd27bc9ceb314
23 changes: 11 additions & 12 deletions packages/quick_blue/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
if (Platform.isIOS || Platform.isMacOS) {
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 @@ -57,13 +57,12 @@ class _MyAppState extends State<MyApp> {
),
body: Column(
children: [
if (Platform.isIOS || Platform.isMacOS)
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
8 changes: 1 addition & 7 deletions packages/quick_blue/lib/quick_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ class QuickBlue {
static Future<bool> isBluetoothAvailable() =>
_platform.isBluetoothAvailable();

static Stream<AvailabilityState> get availabilityChangeStream {
if (!Platform.isIOS && !Platform.isMacOS) {
throw UnimplementedError('setAvailabilityHandler is only implemented on iOS and macOS');
}
return _platform.availabilityChangeStream
.map((availabilityStateValue) => AvailabilityState.parse(availabilityStateValue));
}
static Stream<AvailabilityState> get availabilityChangeStream =>_platform.availabilityChangeStream.map((availabilityStateValue) => AvailabilityState.parse(availabilityStateValue));
rohitsangwan01 marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
29 changes: 26 additions & 3 deletions packages/quick_blue/lib/src/quick_blue_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class QuickBlueLinux extends QuickBluePlatform {
}

bool isInitialized = false;
AvailabilityState _oldAvailabilityState = AvailabilityState.unknown;
StreamSubscription? _availabilityStreamSubscription;

final BlueZClient _client = BlueZClient();

Expand All @@ -33,6 +35,18 @@ class QuickBlueLinux extends QuickBluePlatform {
}
_client.deviceAdded.listen(_onDeviceAdd);

_availabilityStateController.onListen = () {
_availabilityStreamSubscription = _activeAdapter?.propertiesChanged.listen((event) {
if(_oldAvailabilityState == _getAvailabilityState)return;
_oldAvailabilityState = _getAvailabilityState;
_availabilityStateController.add(_getAvailabilityState);
});
};

_availabilityStateController.onCancel = () {
_availabilityStreamSubscription?.cancel();
};

isInitialized = true;
}
}
Expand All @@ -56,9 +70,18 @@ class QuickBlueLinux extends QuickBluePlatform {
return _activeAdapter != null;
}

// 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 _getAvailabilityState {
rohitsangwan01 marked this conversation as resolved.
Show resolved Hide resolved
if (_activeAdapter == null) return AvailabilityState.unsupported;
return _activeAdapter!.powered
? AvailabilityState.poweredOn
: AvailabilityState.poweredOff;
}

@override
Future<void> startScan() async {
Expand Down Expand Up @@ -149,7 +172,7 @@ class QuickBlueLinux extends QuickBluePlatform {
@override
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) {
Expand Down