MAHAulia / device_imei

Flutter plugins to get real IMEI for Mobile Device
MIT License
4 stars 5 forks source link

After giving permission Imei number is not fetching in android version 10 and above #2

Open vrustipatel opened 1 year ago

vrustipatel commented 1 year ago

@override void initState() { super.initState(); // if (PreferenceManagerUtils.getIsImeiNumber() == "") { initPlatformState(); _setPlatformType(); checkPermissionAndAccessIMEI(); // } }

_setPlatformType() { if (Platform.isAndroid) { setState(() { type = 'Android'; }); } else if (Platform.isIOS) { setState(() { type = 'iOS'; }); } else { setState(() { type = 'other'; }); } }

void checkPermissionAndAccessIMEI() async { PermissionStatus status = await Permission.phone.status;

if (!status.isGranted) {
  status = await Permission.phone.request();
  if (!status.isGranted) {
    // Handle the case when the permission is denied by the user

    AppUtils.showErrorSnackBar("Give permission to access app");
    return;
  }
}

// The permission has been granted, you can now access the IMEI
// Your code to access the IMEI goes here

_getImei();

}

_getImei() async { PermissionStatus permission = await Permission.phone.status; printData("device imei 0", deviceImei??"Not found");

DeviceInfo? dInfo = await _deviceImeiPlugin.getDeviceInfo();

if (dInfo != null) {
  setState(() {
    deviceInfo = dInfo;
  });
}
printData("device imei 0", deviceImei??"Not found");

if (Platform.isAndroid) {
  printData("device imei 1", deviceImei??"Not found");

  if (permission.isGranted) {
    printData("device imei 2", deviceImei??"Not found");

    String? imei = await _deviceImeiPlugin.getDeviceImei();

    printData("device imei 3", deviceImei??"Not found");

    if (imei != null) {
      printData("device imei 33", deviceImei??"Not found");

      setState(() {
        getPermission = true;
        deviceImei = imei;
        printData("device imei ", deviceImei??"Not found");
         PreferenceManagerUtils.setIsImeiNumber(deviceImei??"");
      });
    }
  } else {
    PermissionStatus status = await Permission.phone.request();
    if (status == PermissionStatus.granted) {
      setState(() {
        getPermission = false;
      });
      _getImei();
    } else {
      setState(() {
        getPermission = false;
        message = "Permission not granted, please allow permission";
      });
    }
  }
} else {
  String? imei = await _deviceImeiPlugin.getDeviceImei();
  if (imei != null) {
    setState(() {
      getPermission = true;
      deviceImei = imei;

      printData("device imei ", deviceImei??"Not found");

      PreferenceManagerUtils.setIsImeiNumber(deviceImei??"");
    });
  }
}

}

// Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { String platformVersion; // Platform messages may fail, so we use a try/catch PlatformException. // We also handle the message potentially returning null. try { platformVersion = await _deviceImeiPlugin.getPlatformVersion() ?? 'Unknown platform version'; } on PlatformException { platformVersion = 'Failed to get platform version.'; }

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
  _platformVersion = platformVersion;
});

}

Here is my code and also I have given permission in manifest file

<uses-permission android:name="android.permission.READ_PHONE_STATE" />