How to detect huawei device models from other android models in flutter?

1,023

Solution 1

You can fetch device manufacture to detect Huawie device

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.manufacturer}');

plugin used - https://pub.dev/packages/device_info

Or
You can check the availability of GooglePlayServices in the device

GooglePlayServicesAvailability availability = await GoogleApiAvailability.instance.checkGooglePlayServicesAvailability();

Plugin required - https://pub.dev/packages/google_api_availability

Note: If using GooglePlayServices check, need to add platform check also as it returns false in case of iOS devices.

Solution 2

You could follow this Docs detect the Huawei devices by checking whether a device has HMS Core APK installed.

// Create an HmsApiAvailability instance
HmsApiAvailability client = new HmsApiAvailability();

// 0: HMS Core (APK) is available.
// 1: No HMS Core (APK) is found on device.
// 2: HMS Core (APK) installed is out of date.
// 3: HMS Core (APK) installed on the device is unavailable.
// 9: HMS Core (APK) installed on the device is not the official version.
// 21: The device is too old to support HMS Core (APK).
int status = await client.isHMSAvailable();

Solution 3

The method for judging whether HMS is available in the device is as follows:

HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context)

​Implement check if HMS are available.

public class HmsGmsUtil {
    private static final String TAG = "HmsGmsUtil";

    /**
     * Whether the HMS service on the device is available.
     *
     * @param context android context
     * @return true:HMS service is available; false:HMS service is not available;
     */
    public static boolean isHmsAvailable(Context context) {
        boolean isAvailable = false;
        if (null != context) {
            int result = HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context);
            isAvailable = (com.huawei.hms.api.ConnectionResult.SUCCESS == result);
        }
        Log.i(TAG, "isHmsAvailable: " + isAvailable);
        return isAvailable;
    }
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mHmsSignBtn = findViewById(R.id.hms_sign_btn);
    mHmsSignBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signInByHms();
        }   

    mGmsSignBtn = findViewById(R.id.gms_sign_btn);
    mGmsSignBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signInByGms();
        }
    });

    boolean isHmsAvailable = HmsGmsUtil.isHmsAvailable(this);

    Log.i(TAG, "isHmsAvailable: " + isHmsAvailable + ", isGmsAvailable: " + isGmsAvailable);

    if (isHmsAvailable && !isGmsAvailable) {
        // Only hms, Sign In by huawei account.
        mHmsSignBtn.setVisibility(View.VISIBLE);
    } else if (!isHmsAvailable && isGmsAvailable) {
        // Only gms, Sign In by google account.
        mGmsSignBtn.setVisibility(View.VISIBLE);
    } else if (isHmsAvailable && isGmsAvailable) {
        // both hsm and hms, decide by developer.
        mGmsSignBtn.setVisibility(View.VISIBLE);
        mHmsSignBtn.setVisibility(View.VISIBLE);
    } else if (!isHmsAvailable && !isGmsAvailable) {
        // neither hms and gms, decide by developer.
        mHmsSignBtn.setVisibility(View.VISIBLE);
    }
}
Share:
1,023
Muhamad Haydar Jawad
Author by

Muhamad Haydar Jawad

Updated on January 01, 2023

Comments

  • Muhamad Haydar Jawad
    Muhamad Haydar Jawad over 1 year

    As my application is cross platform, there is not any problem to encounter, but when it comes to android, some issues appear such that Huawei mobile devices don't have ability to access google play anymore, and they have their own application store (App Gallery), the problem comes as one of the specifications of my app is to force users to download the latest version, I don't know how to detect the Huawei devices to let users download my app from app gallery directly.