Я занимаюсь разработкой приложения xamarin в .netStandard 2.0 с использованием Visual Studio 2019. У меня проблема с получением данных из iothub. Я могу отправлять данные из приложения xamarin в iot hub, но не могу получить данные из azure iothub.
Liberies
- Microsoft. Azure .Device (1.20.0)
Microsoft. Azure .Device .Клиент (1.24.0)
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.http1);
await SendEvent(deviceClient);
await ReceiveCommands(deviceClient);
}
catch (Exception ex)
{
Debug.WriteLine("Error in sample: {0}", ex.Message);
}
}
async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
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);
}
await Task.Delay(TimeSpan.FromSeconds(1));
}
}