I am trying to create a project in which I have to pilot an IoT device from a smartphone via Wi-Fi.
This device has an integrated Wi-Fi module SPWF01 and is configured as an access point (without Internet access) with the WEP security type. In this configuration of access points, we also have a TCP Socket Server, which intercepts communication with smartphones.
On the side of the smartphone, we have a part that scans and connects to the access point to the device (which works, although I get the point of discrimination on the Wi-Fi icon, since it does not have Internet access). After we are connected, we will start the client socket, which connects to the server on our IoT device (the server socket IP address is actually the gateway of the access point). And here the problem begins, because the client socket will not start. Here is the code:
public void SocketInit(String ip, int port) throws IOException { InetAddress addr = InetAddress.getByName(ip); SocketAddress sockaddr = new InetSocketAddress(addr, port); nsocket = new Socket(); nsocket.setReuseAddress(true); nsocket.setTcpNoDelay(false); nsocket.setReceiveBufferSize(700);
And with the exception of:
java.net.SocketException: socket failed: ENONET (Machine is not on the network)
And I get this error even before reaching nsocket.connect (), exactly on setReuseAddress.
Since the exception I get is ENONET, I thought it should be because the access point does not have Internet access, so I used the proposed solution here for the purpose of testing:
adb shell settings put global captive_portal_detection_enabled 0
This is a solution that cannot be executed programmatically without root access, but I wanted to check if this was a problem. But although the exclamation mark on the Wi-Fi icon disappeared, the client socket still gave me the same exception error.
Does anyone have a solution to this behavior? Thank you in advance!
Sometimes a client socket can be opened, with success 1 out of 20 times. But when this happens, I usually get another exception after sending multiple messages:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
Here is the code I used to connect to the access point from a smartphone:
WifiConfiguration wc=new WifiConfiguration(); wc.SSID= host; wc.status = WifiConfiguration.Status.ENABLED; wc.priority = 40; wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); wc.allowedGroupCiphers.clear(); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); wc.wepKeys[0] = password; wc.wepTxKeyIndex = 0; int netId=mainWifi.addNetwork(wc); try {
The access point security type is WEP. This is due to the fact that the Wi-Fi module cannot implement WPA.
Tests conducted on marshmallows.