Table of Contents

Class DeviceClient

Namespace
nanoFramework.Azure.Devices.Client
Assembly
nanoFramework.Azure.Devices.Client.dll

Azure IoT Client SDK for .NET nanoFramework using MQTT.

public class DeviceClient : IDisposable
Inheritance
DeviceClient
Implements
Inherited Members
Extension Methods

Constructors

DeviceClient(string, string, byte[], MqttQoSLevel, byte[], string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, byte[] clientCert = null, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, byte[] azureCert = null, string modelId = null)

Parameters

iotHubName string

Your Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).

deviceId string

The device ID (name of your device).

clientCert byte[]

The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.

qosLevel MqttQoSLevel

The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).

azureCert byte[]

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, X509Certificate2, MqttQoSLevel, X509Certificate, string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)

Parameters

iotHubName string

Your Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).

deviceId string

The device ID (name of your device).

clientCert X509Certificate2

The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.

qosLevel MqttQoSLevel

The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).

azureCert X509Certificate

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, string, byte[], MqttQoSLevel, byte[], string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, string moduleId, byte[] clientCert = null, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, byte[] azureCert = null, string modelId = null)

Parameters

iotHubName string

Your Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).

deviceId string

The device ID (name of your device).

moduleId string

The module ID which is attached to the device ID.

clientCert byte[]

The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.

qosLevel MqttQoSLevel

The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).

azureCert byte[]

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, string, X509Certificate2, MqttQoSLevel, X509Certificate, string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, string moduleId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)

Parameters

iotHubName string

Your Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).

deviceId string

The device ID (name of your device).

moduleId string

The module ID which is attached to the device ID.

clientCert X509Certificate2

The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.

qosLevel MqttQoSLevel

The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).

azureCert X509Certificate

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, string, string, MqttQoSLevel, byte[], string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, string moduleId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, byte[] azureCert = null, string modelId = null)

Parameters

iotHubName string

The Azure IoT name fully qualified (ex: youriothub.azure-devices.net).

deviceId string

The device ID which is the name of your device.

moduleId string

The module ID which is attached to the device ID.

sasKey string

One of the SAS Key either primary, either secondary.

qosLevel MqttQoSLevel

The default quality level delivery for the MQTT messages, default to the lower quality.

azureCert byte[]

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, string, string, MqttQoSLevel, X509Certificate, string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, string moduleId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, X509Certificate azureCert = null, string modelId = null)

Parameters

iotHubName string

The Azure IoT name fully qualified (ex: youriothub.azure-devices.net).

deviceId string

The device ID which is the name of your device.

moduleId string

The module ID which is attached to the device ID.

sasKey string

One of the SAS Key either primary, either secondary.

qosLevel MqttQoSLevel

The default quality level delivery for the MQTT messages, default to the lower quality.

azureCert X509Certificate

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, string, MqttQoSLevel, byte[], string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, byte[] azureCert = null, string modelId = null)

Parameters

iotHubName string

The Azure IoT name fully qualified (ex: youriothub.azure-devices.net).

deviceId string

The device ID which is the name of your device.

sasKey string

One of the SAS Key either primary, either secondary.

qosLevel MqttQoSLevel

The default quality level delivery for the MQTT messages, default to the lower quality.

azureCert byte[]

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(string, string, string, MqttQoSLevel, X509Certificate, string)

Creates an DeviceClient class.

public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, X509Certificate azureCert = null, string modelId = null)

Parameters

iotHubName string

The Azure IoT name fully qualified (ex: youriothub.azure-devices.net).

deviceId string

The device ID which is the name of your device.

sasKey string

One of the SAS Key either primary, either secondary.

qosLevel MqttQoSLevel

The default quality level delivery for the MQTT messages, default to the lower quality.

azureCert X509Certificate

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(IMqttClient, string, string, byte[], MqttQoSLevel, byte[], string)

Creates an DeviceClient class.

public DeviceClient(IMqttClient mqttc, string iotHubName, string deviceId, byte[] clientCert = null, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, byte[] azureCert = null, string modelId = null)

Parameters

mqttc IMqttClient

The MQTT client to use.

iotHubName string

Your Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).

deviceId string

The device ID (name of your device).

clientCert byte[]

The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.

qosLevel MqttQoSLevel

The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).

azureCert byte[]

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

DeviceClient(IMqttClient, string, string, string, MqttQoSLevel, byte[], string)

Creates an DeviceClient class.

