0

I am working on an App where I need to connect to IOT Device and this IOT device make a hotspot for communication. I am facing an issue in Android 9(Pie) that if mobile data is on then request send to Mobile data instead of the Wifi and it is working good in lower versions. So how to force mobile to send request to wifi instead of the mobile in Andoird 9

1 Answers1

1

You can use this method through reflection to know if the user has data enabled on mobile.

private static boolean isMobileDataEnabled(Context context) {
  boolean enabled = false;
  ConnectivityManager cm =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  try {
    Class cmClass = Class.forName(cm.getClass().getName());
    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
    method.setAccessible(true);
    enabled = (Boolean) method.invoke(cm);
  } catch (Exception e) {
    Log.e("DANG", e.toString());
  }
  return enabled;
}

If yes, then you should ask the user to disable the data and turn on wifi. Make that dialog non-dismissable unless user turns off the mobile data.

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
Adeel Zafar
  • 361
  • 2
  • 16