Search code examples
bluetoothwindows-10-iot-core

Windows IoT Core Bluetooth Pairing


Is it possibe to paire a bluetooth device under Windows IoT Core programmatically? And how do I? Would be perfect to show aviable devices.

The only way I know is http://minwinpc:8080


Solution

  • I wrote a small wrapper for pairing bluetooth devices using observable collections of paired and unpaired devices. You can easily bind those to list boxes on your view.

    BluetoothSerice.cs

    public class BluetoothService
    {
        private TaskScheduler _threadContext;
        private DeviceWatcher _watcherPaired;
        private DeviceWatcher _watcherUnpaired;
    
        private ObservableCollection<DeviceInformation> _pairedDevices;
        public ObservableCollection<DeviceInformation> PairedDevices
        {
            get { return _pairedDevices; }
            set { _pairedDevices = value; }
        }
    
        private ObservableCollection<DeviceInformation> _unpairedDevices;
        public ObservableCollection<DeviceInformation> UnpairedDevices
        {
            get { return _unpairedDevices; }
            set { _unpairedDevices = value; }
        }
    
        public event EventHandler RefreshCompleted;
    
        public BluetoothService()
        {
            _threadContext = TaskScheduler.FromCurrentSynchronizationContext();
            PairedDevices = new ObservableCollection<DeviceInformation>();
            UnpairedDevices = new ObservableCollection<DeviceInformation>();
        }
    
        public void Refresh()
        {
            PairedDevices.Clear();
            UnpairedDevices.Clear();
    
            var selectorPaired = BluetoothDevice.GetDeviceSelectorFromPairingState(true);
            _watcherPaired = DeviceInformation.CreateWatcher(selectorPaired, null, DeviceInformationKind.AssociationEndpoint);
    
            var selectorUnpaired = BluetoothDevice.GetDeviceSelectorFromPairingState(false);
            _watcherUnpaired = DeviceInformation.CreateWatcher(selectorUnpaired, null, DeviceInformationKind.AssociationEndpoint);
    
    
            _watcherPaired.EnumerationCompleted += _watcherPaired_EnumerationCompleted;
            _watcherPaired.Added += _watcherPaired_Added;
            _watcherPaired.Removed += _watcherPaired_Removed;
    
            _watcherUnpaired.EnumerationCompleted += _watcherUnpaired_EnumerationCompleted;
            _watcherUnpaired.Added += _watcherUnpaired_Added;
            _watcherUnpaired.Removed += _watcherUnpaired_Removed;
    
            _watcherPaired.Start();
            _watcherUnpaired.Start();
        }
    
        public async Task<DevicePairingResult> PairAsync(DeviceInformation device)
        {
            return await device.Pairing.PairAsync(DevicePairingProtectionLevel.None);
        }
    
        public async Task<DeviceUnpairingResult> UnpairAsync(DeviceInformation device)
        {
            return await device.Pairing.UnpairAsync();
        }
    
        private void _watcherUnpaired_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            InvokeThread(() =>
            {
                var matched = UnpairedDevices.SingleOrDefault(x => x.Id == args.Id);
                if (matched != null)
                {
                    UnpairedDevices.Remove(matched);
                }
            });
        }
    
        private void _watcherUnpaired_Added(DeviceWatcher sender, DeviceInformation args)
        {
            InvokeThread(() =>
            {
                var matched = UnpairedDevices.SingleOrDefault(x => x.Id == args.Id);
    
                if (matched != null)
                {
                    UnpairedDevices.Remove(matched);
                }
    
                UnpairedDevices.Add(args);
            });
        }
    
        private void _watcherUnpaired_EnumerationCompleted(DeviceWatcher sender, object args)
        {
            _watcherUnpaired.Stop();
    
            RefreshCompleted?.Invoke(this, new EventArgs());
        }
    
        private void _watcherPaired_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            InvokeThread(() =>
            {
                var matched = PairedDevices.SingleOrDefault(x => x.Id == args.Id);
                if (matched != null)
                {
                    PairedDevices.Remove(matched);
                }
            });
        }
    
        private void _watcherPaired_Added(DeviceWatcher sender, DeviceInformation args)
        {
            InvokeThread(() =>
            {
                var matched = PairedDevices.SingleOrDefault(x => x.Id == args.Id);
    
                if (matched != null)
                {
                    PairedDevices.Remove(matched);
                }
    
                PairedDevices.Add(args);
            });
        }
    
        private void _watcherPaired_EnumerationCompleted(DeviceWatcher sender, object args)
        {
            _watcherPaired.Stop();
        }
    
        private void InvokeThread(Action action)
        {
            Task.Factory.StartNew(() =>
            {
                action();
            }, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, _threadContext);
        }
    }