0

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.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Daanoo
  • 11
  • 4
  • 1
    Manufacturers own doc says the protocol is _"SSDP like but it's greatly simplified"_ so who knows? Have you compared your tools traffic to the one generated by the manufacturers tool? – Jussi Kukkonen Jun 09 '20 at 10:40
  • If this was SSDP the M-SEARCH would seem to be missing an extra newline in the end. – Jussi Kukkonen Jun 09 '20 at 10:44
  • @JussiKukkonen Existing applications written for this bulb dont discover it either. I just tried a python one, and it doesnt discover it on the network either. Could it be a network/firewall related issue? – Daanoo Jun 09 '20 at 11:24
  • sure. I'd start by making sure that another wifi-connected device can actually see your multicast M-SEARCH message -- to make sure it's not a network issue. I'd also add the missing empty line in the end of the message – Jussi Kukkonen Jun 10 '20 at 06:04

0 Answers0