2017-12-09 37 views
0

ペアリングされたBluetoothデバイスのリストを自分のUWPのドロップダウンリストに表示しようとしています。私はFindAllAsyncクラスを使う必要があることを知っています。私はそれが正しいと思っていましたが、なんらかの理由でBTデバイスを表示するリストを取得できません。UWP - ドロップダウンリストでBluetoothデバイスのリストを取得する

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

ComboBox_SelectionChangedハンドラにselectorComboBox.ItemsSourceを指定する理由を理解できません。このイベントは、現在選択されている項目が変更されたときに発生します。 InitializeRfcommServer方法でselectorComboBox.ItemsSourceを指定しないのはなぜですか?例えば

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日に更新しました]

"ComboBox.ItemTemplate"にDeviceInformationオブジェクトの名前プロジェクタをバインドする必要があります。ab私の編集したXAMLコードをお見せください。

さらに、あなたはBindingに慣れていないようです。まずBinding関連の文書を読むことができると提案しました。これを参照してくださいData binding overview

詳細は、MSの公式コードサンプルDevice enumeration and pairing sampleです。

+0

こんにちは、ご回答いただきありがとうございます。これは、私が必要なものに私を近づけるようです。私はあなたが提案したように調整を行いましたが、現在ドロップダウンメニューで文字列が表示されますが、その言葉は "Windows.Devices.Enumeration.DeviceInformation"です。私はラップトップとペアになっているデバイスはほとんどありませんが、そのうちのどれもがリストに表示されていません。 – Slavisha

+0

@Slavishaどのようにして 'DeviceInformation'をコンボボックスにバインドしましたか?私はあなたのxamlページを見たい。 –

+0

謝、私は上記のコードを掲載しました。 XAMLコード全体が十分であることを確認する必要がありますか? – Slavisha

関連する問題