我想制作用于从 Azure IoT 中心接收消息的 UWP 应用程序。我找到了控制台接收器应用程序的示例代码,但它不适用于 UWP,因为 UWP 不支持此参考
using Microsoft.ServiceBus.Messaging;
有人可以发布 UWP 应用程序的代码以从 Azure IoT 中心接收消息吗?
我想制作用于从 Azure IoT 中心接收消息的 UWP 应用程序。我找到了控制台接收器应用程序的示例代码,但它不适用于 UWP,因为 UWP 不支持此参考
using Microsoft.ServiceBus.Messaging;
有人可以发布 UWP 应用程序的代码以从 Azure IoT 中心接收消息吗?
你可以试试 AzureSBLite,你可以从 NuGet 包管理器安装它。
https://github.com/ppatierno/azuresblite
接收示例代码:
string ConnectionString = "<EventHub-Compatible-EndPoint>;SharedAccessKeyName=iothubowner;SharedAccessKey<Key>
static string eventHubEntity = "<EventHub-Compatible-Name>";
string partitionId = "0";
DateTime startingDateTimeUtc;
EventHubConsumerGroup group;
EventHubClient client;
MessagingFactory factory;
EventHubReceiver receiver;
public async Task ReceiveDataFromCloud()
{
startingDateTimeUtc = DateTime.UtcNow;
ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConnectionString);
builder.TransportType = ppatierno.AzureSBLite.Messaging.TransportType.Amqp;
factory = MessagingFactory.CreateFromConnectionString(ConnectionString);
client = factory.CreateEventHubClient(eventHubEntity);
group = client.GetDefaultConsumerGroup();
receiver = group.CreateReceiver(partitionId.ToString(), startingDateTimeUtc);//startingDateTimeUtc
while (true)
{
EventData data = receiver.Receive();
if (data != null)
{
var receiveddata = Encoding.UTF8.GetString(data.GetBytes());
Debug.WriteLine("{0} {1} {2}", data.SequenceNumber, data.EnqueuedTimeUtc.ToLocalTime(), Encoding.UTF8.GetString(data.GetBytes()));
}
else
{
break;
}
await Task.Delay(2000);
}
receiver.Close();
client.Close();
factory.Close();
}
您可以从 IoTHub settings-> Messaging 部分获取 EventHub-Compatible-Endpoint 和 EventHub-Compatible Name。在设备到云设置下。
请检查我创建的 UWP 应用程序的源代码:
CloudDataService 类包含两个方法并使用 IoT Hub SDK:
这是一个如何使用 UWP 应用和 IoT 中心发送和接收消息的示例。
官方SDK支持使用HTTP端点接收云端发给设备的消息。看:
https://github.com/Azure/azure-iot-sdks/tree/master/csharp/device/samples/UWPSample
接收设备发送到云的消息比较棘手,因为它是一个 AMQP 端点,但可以使用 AMQPNetLite 完成:
这是从 iot 接收消息的最佳方式。有两种方法可以做到这一点C2D
......D2C
云到设备和设备到云。这取决于您的要求。我对代码进行了全面测试:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
//txtblkMessages.Text = "test";
Start();
}
private static int MESSAGE_COUNT = 1;
private const string DeviceConnectionString = "HostName=[your hostname];DeviceId=[your DeviceId];SharedAccessKey=[your shared key]";
public async Task Start()
{
try
{
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Amqp);
//await SendEvent(deviceClient);
await ReceiveCommands(deviceClient);
}
catch (Exception ex)
{
Debug.WriteLine("Error in sample: {0}", ex.Message);
}
}
async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
//for (int count = 0; count < MESSAGE_COUNT; count++)
//{
dataBuffer = "Hello Iot";
Message eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
await deviceClient.SendEventAsync(eventMessage);
// }
}
async Task ReceiveCommands(DeviceClient deviceClient)
{
Message receivedMessage;
string messageData;
while (true)
{
receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
txtblkMessages.Text = messageData + "\n" + txtblkMessages.Text;
await deviceClient.CompleteAsync(receivedMessage);
}
// Note: In this sample, the polling interval is set to
// 10 seconds to enable you to see messages as they are sent.
// To enable an IoT solution to scale, you should extend this // interval. For example, to scale to 1 million devices, set
// the polling interval to 25 minutes.
// For further information, see
// https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
await Task.Delay(TimeSpan.FromSeconds(1));
}
}
}
我也面临同样的问题。我无法收到消息 Azure IOT 集线器。因为 AMQP 在 UWP 中不支持。我试过下面的示例。但我没有运气收到消息。 https://github.com/Azure/azure-iot-sdks/tree/master/csharp/device/samples/UWPSample
如果您在 UWP 中收到消息,请告诉我。是否计划在 UWP 中支持 AMQP?什么时候会给出这个更新?