bluez.dart icon indicating copy to clipboard operation
bluez.dart copied to clipboard

adapter powered property change isn't emitted in propertiesChanged stream

Open magmast opened this issue 2 years ago • 2 comments

No matter if I'll power on/off the adapter through my system settings or using the setPowered method.

Here is some code that doesn't work, but should. The changes are not reflected in the app while running and the debugger doesn't trigger inside the where callback at line 43.

import 'dart:async';

import 'package:bluez/bluez.dart';
import 'package:flutter/material.dart';

void main() async {
  final client = BlueZClient();
  await client.connect();
  runApp(_App(adapter: client.adapters[0]));
}

class _App extends StatelessWidget {
  const _App({required this.adapter});

  final BlueZAdapter adapter;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bluez issue',
      home: _PoweredPropertyChangeIssue(adapter: adapter),
    );
  }
}

class _PoweredPropertyChangeIssue extends StatefulWidget {
  const _PoweredPropertyChangeIssue({required this.adapter});

  final BlueZAdapter adapter;

  @override
  State<StatefulWidget> createState() => _PoweredPropertyChangeIssueState();
}

class _PoweredPropertyChangeIssueState
    extends State<_PoweredPropertyChangeIssue> {
  late final StreamSubscription propertiesChangedSubscription;

  @override
  void initState() {
    super.initState();
    propertiesChangedSubscription = widget.adapter.propertiesChanged
        .where((properties) => properties.contains('powered'))
        .listen((_) => setState(() {}));
  }

  @override
  void dispose() {
    super.dispose();
    propertiesChangedSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Bluez issue'),
      ),
      body: Center(
        child: widget.adapter.powered
            ? ElevatedButton(
                child: const Text('Turn off'),
                onPressed: () => widget.adapter.setPowered(false),
              )
            : ElevatedButton(
                child: const Text('Turn on'),
                onPressed: () => widget.adapter.setPowered(true),
              ),
      ),
    );
  }
}

If it's important here are my specs:

OS: Fedora 35 DE: Gnome Network controller: MEDIATEK Corp. MT7921 802.11ax PCI Express Wireless Network Adapter (output from lspci)

magmast avatar Mar 03 '22 02:03 magmast

You need to change .where((properties) => properties.contains('powered')) to use the capitalized property name `"Powered".

robert-ancell avatar Mar 15 '22 01:03 robert-ancell

Unfortunately it's still not working.

I've changed it to Powered and added print statement print(properties) inside the where callback to see what are the properties names, but it's still not working.

propertiesChangedSubscription =
    widget.adapter.propertiesChanged.where((properties) {
  print(properties);
  return properties.contains('Powered');
}).listen((_) => setState(() {}));

This callback isn't event executed, because propertiesChanged isn't emitting anything.

I've tried using bluez.dart from git directly, but the situation is the same.

bluez:
  git: https://github.com/canonical/bluez.dart

I'll try today on different laptop to see, if it isn't problem with my hardware.

magmast avatar Mar 16 '22 17:03 magmast