flutternetwork / WiFiFlutter

Flutter plugin suite for various WiFi services.
https://wifi.flutternetwork.dev
289 stars 182 forks source link

Successfully connects to wifi, But it doesn't work with tcp sockets for sending and receiving data. #383

Open karthiksensesemi opened 7 months ago

karthiksensesemi commented 7 months ago

I'm using this package for auto-connect to iot device. It gets connected successfully but when i wanted to receive data through tcp socket "SocketException: Connection failed (OS Error: Network is unreachable, errno = 101), address = 192.168.1.1, port = 5002". I face the above error whereas when i manually to wifi, i receive all the data through sockets.

fondraco commented 6 months ago

I have also the same problem. Although it only occurs for me on android 14. Other androids are working and iOS devices are also fine.

Could you please share your device info? @karthiksensesemi

karthiksensesemi commented 6 months ago

My android version is 11. Now the above data transfer works. But once the data is received and iot wifi is disconnected. The mobile Internet/Wifi doesn't work within the app.

fondraco commented 6 months ago

Are you forcing wifi usage , and if so. Are you also unforcing it before disconnecting?

https://pub.dev/documentation/wifi_iot/latest/wifi_iot/WiFiForIoTPlugin/forceWifiUsage.html

karthiksensesemi commented 6 months ago

Thanks a ton. I fixed it. I was clueless and frustrated from past 3 weeks.

fondraco commented 6 months ago

Glad to hear that. How did you resolve the socket problem?

karthiksensesemi commented 6 months ago

Actually,

readWriteWifi({String? req}) async { try { //connect and listen to data socket = await Socket.connect(wifiHost, wifiPort); print(socket!.address); socket!.write(req); final a = socket!.listen((List data) async { print('Received data from ESP32: $data'); if (data[1] == 1 || data[1] == 2 || data[1] == 3) { sendAck(); } }, onDone: () { socket!.close(); }, onError: (error) async { socket!.close(); final res = await WiFiForIoTPlugin.forceWifiUsage(false); await WiFiForIoTPlugin.disconnect(); syncCompleted(); print(error); }); } catch (e) { syncFailed(); final res = await WiFiForIoTPlugin.forceWifiUsage(false); await WiFiForIoTPlugin.disconnect(); disableWifi(resultCharacteristic!); print(e.toString()); } }

karthiksensesemi commented 6 months ago

I think I need to test with android 14 as well. And will come back with update.

fondraco commented 6 months ago

I've got it working as well. The problem was that on some androids you have to force wifi before pinging the device. iOS doesn't require that.