コード例 #1
0
        public void ConfigureViewModel()
        {
            var devices = _bluetoothService.GetBondedDevices();

            foreach (var device in devices)
            {
                if (!BluetoothDevices.Any(d => d.Name == device.Name && d.Mac == device.Mac))
                {
                    BluetoothDevices.Add(new BluetoothDevice(device.Name, device.Mac));
                }
            }
        }
コード例 #2
0
        public void SearchForDevices()
        {
            BluetoothDevices.Clear();
            localEndPoint = new BluetoothEndPoint(CurrentAdapter.LocalAddress, BluetoothService.SerialPort);
            client        = new BluetoothClient(localEndPoint);
            foreach (BluetoothDeviceInfo device in client.DiscoverDevices())
            {
                BluetoothDevices.Add(device);
            }

            if (BluetoothDevices.Any())
            {
                ChosenDevice = BluetoothDevices[0];
            }
            else
            {
                MessageBox.Show("No bluetooth devices were found", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
コード例 #3
0
        public void GetConnectedDevices()
        {
            _connectedDisposable = _centralManager.GetConnectedPeripherals().Subscribe(scanResult =>
            {
                scanResult.ToList().ForEach(
                    item =>
                {
                    if (!string.IsNullOrEmpty(item.Name))
                    {
                        BluetoothDevices.Add(item);
                    }
                });

                _connectedDisposable?.Dispose();
            });

            if (_centralManager.IsScanning)
            {
                _centralManager.StopScan();
            }
        }
コード例 #4
0
        public void SearchForDevices()
        {
            // urządzenia znalezione w poprzednim wyszukiwaniu nie muszą być aktywne przy aktualnym wyszukaniu
            BluetoothDevices.Clear();

            // utworzenie endpointu (adres hosta i port usługi (emulacja portu szeregowego))
            localEndPoint = new BluetoothEndPoint(CurrentAdapter.LocalAddress, BluetoothService.SerialPort);
            client        = new BluetoothClient(localEndPoint);

            // przepisanie znalezionych urządzeń za pomocą client.DiscoverDevices() do BluetoothDevice
            foreach (BluetoothDeviceInfo device in client.DiscoverDevices())
            {
                BluetoothDevices.Add(device);
            }

            if (BluetoothDevices.Any())
            {
                ChosenDevice = BluetoothDevices[0];
            }
            else
            {
                MessageBox.Show("Nie znaleziono urządzeń bluetooth", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
コード例 #5
0
        private void GetDeviceList()
        {
            if (_centralManager.IsScanning)
            {
                _scanDisposable?.Dispose();
            }
            else
            {
                if (_centralManager.Status == Shiny.AccessState.Available && !_centralManager.IsScanning)
                {
                    _scanDisposable = _centralManager.ScanForUniquePeripherals().Subscribe(scanResult =>
                    {
                        IsSelected = false;

                        if (!string.IsNullOrEmpty(scanResult.Name) && !BluetoothDevices.Contains(scanResult))
                        {
                            BluetoothDevices.Add(scanResult);
                        }
                    });
                }
            }

            IsScanning = _centralManager.IsScanning;
        }
コード例 #6
0
        /// <summary>
        /// Start the Device Watcher and set callbacks to handle devices appearing and disappearing
        /// </summary>
        public void StartWatcher()
        {
            //ProtocolSelectorInfo protocolSelectorInfo;
            string aqsFilter = @"System.Devices.Aep.ProtocolId:=""{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}"" OR System.Devices.Aep.ProtocolId:=""{bb7bb05e-5972-42b5-94fc-76eaa7084d49}""";  //Bluetooth + BluetoothLE

            // Request the IsPaired property so we can display the paired status in the UI
            string[] requestedProperties = { "System.Devices.Aep.IsPaired" };

            //// Get the device selector chosen by the UI, then 'AND' it with the 'CanPair' property
            //protocolSelectorInfo = (ProtocolSelectorInfo)selectorComboBox.SelectedItem;
            //aqsFilter = protocolSelectorInfo.Selector + " AND System.Devices.Aep.CanPair:=System.StructuredQueryType.Boolean#True";

            deviceWatcher = DeviceInformation.CreateWatcher(
                aqsFilter,
                requestedProperties,
                DeviceInformationKind.AssociationEndpoint
                );

            // Hook up handlers for the watcher events before starting the watcher

            handlerAdded = new TypedEventHandler <DeviceWatcher, DeviceInformation>(async(watcher, deviceInfo) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    BluetoothDevices.Add(new BluetoothDeviceInformationDisplay(deviceInfo));
                });
            });
            deviceWatcher.Added += handlerAdded;

            handlerUpdated = new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    // Find the corresponding updated DeviceInformation in the collection and pass the update object
                    // to the Update method of the existing DeviceInformation. This automatically updates the object
                    // for us.
                    foreach (BluetoothDeviceInformationDisplay deviceInfoDisp in BluetoothDevices)
                    {
                        if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                        {
                            deviceInfoDisp.Update(deviceInfoUpdate);
                            break;
                        }
                    }
                });
            });
            deviceWatcher.Updated += handlerUpdated;

            handlerRemoved = new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    // Find the corresponding DeviceInformation in the collection and remove it
                    foreach (BluetoothDeviceInformationDisplay deviceInfoDisp in BluetoothDevices)
                    {
                        if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                        {
                            BluetoothDevices.Remove(deviceInfoDisp);
                            break;
                        }
                    }
                });
            });
            deviceWatcher.Removed += handlerRemoved;

            handlerEnumCompleted = new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) =>
            {
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    // Finished enumerating
                    StackLoadingVisibility = false;

                    ResourceLoader loader = ResourceLoader.GetForCurrentView();

                    var index = ConfirmationText.IndexOf(loader.GetString("BluetoothOn/Text"));
                    if (index != -1)
                    {
                        DisplayMessagePanelAsync(ConfirmationText.Substring(0, index), MessageType.InformationalMessage, true);
                    }
                });
            });
            deviceWatcher.EnumerationCompleted += handlerEnumCompleted;

            handlerStopped = new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) =>
            {
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    // Device watcher stopped
                    StackLoadingVisibility = false;
                });
            });
            deviceWatcher.Stopped += handlerStopped;

            // Start the Device Watcher
            deviceWatcher.Start();
            StackLoadingVisibility = true;
        }