0
votes

I have a Raspberry Pi running Windows IOT Core, which has been provisioned to an Azure IOT Hub. I'm writing a UWP app and I want to send messages to the IOT Hub, which I think I need Microsoft.Azure.Devices.Client.DeviceClient to do. DeviceClient needs a connection string, which I can hardcode in the app for testing.

How do I:

  1. Check if the device the app is running on is provisioned to an IOT Hub?
  2. Get the connection string for the IOT Hub?

I can't know this information at compile time, and I don't want to rebuild my application for every device/deployment.

2

2 Answers

0
votes

Microsoft.Azure.Devices.Client.DeviceClient is IoT Hub Device SDKs. It is used to send telemetry to your IoT hub, and optionally receive messages, job, method, or twin updates from your IoT hub. But if you want to get the connection string for the IoT Hub and check if the device is provisioned to an IoT Hub, you need to use IoT Hub Service SDKs. It enables you to build backend applications to manage your IoT hub, and optionally send messages, schedule jobs, invoke direct methods, or send desired property updates to your IoT devices or modules. But you also need to set the DeviceId and iot hub connection string in your app. It is not recommended to generated the connection string in device client end. The sdk supports UWP app to run on Windows IoT Core.

0
votes

I think you might want to try to check out the UWP Bridge. It is a WinRT library that can be used to connect to the Device Agent and read the connection string from the TPM.

You can build the DMBridgeComponent library from the azure-client-tools repo on GitHub (https://github.com/ms-iot/azure-client-tools) and then reference it from your UWP app.

Then, to read your connection string you just need the following code:

using DMBridgeComponent;
...

var tpm = new TpmBridge();

// Get connection string from TPM
var slotNumberValue = 0;
var connectionString = tpm.GetConnectionString(slotNumberValue, 36000);

Set the slotNumberValue to whatever slot you used when you provisioned your device. It is usually 0 by default.

There is more information here (https://github.com/ms-iot/azure-client-tools/blob/master/docs/device-agent/uwp-bridge.md). That's where I found the sample code.