2

The code below works if the device is online and offline wtihin minutes. However, when it is offline more than 1 hour, and is switched to online, the SetConnectionStatusChangesHandler's handler is not invoked.


    public class IotHubService
    {
        Microsoft.Azure.Devices.Client.DeviceClient _deviceClient;

        public void InitDeviceClient()
        {

            _deviceClient = DeviceClient.Create(_iotHubEndpoint,
                new DeviceAuthenticationWithToken(deviceId, sasToken),
                TransportType.Mqtt_WebSocket_Only);

            var retryPolicy = new MyExponentialBackOff(...);
            _deviceClient.SetRetryPolicy(retryPolicy);

            // Set handlers and callbacks
            _deviceClient.SetConnectionStatusChangesHandler(ConnectionStatusChanged);

            _iotHubCancellationTokenSource = new CancellationTokenSource();
            await _deviceClient.OpenAsync(_iotHubCancellationTokenSource.Token);

        }

        //this does NOT get invoked if the device is offline for more than one hour, and switched to online
        public async void ConnectionStatusChanged(ConnectionStatus status, ConnectionStatusChangeReason reason)
        {

        }

    }

    public class MyExponentialBackOff : IRetryPolicy
    {
        public bool ShouldRetry(int currentRetryCount, Exception lastException, out TimeSpan retryInterval)
        {
            if (currentRetryCount < _retryCount)  //_retryCounty is int.MaxValue
            {
                retryInterval = TimeSpan.FromMilliseconds(10000);

                return true;
            }

            retryInterval = TimeSpan.Zero;
            return false;
        }


    }

Microsoft.Azure.Devices.Client: Version="1.21.0"

Xamarin Android Lollipop

Pingpong
  • 7,681
  • 21
  • 83
  • 209

0 Answers0