Android Quick Setting SocketTimeoutException

36 Views Asked by At

I recently got a power plug with WIFI to control a lamp. Toggling the lamp on/off works great when calling the URL from a browser. For more convenience, I wanted to implement a quick settings toggle for my phone.

So I followed the sample from the Android Dev site and now every time I click on the toggle, my code gets executed. Then I added a HttpURLConnection, set everything up and got an IOException :/

java.io.IOException: Cleartext HTTP traffic to xxx.xxx.xxx.110 not permitted

The plug doesn't support http, so I learned that I had to permit cleartextTraffic.

AndroidManifest.xml
---------------------------------------
<?xml ...
<manifest ...
  <uses-permission android:name="android.permission.INTERNET" />
  ...
  <application
    ...
    android:networkSecurityConfig="@xml/network_security_config"
    tools:targetApi="31">
...
</manifest>
network_security_config.xml
---------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">xxx.xxx.xxx.110</domain>
  </domain-config>
</network-security-config>

After adding that networkSecurityConfig, the 'Cleartext not permitted' error no longer occurred and the lamp was toggling each time I pressed the Quick Setting tile.

But after that, I unplugged the USB cable from the phone tried it again but nothing was happening. Not only did the lamp not toggle, also the neither the color of the tile changed, nor did the label change between 'Off' and 'On' like before.

So I reconnected the USB cable, looked into the Logcat in the AndroidStudio and found a SocketTimeoutException.

java.net.SocketTimeoutException: failed to connect to /xxx.xxx.xxx.110 (port 80) from /xxx.xxx.xxx.120 (port 46074) after 5000ms
  at libcore.io.IoBridge.connectErrno(IoBridge.java:235)
  at libcore.io.IoBridge.connect(IoBridge.java:179)
  at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
  at java.net.Socket.connect(Socket.java:646)
  at com.android.okhttp.internal.Platform.connectSocket(Platform.java:182)
  at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:145)
  at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
  at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
  at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
  at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
  at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
  at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
  at com.mine.qstile.MyQSTileService$1.run(MyQSTileService.java:93)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
  at java.lang.Thread.run(Thread.java:1012)

I clicked the tile again and it instantly toggled the lamp again. Unplugged it and it didn't work. The error message is always the same SocketTimeoutException but the 'from' port changes.

It seems like the call just gets through if I've connected my phone via USB and the USB debugging is enabled? Am I missing something?

Thanks, kopi

0

There are 0 best solutions below