我正在尝试使Windows平板电脑应用程序通过蓝牙与其他设备通信。
首先,我要扫描设备,然后再连接到所选设备。
我做了一个简单的测试应用程序:空白首页,并在其中添加了一个按钮和一个列表框。然后,我尝试了以下代码巫婆,使我在这里遇到了其他麻烦:
ListBox1.Items.Clear();
var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
foreach (var device in devices)
{
ListBox1.Items.Add(device);
}
但是列表只是空的
然后,我尝试枚举设备并过滤掉不需要的设备:
var list = await DeviceInformation.FindAllAsync();
var uniqueList = new HashSet<string>();
var terminators = new List<string>() { "Audio", "Mixer", "Mic", "Realtek", "Usb", "Gmail,", "Line in", "Lyd", "Display", "surface", "@" };
foreach (var element in list)
{
var strToken = element.Name.ToUpper();
if (!uniqueList.Add(strToken))
continue;
var contains = false;
foreach (var word in terminators)
if (strToken.Contains(word.ToUpper()))
contains = true;
if (!contains)
ListBox1.Items.Add(element.Name);
}
但这并没有给出任何详尽的清单。
我感觉自己做错了。请让我回到正轨。
我有一个更明智的选择,我已经提出了另一个SO问题,告诉我这是不可能的。
在Windows 8 / 8.1应用商店中搜索并连接到蓝牙设备?
因此,列出的解决方案是:
1)配对设备
2)列出它们:
ListBox1.Items.Clear();
var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
foreach (var device in devices)
{
ListBox1.Items.Add(device);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句