androidiosflutterdartwakelock

Combining Battery and Wakelock does not give desired outcome


Currently I'm trying to build an app that does the following:

1) When connected to power do not allow device to go to sleep 2) When disconnected from power allow the device to go to sleep

Currently I'm using the Battery plugin to detect whether the device is charing or not.

https://pub.dev/packages/battery

And the wakelock Package i'm using is.

https://pub.dev/packages/wakelock

The code for this is:

class _MyHomePageState extends State<MyHomePage> {
  Battery _battery = Battery();
  bool wakeLockEnabled;
  BatteryState _batteryState;
  StreamSubscription<BatteryState> _batteryStateSubscription;
  Color bgColor = Colors.white;

  @override
  void initState() {
    super.initState();
    _batteryStateSubscription =
        _battery.onBatteryStateChanged.listen((BatteryState state) {
      setState(() {
        _batteryState = state;
        _shouldWakelock();
      });
    });
  }

  void _shouldWakelock() async {
    wakeLockEnabled = await Wakelock.isEnabled;
    if (_batteryState == BatteryState.charging) {
      if (!wakeLockEnabled) {
        setState(() {
          Wakelock.enable();
          bgColor = Colors.green;
        });
      }
    } else if (_batteryState == BatteryState.discharging) {
      if (wakeLockEnabled) {
        setState(() {
          Wakelock.disable();
          bgColor = Colors.red;
        });
      }
    } else if (_batteryState == BatteryState.full) {
      setState(() {
        Wakelock.disable();
        bgColor = Colors.yellow;
      });
    }
    wakeLockEnabled = await Wakelock.isEnabled;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('$_batteryState'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 100,
              height: 100,
              color: bgColor,
            ),
            Text('$wakeLockEnabled'),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    if (_batteryStateSubscription != null) {
      _batteryStateSubscription.cancel();
    }
  }
}

Currently the following tests give the following results (this is all using an iPhone 7)

I made a new project where I left all the boiler plate and just added this to the _incrementCounter() function. (I made it async as well. Here Whenever I press a button to switch between enable and disable the wakelock it works just fine, its only when having wakelock respond to the battery changes it does not work for some reason.

var isEnabled = await Wakelock.isEnabled;

if (isEnabled) {
  await Wakelock.disable();
} else {
  await Wakelock.enable();
}

I hope someone knows what may be causing the odd behaviour.


Solution

  • FWIW: I figured this only happens when debugging. Meaning that when code was being built for release the issue did not occur.