在我的Windows Phone应用程序中枚举可用的WiFi打印机

时间:2013-09-09 17:33:28

标签: c# printing windows-phone-8

我知道很多类似的问题,即“我可以从我的wp8打印吗?”。大多数人似乎满足于简单的“不”反应。我希望将此功能添加到我的应用程序中,我知道它可以在某种程度上完成 - 即使我现在只能支持一个非常小的打印机。

我查看了应用PrintHand,它似乎具备了我需要的功能:打印到无线和蓝牙打印机。

我一直在查看Bluetooth方案文档,我认为这可能有助于发现蓝牙打印机。那是一个开始。也许它也有助于识别无线打印机。

我意识到我需要从这个项目开始非常小,我想首先尝试枚举当前网络上可用的任何无线打印机(我还没有蓝牙打印机)。有没有人碰巧有关于如何开始或更好的方向的指针,一些相关的示例代码?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我能给予的最好的是我的Github Repo,它与Wifi打印机无关,但与谷歌云打印有关。

在他们的文档中使用Google Cloud Print并没有真正的.net参考,但代码在Mono中运行,应该很容易转移到.NET,因此也就是Windows Phone。

MonoGCP

答案 1 :(得分:1)

我能够使用简单服务发现协议检测附近的无线打印机。

这是我的示例函数:

private const string SSDP_IP = "239.255.255.250";
private const string SSDP_PORT = "1900";

public async static void DiscoverAsync2()
{       
    var multicastIP = new HostName(SSDP_IP);
    var found = false;

    using (var socket = new DatagramSocket())
    {
        socket.MessageReceived += (sender, e) =>
        {
            var reader = e.GetDataReader();
            var bytesRemaining = reader.UnconsumedBufferLength;
            var receivedString = reader.ReadString(bytesRemaining);

            // TODO: something useful with this new info

            found = true;
        };

        await socket.BindEndpointAsync(null, string.Empty);
        socket.JoinMulticastGroup(multicastIP);

        while (true)
        { 
            found = false;

            using (var stream = await socket.GetOutputStreamAsync(multicastIP, SSDP_PORT))
            {
                var request = new StringBuilder();
                request.AppendLine("M-SEARCH * HTTP/1.1");
                request.AppendLine("HOST: " + SSDP_IP + ":" + SSDP_PORT);
                request.AppendLine("MAN: \"ssdp:discover\"");
                request.AppendLine("MX: 3");
                request.AppendLine("ST: urn:schemas-upnp-org:device:Printer:1"); // use ssdp:all to get everything
                request.AppendLine(); // without this extra blank line, query won't run properly
                var buff = Encoding.UTF8.GetBytes(request.ToString());

                await stream.WriteAsync(buff.AsBuffer());

                await Task.Delay(5000);

                if (!found)
                    break;
            }
        }
    }