I am trying to make an application in Java that connects to my Yeelight Color Bulb and lets me control it from my PC. But now I am running into a problem, which is that it cant be discovered, the receive request just times out, no matter how long I set the timing to. LAN mode is turned on, I have reset the device earlier, and made sure the setting is still turned on. They are connected to the same router, the bulb via Wifi, the PC via ethernet.
Here is my code:
public static void main(String[] args) throws Exception {
String NL = "\r\n";
StringBuilder sb = new StringBuilder();
sb.append("M-SEARCH * HTTP/1.1").append(NL);
sb.append("HOST: 239.255.255.250:1982").append(NL);
sb.append("MAN: \"ssdp:discover\"").append(NL);
sb.append("ST: wifi_bulb").append(NL);
try (DatagramSocket s = new DatagramSocket()) {
InetAddress address = InetAddress.getByName("239.255.255.250");
s.setSoTimeout(20000);
String str = sb.toString();
DatagramPacket hi = new DatagramPacket(str.getBytes(), str.length(), address, 1982);
s.send(hi);
byte[] buf = new byte[2048];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
s.receive(recv);
System.out.println("Received data: " + recv.getLength() + "\n"
+ new String(buf, recv.getOffset(), recv.getLength()));
}
Here is the error I am getting:
Exception in thread "main" java.net.SocketTimeoutException: Receive timed out
at java.base/java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.base/java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:130)
at java.base/java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:181)
at java.base/java.net.DatagramSocket.receive(DatagramSocket.java:864)
at com.daan.Main.main(Main.java:27)
Does someone know what can be causing this issue? I have also tried something similar in C#, gave me also no luck and timed out as well.