Windows IoT核心蓝牙配对

时间:2016-02-21 10:08:38

标签: bluetooth windows-10-iot-core

是否可以通过编程方式在Windows IoT Core下配置蓝牙设备?我该怎么办?非常适合展示可用的设备。

我知道的唯一方法是http://minwinpc:8080

2 个答案:

答案 0 :(得分:1)

我使用可观察的配对和非配对设备集合编写了一个用于配对蓝牙设备的小包装器。您可以轻松地将它们绑定到视图中的列表框。

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

答案 1 :(得分:0)

这是您最接近的赌注:http://blog.falafel.com/bluetooth-rfcomm-communication-with-a-windows-iot-core-device/

自从使用UWA一词以来,这是一个相当古老的帖子,但这个概念正是您所寻找的。

使用此代码作为起点并将其迁移到UWP,以便能够使用蓝牙Rfcomm Windows运行时API:https://code.msdn.microsoft.com/windowsapps/Bluetooth-Rfcomm-Chat-afcee559

编辑:以下是文档:https://msdn.microsoft.com/en-us/library/windows/apps/mt270289.aspx