public DeviceClient(IMqttClient mqttc, string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, byte[] azureCert = null, string modelId = null)

Parameters

mqttc IMqttClient

The MQTT client to use.

iotHubName string

The Azure IoT name fully qualified (ex: youriothub.azure-devices.net).

deviceId string

The device ID which is the name of your device.

sasKey string

One of the SAS Key either primary, either secondary.

qosLevel MqttQoSLevel

The default quality level delivery for the MQTT messages, default to the lower quality.

azureCert byte[]

Azure certificate for the connection to Azure IoT Hub.

modelId string

Azure Plug and Play model ID.

Properties

IoTHubStatus

The latest status.

public IoTHubStatus IoTHubStatus { get; }

Property Value

IoTHubStatus

IsConnected

True if the device connected.

public bool IsConnected { get; }

Property Value

bool

LastTwin

The latest Twin received.

public Twin LastTwin { get; }

Property Value

Twin

ModelId

Azure Plug and Play model ID.

public string ModelId { get; }

Property Value

string

ModuleId

The module ID attached to the device ID.

public string ModuleId { get; }

Property Value

string

QosLevel

The default level quality.

public MqttQoSLevel QosLevel { get; set; }

Property Value

MqttQoSLevel

Methods

AddMethodCallback(MethodCallback)

Add a callback method.

public void AddMethodCallback(MethodCallback methodCallback)

Parameters

methodCallback MethodCallback

The callback method to add.

Close()

Close the connection with Azure IoT and disconnect the device.

public void Close()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetTwin(CancellationToken)

Gets the twin.

public Twin GetTwin(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A cancellation token.

Returns

Twin

The twin.

Remarks

It is strongly recommended to use a cancellation token that can be canceled and manage this on the caller code level. A reasonable time of few seconds is recommended with a retry mechanism.

Open()

Open the connection with Azure IoT. This will initiate a connection from the device to the Azure IoT Hub instance.

public bool Open()

Returns

bool

True if open.

Reconnect()

Reconnect to Azure Iot Hub.

public void Reconnect()

RemoveMethodCallback(MethodCallback)

Remove a callback method.

public void RemoveMethodCallback(MethodCallback methodCallback)

Parameters

methodCallback MethodCallback

The callback method to remove.

SendMessage(string, string, ArrayList, CancellationToken, string)

Send a message to Azure IoT.

public bool SendMessage(string message, string contentType, ArrayList userProperties, CancellationToken cancellationToken = default, string dtdlComponentname = "")

Parameters

message string

The message to send.

contentType string

Content of the application message.

userProperties ArrayList

User properties to add to the application message.

cancellationToken CancellationToken

A cancellation token. If you use the default one, the confirmation of delivery will not be awaited.

dtdlComponentname string

The DTDL component name.

Returns

bool

True for successful message delivery.

SendMessage(string, string, CancellationToken)

Send a message to Azure IoT.

public bool SendMessage(string message, string contentType, CancellationToken cancellationToken = default)

Parameters

message string

The message to send.

contentType string

Content of the application message.

cancellationToken CancellationToken

A cancellation token. If you use the default one, the confirmation of delivery will not be awaited.

Returns

bool

True for successful message delivery.

SendMessage(string, CancellationToken, string)

Send a message to Azure IoT.

public bool SendMessage(string message, CancellationToken cancellationToken = default, string dtdlComponentname = "")

Parameters

message string

The message to send.

cancellationToken CancellationToken

A cancellation token. If you use the default one, the confirmation of delivery will not be awaited.

dtdlComponentname string

The DTDL component name.

Returns

bool

True for successful message delivery.

UpdateReportedProperties(TwinCollection, CancellationToken)

Update the twin reported properties.

public bool UpdateReportedProperties(TwinCollection reported, CancellationToken cancellationToken = default)

Parameters

reported TwinCollection

The reported properties.

cancellationToken CancellationToken

A cancellation token. If you use the default one, the confirmation of delivery will not be awaited.

Returns

bool

True for successful message delivery.

Events

CloudToDeviceMessage

Cloud to device message received event.

public event CloudToDeviceMessage CloudToDeviceMessage

Event Type

CloudToDeviceMessage

StatusUpdated

Status change event.

public event StatusUpdated StatusUpdated

Event Type

StatusUpdated

TwinUpdated

Device twin updated event.

public event TwinUpdated TwinUpdated

Event Type

TwinUpdated