Ejemplo n.º 1
0
Archivo: Device.cs Proyecto: arionlos/1
 protected async Task SetStateAsync(DeviceState newState, bool isError)
 {
     await _uiThreadService.RunOnMainThread(() =>
     {
         var oldState = DeviceState;
         DeviceState  = newState;
         DeviceStateChanged?.Invoke(this, new DeviceStateChangedEventArgs(oldState, newState, isError));
     });
 }
Ejemplo n.º 2
0
 private void OnDeviceDisconnected(Device device)
 {
     _uIThreadService.RunOnMainThread(() =>
     {
         if (!_isDisappearing)
         {
             _connectionTask = ConnectAsync();
         }
     });
 }
Ejemplo n.º 3
0
        public async Task <bool> ScanAsync(CancellationToken token)
        {
            using (await _asyncLock.LockAsync())
            {
                IsScanning = true;

                try
                {
                    var infraScan     = _infraredDeviceManager.ScanAsync(FoundDevice, token);
                    var bluetoothScan = _bluetoothDeviceManager.ScanAsync(FoundDevice, token);

                    await Task.WhenAll(infraScan, bluetoothScan);

                    return(infraScan.Result && bluetoothScan.Result);
                }
                catch (Exception)
                {
                    return(false);
                }
                finally
                {
                    IsScanning = false;
                }
            }

            async Task FoundDevice(DeviceType deviceType, string deviceName, string deviceAddress, byte[] deviceData)
            {
                using (await _foundDeviceLock.LockAsync())
                {
                    if (Devices.Any(d => d.DeviceType == deviceType && d.Address == deviceAddress))
                    {
                        return;
                    }

                    var device = _deviceFactory(deviceType, deviceName, deviceAddress, deviceData);
                    if (device != null)
                    {
                        await _deviceRepository.InsertDeviceAsync(device.DeviceType, device.Name, device.Address, deviceData);

                        await _uiThreadService.RunOnMainThread(() => Devices.Add(device));
                    }
                }
            }
        }