发现设备mono.upnp

时间:2014-08-17 22:34:31

标签: c# mono xamarin xamarin.android upnp

我正在尝试通过upnp与我的新wemo切换进行通信。在Windows上,这很好用。现在我正在尝试使用mono.upnp lib在android上做同样的事情。一切看起来都一样,但我不知道如何在mono.upnp上发现设备。

以下是Windows上的代码:

    public static List<WeMoDevice> GetDevices ()
    {
        UPnPDeviceFinder finder = new UPnPDeviceFinder ();
        List<WeMoDevice> foundDevices = new List<WeMoDevice> ();


        string deviceType = "upnp:rootdevice";
        Device devices = finder.FindByType (deviceType, 1);


        foreach (Device device in devices) {
            if (device.Type.StartsWith ("urn:Belkin:")) {
                switch (GetDeviceType (device)) {
                case WeMoDeviceType.Switch:
                    WeMoSwitch wemoSwitch = new WeMoSwitch (device);
                    foundDevices.Add (wemoSwitch);
                    break;

                case WeMoDeviceType.Sensor:
                    WeMoSensor wemoSensor = new WeMoSensor (device);
                    foundDevices.Add (wemoSensor);
                    break;
                default:

                    break;

                }
            }
        }

        return foundDevices;
    }

我已经将设备类更改为mono.upnp,但我似乎无法在mono.upnp中找到UPnPDeviceFinder的等效项。

1 个答案:

答案 0 :(得分:2)

好吧终于搞定了。这是我用来打开和关闭wemo的代码:

    const string COMMAND_OFF = @"<?xml version=""1.0"" encoding=""utf-8""?><s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><s:Body><u:SetBinaryState xmlns:u=""urn:Belkin:service:basicevent:1""><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>";
    const string COMMAND_ON = @"<?xml version=""1.0"" encoding=""utf-8""?><s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><s:Body><u:SetBinaryState xmlns:u=""urn:Belkin:service:basicevent:1""><BinaryState>1</BinaryState></u:SetBinaryState></s:Body></s:Envelope>";

    public void On (string iP, string port)
    {
        SendCommand (COMMAND_ON, iP, port); 
    }


    public void Off (string iP, string port)
    {
        SendCommand (COMMAND_OFF, iP, port);
    }

    private void SendCommand (string command, string iP, string port)
    {

        string targetUrl = "http://" + iP + ":" + port + "/upnp/control/basicevent1";


        //  Create the packet and payload to send to the endpoint to get the switch to process the command

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (targetUrl);
        request.Method = "POST";
        request.Headers.Add ("SOAPAction", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
        request.ContentType = @"text/xml; charset=""utf-8""";
        request.KeepAlive = false;
        Byte[] bytes = UTF8Encoding.ASCII.GetBytes (command);
        request.ContentLength = bytes.Length;
        using (Stream stream = request.GetRequestStream ()) {
            stream.Write (bytes, 0, bytes.Length);
            stream.Close ();
            request.GetResponse ();
        }


        //  HACK: If we don't abort the result the device holds on to the connection sometimes and prevents other commands from being received

        request.Abort ();
    }

    public void GetDevice (string Name, wemoAction action)
    {

        try {

            Client client = new Client ();

            client.BrowseAll (); //Browse all available upnp devices

            client.DeviceAdded += (sender, e) => { //do something when a device is found
                System.Console.WriteLine ("got one!");
                if (e.Device.ToString ().Contains ("urn:Belkin")) {

                    if (e.Device.GetDevice ().FriendlyName.Equals (Name)) {
                        var url = e.Device.GetDevice ().Services.First ().EventUrl;
                        switch (action) {
                        case wemoAction.on:
                            On (url.DnsSafeHost, url.Port.ToString ());
                            break;
                        case wemoAction.off:
                            Off (url.DnsSafeHost, url.Port.ToString ());
                            break;
                        }
                    }

                }

            };


        } catch (Exception ex) {
            System.Console.WriteLine (ex.Message);
        }

    }

发送开启和关闭数据包的代码来自此视频:http://www.youtube.com/watch?v=ifzmJFdvNEE