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.
And the wakelock Package i'm using is.
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)
The device is connected to the Mac and the app is launched through IntelliJ, once app has fully launched device does not go to sleep, device is then disconnected while app is running and after about 30 seconds device goes to sleep.
The device is connected to power and the app is launched from the devices screen, the device never goes to sleep, when disconnecting from power the device still does not go to sleep.
The app is launched through the device itself, the device is not connected to power, the device never goes to sleep.
The app is launched through the device itself, the device is not connected to power, the device does not go to sleep, when plugging in a power cable then removing it again after like 10 seconds the wakelock is still not activated and still never goes to sleep.
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.
FWIW: I figured this only happens when debugging. Meaning that when code was being built for release the issue did not occur.