UWP - 在下拉列表中获取蓝牙设备列表

时间:2017-12-09 05:11:49

标签: c# uwp uwp-xaml

我正在尝试将配对的蓝牙设备列表放入我的UWP上的下拉列表中。我知道我需要使用FindAllAsync类,我认为我说得对,但由于某种原因,我无法得到列表来显示BT设备。

贝娄是我的代码,请你指出我在这里缺少的内容:

namespace ArduinoRobotControl
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    private DeviceInformationCollection deviceCollection;
    private DeviceInformation selectedDevice;
    private RfcommDeviceService deviceService;

    public string deviceName = "Dev B"; // Specify the device name to be selected; You can find the device name from the webb under bluetooth 

    StreamSocket streamSocket = new StreamSocket();

    public MainPage()
    {
        this.InitializeComponent();
        InitializeRfcommServer();

    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //var selector = BluetoothDevice.GetDeviceSelector();
        //var BTDevinfo = DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
        var BTDevinfo =  DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort), new string[] { "System.Devices.AepService.AepId" });
        selectorComboBox.ItemsSource = deviceCollection;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ConnectToDevice();
    }

    private async void InitializeRfcommServer()
    {
        try
        {
            string device1 = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
            deviceCollection = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(device1);

        }
        catch (Exception exception)
        {

            errorStatus.Visibility = Visibility.Visible;
            errorStatus.Text = exception.Message;
        }
    }

    private async void ConnectToDevice()
    {
        foreach (var item in deviceCollection)
        {
            if (item.Name == deviceName)
            {
                selectedDevice = item;
                break;
            }
        }

        if (selectedDevice == null)
        {
            errorStatus.Visibility = Visibility.Visible;
            errorStatus.Text = "Cannot find the device specified; Please check the device name";
            return;
        }
        else
        {
            deviceService = await RfcommDeviceService.FromIdAsync(selectedDevice.Id);

            if (deviceService != null)
            {
                //connect the socket   
                try
                {
                    await streamSocket.ConnectAsync(deviceService.ConnectionHostName, deviceService.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    errorStatus.Visibility = Visibility.Visible;
                    errorStatus.Text = "Cannot connect bluetooth device:" + ex.Message;
                }

            }
            else
            {
                errorStatus.Visibility = Visibility.Visible;
                errorStatus.Text = "Didn't find the specified bluetooth device";
            }
        }



    }

我认为问题出在以下几个方面:

        var BTDevinfo =  DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
        selectorComboBox.ItemsSource = deviceCollection;

你有什么建议吗? 谢谢。

1 个答案:

答案 0 :(得分:2)

我无法理解为什么在selectorComboBox.ItemsSource处理程序中指定ComboBox_SelectionChanged?当前选定的项目更改时会发生此事件。为什么不在selectorComboBox.ItemsSource方法中指定InitializeRfcommServer

例如:

private async void InitializeRfcommServer()
{
    try
    {
        string device1 = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
        deviceCollection = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(device1);
        selectorComboBox.ItemsSource = deviceCollection;
    }
    catch (Exception exception)
    {

        errorStatus.Visibility = Visibility.Visible;
        errorStatus.Text = exception.Message;
    }
}

这里是组合框的XAML代码,我试图拉动设备列表:

        <ComboBox x:Name="selectorComboBox" 
                          Margin="845,533,0,0"
                          HorizontalAlignment="Left" 
                          SelectionChanged="selectorComboBox_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

[2017年12月15日更新]

您需要在&#34; ComboBox.ItemTemplate&#39;中绑定DeviceInformation对象的Name projerty。请参阅上面我编辑的XAML代码。

此外,您似乎对Binding不太熟悉。我建议您先阅读Binding相关文档。见Data binding overview

以下是MS官方代码示例Device enumeration and pairing sample,供您参考。