列出并连接到蓝牙设备 [英] List and connect to Bluetooth devices

查看:224
本文介绍了列出并连接到蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的Windows平板电脑应用通过蓝牙与其他设备通信。

I'm tryiing to make my Windows tablet app communicate with an other device via Bluetooth.

首先,我想扫描设备,然后连接到所选的设备。

First I want to scan for devices, then i want to connect to the choosen device.

我我们制作了一个简单的测试应用:空白首页,并在其中添加了一个按钮和一个列表框。然后,我尝试了下面的代码,使我在其他地方不满意:

I've made a simple test app: Blank front page and added a button and a listboxto it. Then I've tried the following code witch I've forund else where here on SO:

    ListBox1.Items.Clear();

    var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

    foreach (var device in devices)
    {
        ListBox1.Items.Add(device);
    }

但列表只是空的

然后,我尝试枚举设备并过滤掉不需要的设备:

Then I've tried to just enum devices and filtered out unwanted devices:

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);
}

但这并没有给出任何详尽的清单。

But that doesn't give any meanfull list.

我感觉自己做错了。请让我回到正轨。

I have a feeling I'm doing ti wrong. Please helt me back on track.

推荐答案

我有一个更明智的选择,我已经解决了另一个这样的问题:

I've just got a little wiser, ive forund an other SO question telling me that it is not posible.

在Windows 8 / 8.1应用商店中搜索并连接到蓝牙设备?

因此,列出的解决方案是:

So the solution for listing is:

1)配对设备

2)列出它们:

ListBox1.Items.Clear();

    var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

    foreach (var device in devices)
    {
        ListBox1.Items.Add(device);
    }

这篇关于列出并连接到蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