Skip to content

Type Mismatch in getWiFiAPState Causes IllegalArgumentException #418

@himanshubora99

Description

@himanshubora99

Description

The getWiFiAPState method in WifiIotPlugin.java returns a WIFI_AP_STATE enum for Android API levels >= 29 (Android Q), but the corresponding Dart method in wifi_iot.dart expects an int? value. This mismatch causes an IllegalArgumentException because Flutter's StandardMessageCodec cannot serialize the WIFI_AP_STATE enum type, which is not a supported data type (e.g., integers, strings, lists, maps).

The issue occurs specifically when getWiFiAPState is called on devices running Android 10 (API 29) or higher, as the method attempts to send the localOnlyHotspotState (a WIFI_AP_STATE enum) directly to the Flutter side.

Steps to Reproduce

Use the wifi_iot plugin (version 0.3.19+2 or similar) in a Flutter project.
Call WiFiForIoTPlugin.getWiFiAPState() on a device/emulator running Android 10 (API 29) or higher.
Observe the crash with the following error in the logcat:

Error Log

E/MethodChannel#wifi_iot(22414): Failed to handle method call
E/MethodChannel#wifi_iot(22414): java.lang.IllegalArgumentException: Unsupported value: 'WIFI_AP_STATE_DISABLED' of type 'class info.whitebyte.hotspotmanager.WIFI_AP_STATE'
E/MethodChannel#wifi_iot(22414): at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:297)
E/MethodChannel#wifi_iot(22414): at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:61)
E/MethodChannel#wifi_iot(22414): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:272)
E/MethodChannel#wifi_iot(22414): at com.alternadom.wifiiot.WifiIotPlugin.getWiFiAPState(WifiIotPlugin.java:650)
E/MethodChannel#wifi_iot(22414): at com.alternadom.wifiiot.WifiIotPlugin.onMethodCall(WifiIotPlugin.java:284)
E/MethodChannel#wifi_iot(22414): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/MethodChannel#wifi_iot(22414): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/MethodChannel#wifi_iot(22414): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#wifi_iot(22414): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
E/MethodChannel#wifi_iot(22414): at android.os.Handler.handleCallback(Handler.java:958)
E/MethodChannel#wifi_iot(22414): at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#wifi_iot(22414): at android.os.Looper.loopOnce(Looper.java:222)
E/MethodChannel#wifi_iot(22414): at android.os.Looper.loop(Looper.java:314)
E/MethodChannel#wifi_iot(22414): at android.app.ActivityThread.main(ActivityThread.java:8706)
E/MethodChannel#wifi_iot(22414): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#wifi_iot(22414): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:565)
E/MethodChannel#wifi_iot(22414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)

Affected Code

WifiIotPlugin.java (Line 650)

/** Gets the Wi-Fi enabled state. *** getWifiApState : return {link WIFI_AP_STATE} */

private void getWiFiAPState(Result poResult) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        poResult.success(moWiFiAPManager.getWifiApState().ordinal());
    } else {
        poResult.success(localOnlyHotspotState); // Causes IllegalArgumentException
    }
}

wifi_iot.dart

static Future<int?> getWiFiAPState() async {
    final Map<String, String> htArguments = Map();
    int? iResult;
    try {
        iResult = await _channel.invokeMethod('getWiFiAPState', htArguments);
    } on MissingPluginException catch (e) {
        print("MissingPluginException : ${e.toString()}");
    }
    return iResult;
}

Proposed Fix

Modify the getWiFiAPState method in WifiIotPlugin.java to return the ordinal of localOnlyHotspotState for API >= 29, ensuring consistency with the pre-API 29 behavior and the Dart side's expectation:

/** Gets the Wi-Fi enabled state. *** getWifiApState : return {link int} */

private void getWiFiAPState(Result poResult) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        poResult.success(moWiFiAPManager.getWifiApState().ordinal());
    } else {
        poResult.success(localOnlyHotspotState.ordinal()); // Return ordinal instead of enum
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions