我正在尝试让我的 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);
}
但这并没有给出任何有意义的列表。
我有一种感觉,我做错了。请帮助我回到正轨。
我变得更聪明了,我发现另一个问题告诉我这是不可能的。
在 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)