@long dao thanh Thanks for reaching out!
You haven't shared any piece of code to check whether your method is reliable or not. You have some options to check the device connections.
I would suggest you start with the below documentations:
- Order device connection events from Azure IoT Hub using Azure Cosmos DB
- Monitor the health of Azure IoT Hub and diagnose problems quickly
- Device Heartbeat
The IoT Hub identity registry contains a field called connectionState. Only use the connectionState field during development and debugging. IoT solutions should not query the field at run time. For example, do not query the connectionState field to check if a device is connected before you send a cloud-to-device message or an SMS. We recommend subscribing to the device disconnected event on Event Grid to get alerts and monitor the device connection state. Use this tutorial to learn how to integrate Device Connected and Device Disconnected events from IoT Hub in your IoT solution.
If your IoT solution needs to know if a device is connected, you can implement the heartbeat pattern. In the heartbeat pattern, the device sends device-to-cloud messages at least once every fixed amount of time (for example, at least once every hour). Therefore, even if a device does not have any data to send, it still sends an empty device-to-cloud message (usually with a property that identifies it as a heartbeat). On the service side, the solution maintains a map with the last heartbeat received for each device. If the solution does not receive a heartbeat message within the expected time from the device, it assumes that there is a problem with the device.
Regarding your specific questions, please share more details incase if that doesn't help. Kindly check the above details and let us know if you have further queries.