eekinci
eekinci

Reputation: 839

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

Upvotes: 0

Views: 1272

Answers (2)

Roy Ben Shabat
Roy Ben Shabat

Reputation: 386

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);
    }
}

Upvotes: 1

Tobias Kullblikk
Tobias Kullblikk

Reputation: 226

This is your closest bet: http://blog.falafel.com/bluetooth-rfcomm-communication-with-a-windows-iot-core-device/

It's a fairly old post since the term UWA is being used, but the concept is what you are looking for.

Use this code as the starting point and migrate it over to UWP to be able to use the Bluetooth Rfcomm Windows Runtime API: https://code.msdn.microsoft.com/windowsapps/Bluetooth-Rfcomm-Chat-afcee559

Edit: Here is the documentation: https://msdn.microsoft.com/en-us/library/windows/apps/mt270289.aspx

Upvotes: 0

Related Questions