I'm trying to create android app which can control Yeelight wifi color bulb. I've written simple peace of code that connect to Yeelight but I'm really not sure what exactly I'm doing wrong.
此代码包含连接在 239.255.255.250:1982 端口上的简单 SSDP 消息。
所有连接到同一个 wifi(灯泡和电话)的设备我都有 D-Link DIR 600M 路由器。
异常:我收到 connectiontimeout 异常。我设置了多少时间并不重要,但此代码在给定时间后将无法连接。
为脏代码道歉。
**Here is the code**
public void connectDevice(){
try {
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");
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(50000);
clientSocket.send(sendPacket);
/* Receive one response */
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
}
catch (Exception e) {
e.printStackTrace();
}
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}