system_state 1.2.8
system_state: ^1.2.8 copied to clipboard
A Flutter plugin for monitoring and controlling system states like battery, Wi-Fi, volume, and mobile data on Android.
SystemState Plugin #
SystemState is a Flutter plugin designed to provide access to essential device states and controls, currently supporting Android. The plugin allows you to monitor and control Battery, Volume, Wi-Fi, Mobile Data, and Bluetooth states. Future updates will extend functionality and support additional platforms.
Features #
- Battery State Monitoring: Retrieve and listen to battery level, temperature, and charging status.
- Volume Control: Get the current system volume, set a new volume level, and listen to volume changes.
- Wi-Fi State Monitoring and Control:
- Check if Wi-Fi is enabled or connected.
- Retrieve the name of the connected Wi-Fi network (
connectedWifiName). - Listen to Wi-Fi state changes.
- Mobile Data State Monitoring and Control:
- Check if mobile data is enabled.
- Retrieve the SIM operator name, network operator, and network type (e.g., 4G, 5G).
- Listen to mobile data state changes.
- Bluetooth State Monitoring and Control:
- Check if Bluetooth is enabled.
- Enable or disable Bluetooth programmatically (if permissions allow).
- Retrieve a list of paired and connected devices.
- Listen to Bluetooth state changes (on/off and device connect/disconnect).
Note: Currently,
SystemStateis Android-only. Platform checks ensure that unsupported platforms throw exceptions. Future versions will include support for iOS, Web, and more.
Installation #
Add the plugin to your pubspec.yaml:
dependencies:
system_state: ^1.3.0
Then run:
flutter pub get
Permissions #
Volume Control #
- To modify system volume:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Wi-Fi State #
- To read Wi-Fi and network state:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- To toggle Wi-Fi state:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
- To view connected Wi-Fi name (SSID), add the following permissions and ensure location services are enabled on the device:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Mobile Data State #
- To check network status and retrieve operator information:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Bluetooth State #
- To view and change Bluetooth state:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
- For Android 12+ to connect or get information about devices:
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
- To discover devices and get location-based Bluetooth info:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Usage #
Import the Package #
import 'package:system_state/system_state.dart';
Bluetooth Monitoring and Control #
Future<void> getBluetoothState() async {
try {
final state = await SystemState.bluetooth.getBluetoothState();
print("Bluetooth Enabled: ${state.isBluetoothEnabled}");
print("Connected Devices: ${state.connectedDevices.map((d) => d.name)}");
} catch (e) {
print("Error fetching Bluetooth state: $e");
}
}
void listenBluetoothState() {
SystemState.bluetooth.listen((state) {
print("Bluetooth Enabled: ${state.isBluetoothEnabled}");
print("Connected Devices: ${state.connectedDevices.map((d) => d.name)}");
});
}
Mobile Data, Battery, Volume, and Wi-Fi Features #
Refer to the respective sections in the original documentation.
API Reference #
BluetoothController.getBluetoothState(): Retrieves current Bluetooth status and connected devices.BluetoothController.setBluetoothEnabled(bool enabled): Enable or disable Bluetooth.BluetoothController.listen(void Function(BluetoothState state) callback): Listen for Bluetooth state changes.MobileData,Battery,Volume, andWi-Fimethods remain unchanged.
Bluetooth Classes #
class BluetoothState {
final bool isBluetoothEnabled;
final List<BluetoothDevice> connectedDevices;
}
class BluetoothDevice {
final String name;
final String address;
}
Platform Support #
| Platform | Battery | Volume | Wi-Fi | Mobile Data | Bluetooth |
|---|---|---|---|---|---|
| Android | ✅ | ✅ | ✅ | ✅ | ✅ |
| iOS | ❌ | ❌ | ❌ | ❌ | ❌ |
| Web | ❌ | ❌ | ❌ | ❌ | ❌ |
Platform checks ensure the plugin throws an exception if accessed on non-Android platforms. Future versions will aim to support more platforms, including iOS and Web.
Future Development #
- Network Controller: Manage Airplane Mode and Bluetooth.
- Cross-Platform Support: iOS using CoreBluetooth, Web using Web Bluetooth API.
- Device Discovery: Optional scanning for nearby Bluetooth devices.
Contributing #
We welcome contributions! Submit issues or pull requests for new features or bug fixes.
License #
Licensed under the MIT License.