无法连接到Yeelight wifi彩色灯泡

时间:2018-05-09 17:26:08

标签: android iot xiaomi

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. 

此代码包含简单的SSDP消息,该消息在239.255.255.250:1982端口上连接。

所有连接到同一wifi(灯泡和手机)的设备 我有D-Link DIR 600M路由器。

异常:我收到了connectiontimout异常。无论我设置多长时间,但这段代码在给定时间后无法连接。

为脏代码道歉。

**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();
        }
    }

0 个答案:

没有答案