예제 #1
0
        public async Task <string> ConnectToDeviceAsync(string uniqueID, string deviceID)
        {
            //Send a message to the app service
            var message = new ValueSet();

            message.Add(Commands.Command, Commands.Connect);
            message.Add(Parameters.DeviceID, deviceID);
            message.Add(Parameters.UniqueID, uniqueID);

            var responseMessage = await SendMessageAsync(message);

            // check the response message
            if (responseMessage.ContainsKey(Parameters.Result) != true)
            {
                throw new Exception($"The app service response message does not contain a key called \"{Parameters.Result}\"");
            }
            if (responseMessage.ContainsKey(Parameters.Details) != true)
            {
                throw new Exception($"The app service response message does not contain a key called \"{Parameters.Details}\"");
            }

            var result = (bool)responseMessage[Parameters.Result];
            var detail = (string)responseMessage[Parameters.Details];

            if (result != true)
            {
                throw new Exception(detail);
            }

            ConnectedBluetoothLEDeviceInformation = JsonConvert.DeserializeObject <BluetoothLEDeviceInformation>(detail);
            uniqueID = (string)responseMessage[Parameters.UniqueID];

            return(uniqueID);
        }
예제 #2
0
        public Task CloseAsync()
        {
            ConnectedBluetoothLEDeviceInformation = null;
            IsOpened = false;

            _appServiceConnection.RequestReceived -= OnRequestReceived;
            _appServiceConnection.Dispose();

            return(Task.CompletedTask);
        }
예제 #3
0
 private void OnDeviceDisconnected(IBluetoothLEDevice bluetoothLEDevice)
 {
     Task.Run(async() =>
     {
         using (await _asyncLock.LockAsync())
         {
             await DisconnectInternalAsync();
             _onDeviceDisconnected?.Invoke(this);
         }
     });
 }
예제 #4
0
        public async override Task <DeviceConnectionResult> ConnectAsync(CancellationToken token)
        {
            using (await _asyncLock.LockAsync())
            {
                if (_bleDevice != null || DeviceState != DeviceState.Disconnected)
                {
                    return(DeviceConnectionResult.Error);
                }

                try
                {
                    _bleDevice = _bleService.GetKnownDevice(Address);
                    if (_bleDevice == null)
                    {
                        return(DeviceConnectionResult.Error);
                    }

                    _bleDevice.Disconnected += OnDeviceDisconnected;

                    await SetStateAsync(DeviceState.Connecting, false);

                    var services = await _bleDevice.ConnectAndDiscoverServicesAsync(token);

                    token.ThrowIfCancellationRequested();

                    if (ProcessServices(services))
                    {
                        await StartOutputTaskAsync();

                        token.ThrowIfCancellationRequested();


                        await SetStateAsync(DeviceState.Connected, false);

                        return(DeviceConnectionResult.Ok);
                    }
                }
                catch (OperationCanceledException)
                {
                    await DisconnectInternalAsync(false);

                    return(DeviceConnectionResult.Canceled);
                }
                catch (Exception)
                {
                }

                await DisconnectInternalAsync(true);

                return(DeviceConnectionResult.Error);
            }
        }
예제 #5
0
        private async Task DisconnectInternalAsync()
        {
            if (_bleDevice != null)
            {
                await StopOutputTaskAsync();

                DeviceState = DeviceState.Disconnecting;
                _bleDevice.Disconnect();
                _bleDevice = null;
            }

            DeviceState = DeviceState.Disconnected;
        }
예제 #6
0
        private async Task DisconnectInternalAsync(bool isError)
        {
            if (_bleDevice != null)
            {
                await StopOutputTaskAsync();
                await SetStateAsync(DeviceState.Disconnecting, isError);

                _bleDevice.Disconnected -= OnDeviceDisconnected;
                _bleDevice?.Disconnect();
                _bleDevice = null;
            }

            await SetStateAsync(DeviceState.Disconnected, isError);
        }
예제 #7
0
 private void OnServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
 {
     ConnectedBluetoothLEDeviceInformation = null;
     IsOpened = false;
 }
예제 #8
0
        public async override Task <DeviceConnectionResult> ConnectAsync(
            bool reconnect,
            Action <Device> onDeviceDisconnected,
            IEnumerable <ChannelConfiguration> channelConfigurations,
            bool startOutputProcessing,
            bool requestDeviceInformation,
            CancellationToken token)
        {
            using (await _asyncLock.LockAsync())
            {
                if (_bleDevice != null || DeviceState != DeviceState.Disconnected)
                {
                    return(DeviceConnectionResult.Error);
                }

                _onDeviceDisconnected = onDeviceDisconnected;

                try
                {
                    _bleDevice = _bleService.GetKnownDevice(Address);
                    if (_bleDevice == null)
                    {
                        return(DeviceConnectionResult.Error);
                    }

                    DeviceState = DeviceState.Connecting;
                    var services = await _bleDevice.ConnectAndDiscoverServicesAsync(
                        reconnect || AutoConnectOnFirstConnect,
                        OnCharacteristicChanged,
                        OnDeviceDisconnected,
                        token);

                    token.ThrowIfCancellationRequested();

                    if (await ValidateServicesAsync(services, token) &&
                        await AfterConnectSetupAsync(requestDeviceInformation, token))
                    {
                        if (startOutputProcessing)
                        {
                            await StartOutputTaskAsync();
                        }

                        token.ThrowIfCancellationRequested();

                        DeviceState = DeviceState.Connected;
                        return(DeviceConnectionResult.Ok);
                    }
                }
                catch (OperationCanceledException)
                {
                    await DisconnectInternalAsync();

                    return(DeviceConnectionResult.Canceled);
                }
                catch (Exception)
                {
                }

                await DisconnectInternalAsync();

                return(DeviceConnectionResult.Error);
            }
        }