I've got a flutter app in which I want to start the hotspot of the device. So I have to write some platform specific code on android to use hotspot.
I got this that compiles without a problem:
try {
if (call.method == "startLocalOnlyHotspot") {
var wifiManager: WifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
override fun onStarted(localOnlyHotspotReservation: WifiManager.LocalOnlyHotspotReservation) {
super.onStarted(localOnlyHotspotReservation)
result.success(localOnlyHotspotReservation)
}
override fun onStopped() {
super.onStopped()
result.success(null)
}
override fun onFailed(reason: Int) {
super.onFailed(reason)
result.success(null)
}
}, Handler())
// log the results
Log.d("Hotspot", "Hotspot started")
// result.success(ApManager.configApState(this))
} else {
result.notImplemented()
}
} catch (e: Exception) {
result.error("ERROR", e.message, null)
}
But when I call this code from flutter with this:
await platform.invokeMethod('startLocalOnlyHotspot');
the app crashes with I think this main error:
java.lang.IllegalArgumentException: Unsupported value: 'android.net.wifi.WifiManager$LocalOnlyHotspotReservation@f603f75' of type 'class android.net.wifi.WifiManager$LocalOnlyHotspotReservation'
But if it's not that, I put the full error stack here :
W/WifiManager(28567): com.kwikwink.kw_delivery attempted call to setWifiApEnabled: enabled = false D/Hotspot (28567): Hotspot started D/WifiManager(28567): LocalOnlyHotspotCallbackProxy: handle message what: 0 msg: { when=0 what=0 obj=* ID: -2 SSID: AndroidShare_2246 PROVIDER-NAME: null BSSID: null FQDN: null PRIO: 0 HIDDEN: false PMF: false D/WifiManager(28567): NetworkSelectionStatus NETWORK_SELECTION_ENABLED D/WifiManager(28567): hasEverConnected: false D/WifiManager(28567): KeyMgmt: WPA2_PSK Protocols: D/WifiManager(28567): AuthAlgorithms: D/WifiManager(28567): PairwiseCiphers: D/WifiManager(28567): GroupCiphers: D/WifiManager(28567): PSK: * D/WifiManager(28567): Enterprise config: D/WifiManager(28567): eap NULL D/WifiManager(28567): phase2 "auth=NULL" D/WifiManager(28567): IP config: D/WifiManager(28567): IP assignment: UNASSIGNED D/WifiManager(28567): Proxy settings: UNASSIGNED D/WifiManager(28567): cuid=-1 luid=-1 lcuid=0 userApproved=USER_UNSPECIFIED noInternetAccessExpected=false D/WifiManager(28567): recentFailure: Association Rejection code: 0 D/WifiManager(28567): D/WifiManager(28567): samsungSpecificFlags: D/WifiManager(28567): semAutoWifiScore: 0 D/WifiManager(28567): isVendorAp : false D/WifiManager(28567): recoverableRSSI: -200 D/WifiManager(28567): inRecoverArea: false D/WifiManager(28567): disabledTime: 0 D/WifiManager(28567): notInRangeTime: 0 D/WifiManager(28567): isUsableInternet: false D/WifiManager(28567): skipInternetCheck: -1 D/WifiManager(28567): notAskAgainCheck: false D/WifiManager(28567): nextTargetRssi: 0 D/WifiManager(28567): isCaptivePortal: false D/WifiManager(28567): isAuthenticated: false D/WifiManager(28567): loginUrl: null D/WifiManager(28567): autoReconnect: 1 D/WifiManager(28567): isRecommended: false D/WifiManager(28567): isHomeProviderNetwork: false D/WifiManager(28567): WapiCertIndex: 0 D/WifiManager(28567): WapiPskType: 0 D/WifiManager(28567): isWeChatAp : false D/WifiManager(28567): entryRssi24GHz : -78 D/WifiManager(28567): entryRssi5GHz : -75 target=android.net.wifi.WifiManager$LocalOnlyHotspotCallbackProxy$1 } D/AndroidRuntime(28567): Shutting down VM E/AndroidRuntime(28567): FATAL EXCEPTION: main E/AndroidRuntime(28567): Process: com.kwikwink.kw_delivery, PID: 28567 E/AndroidRuntime(28567): java.lang.IllegalArgumentException: Unsupported value: 'android.net.wifi.WifiManager$LocalOnlyHotspotReservation@f603f75' of type 'class android.net.wifi.WifiManager$LocalOnlyHotspotReservation' E/AndroidRuntime(28567): at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:297) E/AndroidRuntime(28567): at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:63) E/AndroidRuntime(28567): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267) E/AndroidRuntime(28567): at com.kwikwink.kw_delivery.MainActivity$configureFlutterEngine$1$1.onStarted(MainActivity.kt:33) E/AndroidRuntime(28567): at android.net.wifi.WifiManager$LocalOnlyHotspotCallbackProxy$1.handleMessage(WifiManager.java:3755) E/AndroidRuntime(28567): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(28567): at android.os.Looper.loop(Looper.java:214) E/AndroidRuntime(28567): at android.app.ActivityThread.main(ActivityThread.java:7050) E/AndroidRuntime(28567): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(28567): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) E/AndroidRuntime(28567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) I/Process (28567): Sending signal. PID: 28567 SIG: 9 Lost connection to device.
If anyone can help me with this it could be lovely. Thanks anyway!
The problem is in this line:
result.success(localOnlyHotspotReservation)
You cannot return a Java object (in this case WifiManager$LocalOnlyHotspotReservation
) from a method call - just supported values (integers, strings, lists, maps, etc).
If you'd like to return some particular value from there, like the SSID, you'd need to extract it in the native code and return that (supported) type. For example:
result.success(localOnlyHotspotReservation.getSoftApConfiguration().getWifiSsid().toString())