示例#1
0
        public async Task <ConnectResult> Connect(string strDeviceID, NukiConnectionConfig connectionInfo)
        {
            ConnectResult result = ConnectResult.Failed;

            try
            {
                if (connectionInfo != null)
                {
                    m_connectionInfo = connectionInfo;
                }
                var deviceService = await GattDeviceService.FromIdAsync(strDeviceID);

                if (deviceService != null)
                {
                    foreach (var character in
                             deviceService.GetCharacteristics(KeyTurnerUGDIO.Value).
                             Concat(deviceService.GetCharacteristics(KeyTurnerPairingGDIO.Value)))
                    {
                        if (character.Uuid == KeyTurnerPairingGDIO.Value)
                        {
                            result = ConnectResult.Successfull;
                            m_pairingGDIO.SetConnection(character);
                        }
                        else if (character.Uuid == KeyTurnerUGDIO.Value)
                        {
                            result = ConnectResult.Successfull;
                            m_UGDIO.SetConnection(character);
                        }
                    }
                    if (m_bleDevice != null)
                    {
                        m_bleDevice.ConnectionStatusChanged -= M_bleDevice_ConnectionStatusChanged;
                    }
                    m_bleDevice = deviceService.Device;
                    m_bleDevice.ConnectionStatusChanged += M_bleDevice_ConnectionStatusChanged;
                    RaisePropertyChanged(nameof(Connected));
                }
                else
                {
                    Log.Warn($"Unable to get GattDeviceService.FromIdAsync(\"{strDeviceID}\");");
                }

                if (m_bleDevice?.ConnectionStatus == BluetoothConnectionStatus.Connected)
                {
                    result = ConnectResult.Connected;
                }
            }
            catch (Exception ex)
            {
                Log.Error("Connect failed: {0}", ex);
                if (ex is InvalidOperationException && (uint)ex.HResult == 0x8000000E)
                {
                    result = ConnectResult.NeedRepair;
                }
                else
                {
                }
            }
            return(result);
        }
示例#2
0
        private NukiConnectionConfig[] LoadBluetoothConnectionInfo()
        {
            NukiConnectionConfig[] retValues = new NukiConnectionConfig[0];
            try
            {
                string strLockSettings = _helper.Read <string>(nameof(PairdLocks), string.Empty);
                if (!string.IsNullOrEmpty(strLockSettings))
                {
                    using (MemoryStream mem = new MemoryStream())
                    {
                        using (StreamWriter w = new StreamWriter(mem, Encoding.UTF8, 128, true))
                            w.Write(strLockSettings);

                        mem.Position = 0;
                        retValues    = s_XmlSerializer.Deserialize(mem) as NukiConnectionConfig[];
                    }
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                Log.Error("Load BluetoothConnectionInfo failed: {0}", ex);
            }
            return(retValues);
        }
示例#3
0
 public Task <bool> Connect(NukiConnectionConfig connectionInfo)
 {
     if (connectionInfo == null)
     {
         throw new ArgumentNullException(nameof(connectionInfo));
     }
     return(Connect(connectionInfo.DeviceName));
 }
示例#4
0
        public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary <string, object> state)
        {
            ShowProgressbar(true);

            NukiConnectionConfig = SettingsService.Instance.PairdLocks.Where((l) => l.UniqueClientID.Value == parameter as uint?).FirstOrDefault();
            await TryToConnect();

            ShowProgressbar(false);
            await base.OnNavigatedToAsync(parameter, mode, state);
        }
        public async Task <NukiConnectResult> TryConnect(NukiConnectionConfig connectionInfo, Func <Action, IAsyncAction> dispatch, int nTimeout = 3000)
        {
            bool            blnRet     = false;
            INukiConnection connection = null;

            var connectTask = TryConnect(connectionInfo, dispatch);

            var completedTask = await Task.WhenAny(connectTask, Task.Delay(nTimeout));

            if (completedTask == connectTask)
            {
                if (connectTask.IsFaulted)
                {
                    Log.Error($"Connection to {connectionInfo.DeviceName} failed", connectTask.Exception);
                }
                else if (connectTask.IsCanceled)
                {
                    Log.Warn($"Connection to {connectionInfo.DeviceName} cancled");
                }
                else if (connectTask.IsCompleted)
                {
                    connection = connectTask.Result;
                    blnRet     = connection != null;
                }
                else
                {
                    Log.Fatal($"Connection to {connectionInfo.DeviceName} abnormal");
                }
            }
            else
            {
                Log.Warn($"Connection to {connectionInfo.DeviceName} timedout");
            }


            return(new NukiConnectResult(blnRet, connection));
        }
        private Task <INukiConnection> TryConnect(NukiConnectionConfig connectionInfo, Func <Action, IAsyncAction> dispatch)
        {
            return(Task.Run(async() =>
            {
                INukiConnection retValue = null;
                TaskCompletionSource <INukiConnection> taskCompletionSource = new TaskCompletionSource <INukiConnection>();

                DeviceWatcher s_Watcher = null;
                TypedEventHandler <DeviceWatcher, DeviceInformation> OnBLEAdded = null;
                try
                {
                    OnBLEAdded = async(watcher, deviceInfo) =>
                    {
                        await dispatch(async() =>
                        {
                            if (deviceInfo.Name == connectionInfo.DeviceName)
                            {
                                var connection = BluetoothConnection.Connections[deviceInfo.Name];
                                var result = await connection.Connect(deviceInfo.Id, connectionInfo);

                                if (result >= BluetoothConnection.ConnectResult.Successfull)
                                {
                                    Log.Info("Connected to: " + deviceInfo.Id + ", Name: " + deviceInfo.Name);
                                    taskCompletionSource.TrySetResult(connection);
                                }
                                else
                                {
                                    taskCompletionSource.TrySetCanceled();
                                }
                            }
                            else
                            {
                            }
                        });
                    };
                    string[] requestedProperties = { "System.Devices.Aep.IsPaired" };

                    //System.Devices.Aep.IsPaired
                    string aqs = $"(System.ItemNameDisplay:=\"{connectionInfo.DeviceName}\")";
                    string strService = GattDeviceService.GetDeviceSelectorFromUuid(BluetoothConnection.KeyTurnerService.Value);
                    //for bluetooth LE Devices

                    aqs = $"({strService}) AND {aqs}";

                    s_Watcher = DeviceInformation.CreateWatcher(aqs, requestedProperties);
                    s_Watcher.Added += OnBLEAdded;
                    s_Watcher.Stopped += async(s, o) =>
                    {
                        await dispatch(async() =>
                        {
                            await Task.Delay(1000);
                            if (!taskCompletionSource.Task.IsCompleted)
                            {
                                taskCompletionSource.TrySetCanceled();
                            }
                        });
                    };

                    s_Watcher.Start();

                    retValue = await taskCompletionSource.Task;
                }
                finally
                {
                    s_Watcher.Added -= OnBLEAdded;

                    if (DeviceWatcherStatus.Started == s_Watcher.Status ||
                        DeviceWatcherStatus.EnumerationCompleted == s_Watcher.Status)
                    {
                        s_Watcher.Stop();
                    }
                }

                return retValue;
            }));
        }
示例#7
0
 public BluetoothPairResult(BlutoothPairStatus status, NukiConnectionConfig connectionInfo)
 {
     Status         = status;
     ConnectionInfo = connectionInfo;
 }