Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
karthiksensesemi opened this issue Apr 22, 2024 · 8 comments

Comments

@karthiksensesemi
Copy link

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
Copy link

fondraco commented May 2, 2024

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
Copy link
Author

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
Copy link

fondraco commented May 2, 2024

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
Copy link
Author

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

@fondraco
Copy link

fondraco commented May 2, 2024

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

@karthiksensesemi
Copy link
Author

karthiksensesemi commented May 2, 2024

Actually,

  • ForceWifiUsage(true) helped me fix this socket problem.
  • My code:

Future connectWifi() async {
await WiFiForIoTPlugin.disconnect();
await WiFiForIoTPlugin.connect(wifiSsid, isHidden: true);
final res = await WiFiForIoTPlugin.forceWifiUsage(true);
print(" connection status : $res");
return isConnected;
}

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
Copy link
Author

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

@fondraco
Copy link

fondraco commented May 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants