all_device_sensors 1.0.1
all_device_sensors: ^1.0.1 copied to clipboard
A Flutter plugin to access all device sensors — accelerometer, gyroscope, magnetometer, linear acceleration, gravity, barometer, proximity, light and step counter. By Bharathwaj.
all_device_sensors #
A comprehensive Flutter plugin to access all device sensors including accelerometer, gyroscope, and magnetometer with real-time streaming support.
Author: Bharathwaj
Features #
✅ Get list of all available sensors on the device
✅ Real-time accelerometer data streaming
✅ Real-time gyroscope data streaming
✅ Real-time magnetometer data streaming
✅ Cross-platform support (Android & iOS)
✅ Detailed sensor information (name, vendor, type, power, resolution)
Supported Sensors #
- Accelerometer - Measures device acceleration
- Gyroscope - Measures device rotation rate
- Magnetometer - Measures magnetic field
- And all other sensors available on the device
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
all_device_sensors: ^1.0.0
Then run:
flutter pub get
Platform Setup #
iOS #
Add the following to your Info.plist file:
<key>NSMotionUsageDescription</key>
<string>This app needs access to motion sensors</string>
Android #
No additional setup required. The plugin automatically handles sensor access.
Usage #
Import the package #
import 'package:all_device_sensors/all_device_sensors.dart';
Get Platform Version #
final allDeviceSensors = AllDeviceSensors();
String? version = await allDeviceSensors.getPlatformVersion();
print('Platform: $version');
Get All Available Sensors #
final allDeviceSensors = AllDeviceSensors();
List<Map<String, dynamic>>? sensors = await allDeviceSensors.getSensors();
sensors?.forEach((sensor) {
print('Name: ${sensor['name']}');
print('Vendor: ${sensor['vendor']}');
print('Type: ${sensor['type']}');
print('Power: ${sensor['power']} mA');
print('Resolution: ${sensor['resolution']}');
});
Stream Accelerometer Data #
final allDeviceSensors = AllDeviceSensors();
StreamSubscription? subscription = allDeviceSensors.accelerometerStream.listen((data) {
print('Accelerometer - X: ${data['x']}, Y: ${data['y']}, Z: ${data['z']}');
});
// Don't forget to cancel the subscription
subscription?.cancel();
Stream Gyroscope Data #
final allDeviceSensors = AllDeviceSensors();
StreamSubscription? subscription = allDeviceSensors.gyroscopeStream.listen((data) {
print('Gyroscope - X: ${data['x']}, Y: ${data['y']}, Z: ${data['z']}');
});
// Don't forget to cancel the subscription
subscription?.cancel();
Stream Magnetometer Data #
final allDeviceSensors = AllDeviceSensors();
StreamSubscription? subscription = allDeviceSensors.magnetometerStream.listen((data) {
print('Magnetometer - X: ${data['x']}, Y: ${data['y']}, Z: ${data['z']}');
});
// Don't forget to cancel the subscription
subscription?.cancel();
Complete Example #
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:all_device_sensors/all_device_sensors.dart';
class SensorPage extends StatefulWidget {
@override
State<SensorPage> createState() => _SensorPageState();
}
class _SensorPageState extends State<SensorPage> {
final _plugin = AllDeviceSensors();
Map<String, dynamic> _accelerometerData = {};
StreamSubscription? _subscription;
@override
void initState() {
super.initState();
_subscription = _plugin.accelerometerStream.listen((data) {
setState(() {
_accelerometerData = data;
});
});
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Sensors')),
body: Center(
child: Text(
'X: ${_accelerometerData['x']?.toStringAsFixed(2)}\n'
'Y: ${_accelerometerData['y']?.toStringAsFixed(2)}\n'
'Z: ${_accelerometerData['z']?.toStringAsFixed(2)}',
),
),
);
}
}
Sensor Data Format #
All sensor streams return data in the following format:
{
'x': double, // X-axis value
'y': double, // Y-axis value
'z': double, // Z-axis value
'timestamp': int // Timestamp in milliseconds
}
API Reference #
Methods #
| Method | Return Type | Description |
|---|---|---|
getPlatformVersion() |
Future<String?> |
Returns the platform version |
getSensors() |
Future<List<Map<String, dynamic>>?> |
Returns list of all available sensors |
Streams #
| Stream | Return Type | Description |
|---|---|---|
accelerometerStream |
Stream<Map<String, dynamic>> |
Real-time accelerometer data |
gyroscopeStream |
Stream<Map<String, dynamic>> |
Real-time gyroscope data |
magnetometerStream |
Stream<Map<String, dynamic>> |
Real-time magnetometer data |
Sensor Information Fields #
When calling getSensors(), each sensor contains:
name- Sensor namevendor- Sensor manufacturertype- Sensor type codeversion- Sensor versionpower- Power consumption in mAresolution- Sensor resolutionmaxRange- Maximum sensor range
Performance Tips #
- Cancel subscriptions when not needed to save battery
- Use appropriate update intervals based on your needs
- Handle sensor unavailability gracefully
- Test on real devices as emulators may not have all sensors
Troubleshooting #
iOS: Sensors not working #
- Ensure
NSMotionUsageDescriptionis added toInfo.plist - Test on a real device (simulator has limited sensor support)
Android: No sensor data #
- Check device has the required sensors
- Ensure app has proper permissions
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Author #
Bharathwaj
Changelog #
See CHANGELOG.md for version history.
Support #
For issues, feature requests, or questions, please file an issue on the GitHub repository.