Written simple code in Android to connect with Yeelight wifi color bulb but unable to connect with bulb, no idea why. Here is my code.
Note : Device is simple pojo class, you can remove it.
I’m getting timeout exception
try {
Device device = null;
byte[] sendData;
byte[] receiveData = new byte[1024];
DatagramSocket clientSocket=null;
/* Create the search request */
String NL = "\r\n";
StringBuilder mSearch = new StringBuilder();
mSearch.append("M-SEARCH * HTTP/1.1").append(NL);
mSearch.append("HOST: 239.255.255.250:1982").append(NL);
mSearch.append("MAN: \"ssdp:discover\"").append(NL);
mSearch.append("ST: wifi_bulb").append(NL);
try {
/* Send the request */
sendData = mSearch.toString().getBytes();
DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length, InetAddress.getByName("239.255.255.250"), 1982);
clientSocket = new DatagramSocket();
clientSocket.setSoTimeout(20000);
clientSocket.send(sendPacket);
/* Receive one response */
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
device = Device.parse(receivePacket);
System.out.println("URL "+device.getDescriptionUrl());
}
catch (Exception e) {
e.printStackTrace();
}
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}