flutter_mobile_red_type 0.0.1 copy "flutter_mobile_red_type: ^0.0.1" to clipboard
flutter_mobile_red_type: ^0.0.1 copied to clipboard

PlatformAndroid

Flutter plugin to detect mobile network type (2G, 3G, 4G, 5G) natively on Android.

flutter_mobile_red_type #

Flutter plugin to detect mobile network type (2G, 3G, 4G, 5G) natively on Android.

Features #

  • ✅ Mobile network type detection (2G, 3G, 4G, 5G)
  • ✅ Native Android implementation using Kotlin
  • ✅ Support for multiple Android versions (API 21+)
  • ✅ Permission and error handling
  • ✅ Simple and easy to use API

Platforms #

  • ✅ Android (native)
  • ❌ iOS (currently not supported)

Installation #

Add this dependency to your pubspec.yaml file:

dependencies:
  flutter_mobile_red_type: ^0.0.1

Then run:

flutter pub get

Permissions #

The plugin requires the following permissions on Android. They will be added automatically when using the plugin:

  • ACCESS_NETWORK_STATE: To access network state
  • READ_PHONE_STATE: To read mobile network type (Android < 13)
  • READ_BASIC_PHONE_STATE: To read mobile network type (Android 13+)

Note: These permissions are automatically declared in the plugin's AndroidManifest.xml, but you also need to request them at runtime if your app targets Android 6.0+ (API 23+). To request permissions at runtime, you can use the permission_handler package:

dependencies:
  flutter_mobile_red_type: ^0.0.1
  permission_handler: ^11.4.0

Usage #

Basic example #

import 'package:flutter_mobile_red_type/flutter_mobile_red_type.dart';

// Get network type as enum
final networkType = await FlutterMobileRedType.getNetworkType();

switch (networkType) {
  case NetworkType.twoG:
    print('Connected to 2G network');
    break;
  case NetworkType.threeG:
    print('Connected to 3G network');
    break;
  case NetworkType.fourG:
    print('Connected to 4G network');
    break;
  case NetworkType.fiveG:
    print('Connected to 5G network');
    break;
  case NetworkType.unknown:
    print('Unknown network type');
    break;
  case NetworkType.permissionDenied:
    print('Permissions denied');
    break;
}

Get as String #

// Get network type as String
final networkTypeString = await FlutterMobileRedType.getNetworkTypeString();
print('Network type: $networkTypeString'); // "2G", "3G", "4G", "5G", etc.

Complete example with error handling #

import 'package:flutter_mobile_red_type/flutter_mobile_red_type.dart';

Future<void> checkNetworkType() async {
  try {
    final networkType = await FlutterMobileRedType.getNetworkType();
    
    if (networkType == NetworkType.permissionDenied) {
      print('Please grant the necessary permissions');
      return;
    }
    
    if (networkType == NetworkType.unknown) {
      print('Could not determine network type');
      return;
    }
    
    print('Current network type: ${networkType.value}');
    
    // Perform actions based on network type
    switch (networkType) {
      case NetworkType.twoG:
      case NetworkType.threeG:
        // Slow network, use image compression
        break;
      case NetworkType.fourG:
      case NetworkType.fiveG:
        // Fast network, load high quality content
        break;
      default:
        break;
    }
  } catch (e) {
    print('Error getting network type: $e');
  }
}

Example with UI and permission handling #

Complete example of a widget that displays network type with permission handling:

import 'package:flutter/material.dart';
import 'package:flutter_mobile_red_type/flutter_mobile_red_type.dart';
import 'package:permission_handler/permission_handler.dart';

class NetworkTypeWidget extends StatefulWidget {
  const NetworkTypeWidget({super.key});

  @override
  State<NetworkTypeWidget> createState() => _NetworkTypeWidgetState();
}

class _NetworkTypeWidgetState extends State<NetworkTypeWidget> {
  NetworkType? _networkType;
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
    _checkPermissionsAndGetNetworkType();
  }

  Future<void> _checkPermissionsAndGetNetworkType() async {
    // Check permissions first
    final phoneStatus = await Permission.phone.status;
    
    if (!phoneStatus.isGranted) {
      // Request permissions
      final result = await Permission.phone.request();
      if (!result.isGranted) {
        setState(() {
          _networkType = NetworkType.permissionDenied;
        });
        return;
      }
    }

    // Get network type
    setState(() {
      _isLoading = true;
    });

    try {
      final networkType = await FlutterMobileRedType.getNetworkType();
      setState(() {
        _networkType = networkType;
        _isLoading = false;
      });
    } catch (e) {
      setState(() {
        _networkType = NetworkType.unknown;
        _isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      return const CircularProgressIndicator();
    }

    if (_networkType == null) {
      return const Text('Loading...');
    }

    if (_networkType == NetworkType.permissionDenied) {
      return Column(
        children: [
          const Text('Permissions denied'),
          ElevatedButton(
            onPressed: _checkPermissionsAndGetNetworkType,
            child: const Text('Request permissions'),
          ),
        ],
      );
    }

    return Column(
      children: [
        Text(
          'Network type: ${_networkType!.value}',
          style: const TextStyle(fontSize: 24),
        ),
        ElevatedButton(
          onPressed: _checkPermissionsAndGetNetworkType,
          child: const Text('Refresh'),
        ),
      ],
    );
  }
}

API #

FlutterMobileRedType class #

Static methods

  • Future<NetworkType> getNetworkType(): Gets the mobile network type as enum.
  • Future<String> getNetworkTypeString(): Gets the mobile network type as String.

NetworkType enum #

Possible values:

  • NetworkType.twoG: 2G network (GPRS, EDGE)
  • NetworkType.threeG: 3G network (UMTS, HSPA, etc.)
  • NetworkType.fourG: 4G network (LTE)
  • NetworkType.fiveG: 5G network (NR)
  • NetworkType.unknown: Unknown or unavailable network type
  • NetworkType.permissionDenied: Permissions denied

Properties

  • String value: Gets the value as String ('2G', '3G', '4G', '5G', etc.)

Methods

  • static NetworkType fromString(String value): Converts a String to NetworkType.

Requirements #

  • Flutter SDK >= 1.17.0
  • Dart SDK ^3.8.0
  • Android minSdkVersion: 21 (Android 5.0)

Tested Devices #

This plugin has been tested and verified on the following devices:

  • POS KOZEN P8 Neo - Working correctly with accurate detection of 2G, 3G, 4G and 5G

If you test the plugin on other devices and it works correctly, consider adding your device to this list via a pull request.

Limitations #

  • Only works on Android devices
  • Requires network and phone permissions
  • Detection may vary depending on the device and Android version
  • 5G detection may not be available on some devices

Troubleshooting #

Permissions denied #

If you receive NetworkType.permissionDenied, make sure your application has the necessary permissions. You can request them manually:

import 'package:permission_handler/permission_handler.dart';

// Request permissions
await Permission.phone.request();

Unknown network type #

If you receive NetworkType.unknown, it may be due to:

  • The device does not have an active mobile data connection
  • The Android version does not support detection
  • The device is in airplane mode

Contributing #

Contributions are welcome. Please open an issue or pull request if you find any problems or have suggestions.

License #

See the LICENSE file for more information.

0
likes
160
points
88
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter plugin to detect mobile network type (2G, 3G, 4G, 5G) natively on Android.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_mobile_red_type

Packages that implement flutter_mobile_red_type