DeviceClient Class
- java.
lang. Object - com.
microsoft. azure. sdk. iot. device. InternalClient - com.
microsoft. azure. sdk. iot. device. DeviceClient
- com.
- com.
Implements
public final class DeviceClient
extends InternalClient
implements java.io.Closeable
The public-facing API. Allows a single logical or physical device to connect to an IoT Hub. The IoT Hub client supports sending events to and receiving messages from an IoT Hub.
To support these workflows, the client library will provide the following abstractions: a message, with its payload and associated properties; and a client, which sends and receives messages.
The client buffers messages while the network is down, and re-sends them when the network comes back online. It also batches messages to improve communication efficiency (HTTPS only).
The client supports HTTPS 1.1 and AMQPS 1.0 transports.
Field Summary
Modifier and Type | Field and Description |
---|---|
static final java.nio.charset.Charset |
CONNECTION_STRING_CHARSET
Deprecated
as of release 1.2.27 this value is deprecated and will not be replaced. The charset used for URL-encoding the device ID in the connection string.
|
static final java.lang.String |
DEVICE_ID_ATTRIBUTE
Deprecated
as of release 1.2.27 this value is deprecated and will not be replaced. The device ID attribute name in a connection string.
|
static final java.lang.String |
HOSTNAME_ATTRIBUTE
Deprecated
as of release 1.2.27 this value is deprecated and will not be replaced. The hostname attribute name in a connection string.
|
protected long | RECEIVE_PERIOD_MILLIS |
static long |
RECEIVE_PERIOD_MILLIS_AMQPS
Deprecated
as of release 1.2.27 these value is deprecated and replaced by setOption(String optionName, Object value) SetMinimumPollingInterval to change it. The number of milliseconds the transport will wait between polling for messages.
|
static long | RECEIVE_PERIOD_MILLIS_HTTPS |
static long | RECEIVE_PERIOD_MILLIS_MQTT |
static long |
SEND_PERIOD_MILLIS
Deprecated
as of release 1.2.27 this value is deprecated and replaced by setOption(String optionName, Object value) SetSendInterval to change it. The number of milliseconds the transport will wait between sending out messages.
|
static final java.lang.String |
SHARED_ACCESS_KEY_ATTRIBUTE
Deprecated
as of release 1.2.27 this value is deprecated and will not be replaced. The shared access key attribute name in a connection string.
|
static final java.lang.String |
SHARED_ACCESS_TOKEN_ATTRIBUTE
Deprecated
as of release 1.2.27 this value is deprecated and will not be replaced. The shared access signature attribute name in a connection string.
|
Constructor Summary
Modifier | Constructor | Description | |
---|---|---|---|
protected | DeviceClient() | ||
DeviceClient(String connString, IotHubClientProtocol protocol) |
Constructor that takes a connection string as an argument. |
||
DeviceClient(String connString, IotHubClientProtocol protocol, ClientOptions clientOptions) |
Constructor that takes a connection string as an argument. |
||
DeviceClient(String connString, IotHubClientProtocol protocol, String publicKeyCertificate, boolean isCertificatePath, String privateKey, boolean isPrivateKeyPath) |
Deprecated
For x509 authentication, use DeviceClient(String connString, IotHubClientProtocol protocol, ClientOptions clientOptions) and provide an SSLContext instance in the ClientOptions instance. For a sample on how to build this SSLContext, see this code which references a helper class for building SSLContext objects for x509 authentication as well as for SAS based authentication. When not using this deprecated constructor, you can safely exclude the Bouncycastle dependencies that this library declares. See this pom.xml for an example of how to do this.
Constructor that uses x509 authentication for communicating with Iot |
||
DeviceClient(String connString, IotHubClientProtocol protocol, SSLContext sslContext) |
Deprecated
For x509 authentication, use DeviceClient(String connString, IotHubClientProtocol protocol, ClientOptions clientOptions) and provide an SSLContext instance in the ClientOptions instance. For a sample on how to build this SSLContext, see this code which references a helper class for building SSLContext objects for x509 authentication as well as for SAS based authentication. When not using this deprecated constructor, you can safely exclude the Bouncycastle dependencies that this library declares. See this pom.xml for an example of how to do this.
Creates a device client that uses the provided SSLContext for SSL negotiation |
||
DeviceClient(String connString, TransportClient transportClient) |
Deprecated
MultiplexingClient should be used instead of TransportClient for creating all multiplexed connections.
Constructor that takes a connection string and a transport client as an argument. |
||
DeviceClient(String hostName, String deviceId, SasTokenProvider sasTokenProvider, IotHubClientProtocol protocol) |
Constructor that allows for the client's SAS token generation to be controlled by the user. |
||
DeviceClient(String hostName, String deviceId, SasTokenProvider sasTokenProvider, IotHubClientProtocol protocol, ClientOptions clientOptions) |
Constructor that allows for the client's SAS token generation to be controlled by the user. |
Method Summary
Methods inherited from InternalClient
Methods inherited from java.lang.Object
Field Details
CONNECTION_STRING_CHARSET
public static final Charset CONNECTION_STRING_CHARSET
Deprecated
DEVICE_ID_ATTRIBUTE
public static final String DEVICE_ID_ATTRIBUTE
Deprecated
HOSTNAME_ATTRIBUTE
public static final String HOSTNAME_ATTRIBUTE
Deprecated
RECEIVE_PERIOD_MILLIS
protected long RECEIVE_PERIOD_MILLIS
RECEIVE_PERIOD_MILLIS_AMQPS
public static long RECEIVE_PERIOD_MILLIS_AMQPS
Deprecated
RECEIVE_PERIOD_MILLIS_HTTPS
public static long RECEIVE_PERIOD_MILLIS_HTTPS
RECEIVE_PERIOD_MILLIS_MQTT
public static long RECEIVE_PERIOD_MILLIS_MQTT
SEND_PERIOD_MILLIS
public static long SEND_PERIOD_MILLIS
Deprecated
SHARED_ACCESS_KEY_ATTRIBUTE
public static final String SHARED_ACCESS_KEY_ATTRIBUTE
Deprecated
SHARED_ACCESS_TOKEN_ATTRIBUTE
public static final String SHARED_ACCESS_TOKEN_ATTRIBUTE
Deprecated
Constructor Details
DeviceClient
protected DeviceClient()
DeviceClient
public DeviceClient(String connString, IotHubClientProtocol protocol)
Constructor that takes a connection string as an argument.
Parameters:
HostName
, DeviceId
, and SharedAccessKey
.
Throws:
connString
or
protocol
are null
; or if connString
is missing
one of the following attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
connString
or
protocol
are null
; or if connString
is missing
one of the following attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
DeviceClient
public DeviceClient(String connString, IotHubClientProtocol protocol, ClientOptions clientOptions)
Constructor that takes a connection string as an argument.
Parameters:
HostName
, DeviceId
, and SharedAccessKey
.
Throws:
connString
or
protocol
are null
; or if connString
is missing
one of the following attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
connString
or
protocol
are null
; or if connString
is missing
one of the following attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
DeviceClient
public DeviceClient(String connString, IotHubClientProtocol protocol, String publicKeyCertificate, boolean isCertificatePath, String privateKey, boolean isPrivateKeyPath)
Deprecated
Constructor that uses x509 authentication for communicating with IotHub
Parameters:
Throws:
DeviceClient
public DeviceClient(String connString, IotHubClientProtocol protocol, SSLContext sslContext)
Deprecated
Creates a device client that uses the provided SSLContext for SSL negotiation
Parameters:
Throws:
DeviceClient
public DeviceClient(String connString, TransportClient transportClient)
Deprecated
Constructor that takes a connection string and a transport client as an argument.
Parameters:
HostName
, DeviceId
, and SharedAccessKey
.
Throws:
connString
or if connString
is missing one of the following
attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
connString
or if connString
is missing one of the following
attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
connString
or if connString
is missing one of the following
attributes:HostName
, DeviceId
, or
SharedAccessKey
or if the IoT hub hostname does not conform to
RFC 3986 or if the provided connString
is for an x509 authenticated device
DeviceClient
public DeviceClient(String hostName, String deviceId, SasTokenProvider sasTokenProvider, IotHubClientProtocol protocol)
Constructor that allows for the client's SAS token generation to be controlled by the user. Note that options in this client such as setting the SAS token expiry time will throw UnsupportedOperationException since the SDK no longer controls that when this constructor is used.
Parameters:
DeviceClient
public DeviceClient(String hostName, String deviceId, SasTokenProvider sasTokenProvider, IotHubClientProtocol protocol, ClientOptions clientOptions)
Constructor that allows for the client's SAS token generation to be controlled by the user. Note that options in this client such as setting the SAS token expiry time will throw UnsupportedOperationException since the SDK no longer controls that when this constructor is used.
Parameters:
Method Details
<Type1,Type2>startDeviceTwin
public void
Starts the device twin. This device client will receive a callback with the current state of the full twin, including reported properties and desired properties. After that callback is received, this device client will receive a callback each time a desired property is updated. That callback will either contain the full desired properties set, or only the updated desired property depending on how the desired property was changed. IoT Hub supports a PUT and a PATCH on the twin. The PUT will cause this device client to receive the full desired properties set, and the PATCH will cause this device client to only receive the updated desired properties. Similarly, the version of each desired property will be incremented from a PUT call, and only the actually updated desired property will have its version incremented from a PATCH call. The java service client library uses the PATCH call when updated desired properties, but it builds the patch such that all properties are included in the patch. As a result, the device side will receive full twin updates, not partial updates. See PUT and PATCH
Parameters:
null
.
null
.
null
.
null
.
Throws:
null
null
null
close
public void close()
Deprecated
Completes all current outstanding requests and closes the IoT Hub client. Must be called to terminate the background thread that is sending data to IoT Hub. After closeNow()
is called, the IoT Hub client is no longer usable. If the client is already closed, the function shall do nothing.
Overrides:
DeviceClient.close()Throws:
closeNow
public void closeNow()
Closes the IoT Hub client by releasing any resources held by client. When closeNow is called all the messages that were in transit or pending to be sent will be informed to the user in the callbacks and any existing connection to IotHub will be closed. Must be called to terminate the background thread that is sending data to IoT Hub. After closeNow()
is called, the IoT Hub client is no longer usable. If the client is already closed, the function shall do nothing.
Overrides:
DeviceClient.closeNow()Throws:
completeFileUpload
public void completeFileUpload(FileUploadCompletionNotification notification)
Notify IoT Hub that a file upload has been completed, successfully or otherwise. See this documentation for more details.
Parameters:
Throws:
completeFileUploadAsync
public void completeFileUploadAsync(FileUploadCompletionNotification notification)
Deprecated
Notify IoT Hub that a file upload has been completed, successfully or otherwise. See this documentation for more details.
Parameters:
Throws:
createFromSecurityProvider
public static DeviceClient createFromSecurityProvider(String uri, String deviceId, SecurityProvider securityProvider, IotHubClientProtocol protocol)
Creates a device client that uses the provided security provider for authentication.
Parameters:
Returns:
Throws:
createFromSecurityProvider
public static DeviceClient createFromSecurityProvider(String uri, String deviceId, SecurityProvider securityProvider, IotHubClientProtocol protocol, ClientOptions clientOptions)
Creates a device client that uses the provided security provider for authentication.
Parameters:
Returns:
Throws:
getDeviceTwin
public void getDeviceTwin()
Retrieves the twin's latest desired properties
Throws:
getFileUploadSasUri
public FileUploadSasUriResponse getFileUploadSasUri(FileUploadSasUriRequest request)
Get a file upload SAS URI which the Azure Storage SDK can use to upload a file to blob for this device. See this documentation for more details.
Parameters:
Returns:
Throws:
open
public void open()
Starts asynchronously sending and receiving messages from an IoT Hub. If the client is already open, the function shall do nothing.
Overrides:
DeviceClient.open()Throws:
registerConnectionStateCallback
public void registerConnectionStateCallback(IotHubConnectionStateCallback callback, Object callbackContext)
Deprecated
Registers a callback to be executed whenever the connection to the device is lost or established.
Parameters:
null
if no callback is provided.
Throws:
setMessageCallback
public DeviceClient setMessageCallback(MessageCallback callback, Object context)
Sets the message callback.
Parameters:
null
.
null
.
Returns:
Throws:
null
but a context is
passed in.
setOption
public void setOption(String optionName, Object value)
Sets a runtime option identified by parameter optionName
to value
. The options that can be set via this API are: - SetMinimumPollingInterval - this option is applicable only when the transport configured with this client is HTTP. This option specifies the interval in milliseconds between calls to the service checking for availability of new messages. The value is expected to be of type long
. - SetSendInterval - this option is applicable to all protocols. This value sets the period (in milliseconds) that this SDK spawns threads to send queued messages. Even if no message is queued, this thread will be spawned. - SetReceiveInterval - this option is applicable to all protocols in case of HTTPS protocol, this option acts the same as SetMinimumPollingInterval
in case of MQTT and AMQP protocols, this option specifies the interval in milliseconds between spawning a thread that dequeues a message from the SDK's queue of received messages. - SetCertificatePath - this option is applicable only when the transport configured with this client is AMQP. This option specifies the path to the certificate used to verify peer. The value is expected to be of type String
. - SetSASTokenExpiryTime - this option is applicable for HTTP/ AMQP/MQTT. This option specifies the interval in seconds after which SASToken expires. If the transport is already open then setting this option will restart the transport with the updated expiry time, and will use that expiry time length for all subsequently generated sas tokens. The value is expected to be of type long
. - SetHttpsReadTimeout - this option is applicable for HTTPS. This option specifies the read timeout in milliseconds per https request made by this client. By default, this value is 4 minutes. The value is expected to be of type int
. - SetHttpsConnectTimeout - this option is applicable for HTTPS. This option specifies the connect timeout in milliseconds per https request made by this client. By default, this value is 0 (no connect timeout). The value is expected to be of type int
. - SetAmqpOpenAuthenticationSessionTimeout - this option is applicable for AMQP with SAS token authentication. This option specifies the timeout in seconds to wait to open the authentication session. By default, this value is 20 seconds. The value is expected to be of type int
. - SetAmqpOpenDeviceSessionsTimeout - this option is applicable for AMQP. This option specifies the timeout in seconds to open the device sessions. By default, this value is 60 seconds. The value is expected to be of type int
.
Overrides:
DeviceClient.setOption(String optionName, Object value)Parameters:
Throws:
startDeviceTwin
public void startDeviceTwin(IotHubEventCallback deviceTwinStatusCallback, Object deviceTwinStatusCallbackContext, DeviceTwin.TwinPropertiesCallback genericPropertiesCallBack, Object genericPropertyCallBackContext)
Starts the device twin. This device client will receive a callback with the current state of the full twin, including reported properties and desired properties. After that callback is received, this device client will receive a callback each time a desired property is updated. That callback will either contain the full desired properties set, or only the updated desired property depending on how the desired property was changed. IoT Hub supports a PUT and a PATCH on the twin. The PUT will cause this device client to receive the full desired properties set, and the PATCH will cause this device client to only receive the updated desired properties. Similarly, the version of each desired property will be incremented from a PUT call, and only the actually updated desired property will have its version incremented from a PATCH call. The java service client library uses the PATCH call when updated desired properties, but it builds the patch such that all properties are included in the patch. As a result, the device side will receive full twin updates, not partial updates. See PUT and PATCH
Parameters:
null
.
null
.
null
.
null
.
Throws:
null
null
null
startDeviceTwin
public void startDeviceTwin(IotHubEventCallback deviceTwinStatusCallback, Object deviceTwinStatusCallbackContext, DeviceTwin.TwinPropertyCallBack genericPropertyCallBack, Object genericPropertyCallBackContext)
Starts the device twin. This device client will receive a callback with the current state of the full twin, including reported properties and desired properties. After that callback is received, this device client will receive a callback each time a desired property is updated. That callback will either contain the full desired properties set, or only the updated desired property depending on how the desired property was changed. IoT Hub supports a PUT and a PATCH on the twin. The PUT will cause this device client to receive the full desired properties set, and the PATCH will cause this device client to only receive the updated desired properties. Similarly, the version of each desired property will be incremented from a PUT call, and only the actually updated desired property will have its version incremented from a PATCH call. The java service client library uses the PATCH call when updated desired properties, but it builds the patch such that all properties are included in the patch. As a result, the device side will receive full twin updates, not partial updates. See PUT and PATCH
Parameters:
null
.
null
.
null
.
null
. *
Throws:
null
null
null
subscribeToDeviceMethod
public void subscribeToDeviceMethod(DeviceTwin.DeviceMethodCallback deviceMethodCallback, Object deviceMethodCallbackContext, IotHubEventCallback deviceMethodStatusCallback, Object deviceMethodStatusCallbackContext)
Subscribes to device methods
Parameters:
null
.
null
.
null
.
null
.
Throws:
uploadToBlobAsync
public void uploadToBlobAsync(String destinationBlobName, InputStream inputStream, long streamLength, IotHubEventCallback callback, Object callbackContext)
Deprecated
Asynchronously upload a stream to the IoT Hub. NOTE: IotHub does not currently support CA signed devices using file upload. Please use SAS based authentication or self signed certificates.
Parameters:
null
.
Throws:
null
,
empty or not valid, or if the callback is null
.
null
,
empty or not valid, or if the callback is null
.