imei_getter
A Flutter plugin to get device identifiers with proper permission handling and graceful fallbacks for modern Android and iOS restrictions.
Features
- Android (API < 29): Reads IMEI number when
READ_PHONE_STATEpermission is granted - Android (API 29+): Falls back to
Settings.Secure.ANDROID_ID(no permission required) - iOS: Returns
identifierForVendor(stable per vendor, resets on app reinstall) - Permission Handling: Built-in methods to check and request permissions
- Privacy-First: Handles restrictions gracefully and provides clear guidance
Installation
Add this to your package's pubspec.yaml file:
dependencies:
imei_getter: ^0.2.0
Then run:
flutter pub get
Platform Setup
Android
The plugin automatically handles permissions. For Android 9 and below, add the following to your android/app/src/main/AndroidManifest.xml:
<manifest>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- ... other permissions ... -->
</manifest>
Note: On Android 10 (API 29) and above, IMEI access is restricted even with permission. The plugin automatically falls back to ANDROID_ID.
iOS
No additional setup required. The plugin uses identifierForVendor which is available by default.
Usage
Basic Example
import 'package:imei_getter/imei_getter.dart';
// Get device identifier
final identifier = await ImeiGetter.getDeviceIdentifier();
print('Device ID: $identifier');
With Permission Handling (Android)
import 'package:imei_getter/imei_getter.dart';
Future<String?> getDeviceId() async {
// Check if permission is granted (Android only)
final hasPermission = await ImeiGetter.hasPermission();
if (!hasPermission) {
// Request permission
await ImeiGetter.requestPermission();
// Check again after request
final granted = await ImeiGetter.hasPermission();
if (!granted) {
print('Permission denied. Using fallback identifier.');
}
}
// Get identifier (will use fallback on Android 10+ or if permission denied)
return await ImeiGetter.getDeviceIdentifier();
}
Complete Example
See the example/ directory for a complete Flutter app demonstrating:
- Permission requests
- Error handling
- Display of device identifier
- Platform-specific behavior
API Reference
Future<String?> getDeviceIdentifier()
Returns the device identifier:
- Android (API < 29): IMEI if permission granted, otherwise
ANDROID_ID - Android (API 29+):
ANDROID_ID(IMEI not accessible) - iOS:
identifierForVendor.uuidString
Returns null if identifier cannot be retrieved.
Future<bool> hasPermission()
Checks if READ_PHONE_STATE permission is granted (Android only).
- Returns
trueon iOS (no permission needed) - Returns
trueon Android 10+ (usesANDROID_ID, no permission needed) - Returns
true/falseon Android < 10 based on permission status
Future<void> requestPermission()
Requests READ_PHONE_STATE permission (Android only).
- No-op on iOS
- No-op on Android 10+ (uses
ANDROID_ID, no permission needed) - Shows permission dialog on Android < 10
Limitations & Privacy Considerations
Android
- Android 10 (API 29) and above: IMEI access is restricted by the system, even with
READ_PHONE_STATEpermission. The plugin automatically usesANDROID_IDas a fallback. - Android 9 and below: IMEI can be accessed with proper permission.
- Play Store Policy: Google Play Store has strict policies regarding IMEI collection. Ensure your use case complies with their policies.
iOS
- IMEI Not Available: iOS does not provide IMEI access to apps.
- identifierForVendor: Returns a stable identifier per vendor (app developer). This identifier:
- Resets when all apps from the vendor are uninstalled
- Resets on device restore
- Is unique per device-vendor combination
Privacy Best Practices
- Only request IMEI when necessary: Consider if
ANDROID_IDoridentifierForVendormeets your needs. - Inform users: Clearly explain why you need device identifiers in your privacy policy.
- Handle gracefully: Always handle
nullreturns and permission denials. - Comply with regulations: Ensure compliance with GDPR, CCPA, and other privacy regulations.
- Play Store compliance: Review Google Play Store policies before collecting IMEI.
Testing
Run the example app:
cd example
flutter run
Run unit tests:
flutter test
License
See LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.