コード例 #1
0
        private async Task <bool> ConnectDevice(DeviceInformation deviceInfo = null)
        {
            if (connected)
            {
                RemoveValueChangedHandler();
                if (await ClearBluetoothLEDeviceAsync())
                {
                    connected = false;
                }
            }
            connecting = true;
            bool noexp = false;

            if (deviceInfo == null)
            {
                deviceInfo = dict[deviceQueue.Dequeue()];
                noexp      = true;
            }
            if (deviceInfo.IsEnabled)
            {
                Console.Write("Enabled");
            }
            var recon = (bluetoothLeDevice != null && bluetoothLeDevice.Equals(deviceInfo));

            Update_DeviceName($"Connecting to {deviceInfo.Name}", 1);
            if (bluetoothLeDevice == null)
            {
                bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
            }
            else
            {
                var devinfolock = deviceInfo;
                if (await ClearBluetoothLEDeviceAsync())
                {
                    bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);

                    Update_DeviceList();
                }
            }


            if (bluetoothLeDevice != null)
            {
                bluetoothLeDevice.ConnectionStatusChanged += ConnectionChanged;
            }
            else
            {
                if (noexp)
                {
                    deviceQueue.Enqueue(deviceInfo.Name);
                }
                connecting = false;
                Update_DeviceName("<Disconnected>", 2);
                Console.WriteLine("Connection Failed");
                return(false);
            }


            if (deviceInfo.Pairing.IsPaired)
            {
                await System.Windows.Application.Current.Dispatcher.InvokeAsync(async() =>
                {
                    await deviceInfo.Pairing.UnpairAsync();
                    Process.Start(System.Windows.Application.ResourceAssembly.Location);
                    System.Windows.Application.Current.Shutdown();
                });

                return(false);
                //gattgot = await GetGatt(deviceInfo);
            }
            else if (deviceInfo.Pairing.CanPair)
            {
                if (subscribedForNotifications)
                {
                    RemoveValueChangedHandler();
                }

                GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

                if (result.Status == GattCommunicationStatus.Success)
                {
                    Update_DeviceName($"Subscribing to {deviceInfo.Name}", 1);
                    connected  = true;
                    connecting = false;
                    return(await GetGatt(result));
                }
                else
                {
                    await ClearBluetoothLEDeviceAsync();

                    Update_DeviceName(deviceInfo.Name + " is unreachable", 2);
                    if (noexp)
                    {
                        deviceQueue.Enqueue(deviceInfo.Name);
                    }
                    connecting = false;
                    ConnectDevice();
                    return(false);
                }
            }
            else
            {
                bluetoothLeDevice.ConnectionStatusChanged -= ConnectionChanged;
                bluetoothLeDevice?.Dispose();
                if (noexp && deviceInfo.Name != string.Empty)
                {
                    deviceQueue.Enqueue(deviceInfo.Name);
                }
                Update_DeviceName(deviceInfo.Name + " is unreachable", 2);
                connecting = false;
            }
            return(false);
        }