ペアリングされた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;
は、あなたが何か提案はありますか
怒鳴るあなたは私がここで行方不明です何でポイントしてくださいすることができ、私のコードです? ありがとうございます。
こんにちは、ご回答いただきありがとうございます。これは、私が必要なものに私を近づけるようです。私はあなたが提案したように調整を行いましたが、現在ドロップダウンメニューで文字列が表示されますが、その言葉は "Windows.Devices.Enumeration.DeviceInformation"です。私はラップトップとペアになっているデバイスはほとんどありませんが、そのうちのどれもがリストに表示されていません。 – Slavisha
@Slavishaどのようにして 'DeviceInformation'をコンボボックスにバインドしましたか?私はあなたのxamlページを見たい。 –
謝、私は上記のコードを掲載しました。 XAMLコード全体が十分であることを確認する必要がありますか? – Slavisha