【问题标题】:Windows 10 UWP Bluetooth Connection with Arduino与 Arduino 的 Windows 10 UWP 蓝牙连接
【发布时间】:2017-05-13 07:42:07
【问题描述】:

我试图找到通过 UWP 应用程序通过蓝牙查找和连接 Arduino 的方法,因此经过大量的摆弄,我找到了两种方法(不幸的是,找不到任何官方的 MS 文档) 所以一种方法是

            DeviceInformationCollection availableDevices = await BluetoothSerial.listAvailableDevicesAsync();
        foreach (DeviceInformation device in availableDevices)
        {
            deviceList.Add(device);

        }

这会返回一个名为“Dev B”的蓝牙设备,它实际上是我的 arduino 的蓝牙模块。即使有其他可用的配对/未配对设备,此方法始终只返回“Dev B”。

然后我用这个函数连接Arduino

            var selectedDevice = (DeviceInformation)DeviceListView.SelectedItem;
        uint BaudRate = 9600;
        var connection = new BluetoothSerial(selectedDevice.Name);
        arduino = new RemoteDevice(connection);
        connection.begin(BaudRate, SerialConfig.SERIAL_8N1);
        connection.ConnectionEstablished += OnConnectionEstablished;

        ConnectedDeviceTextBox.Text = "Connected with Arduino.";

而且它工作得非常好并且可以连接起来。到目前为止一切都很好

所以因为我需要找到所有可用的蓝牙设备,所以我找到了以下方法

            var selector = BluetoothDevice.GetDeviceSelectorFromPairingState(true);
        var devices = await DeviceInformation.FindAllAsync(selector);
        foreach (DeviceInformation device in devices)
        {
            deviceList.Add(device);
        }

这给了我所有配对的蓝牙设备,但蓝牙模块的名称现在是 HC-05(这实际上是窗口在蓝牙设置和其他任何地方显示的名称)。但是,如果我将此设备信息传递给上面的连接代码,它就不会连接。我尝试在

中使用设备名称
var connection = new BluetoothSerial(selectedDevice.Name);

但它不起作用,我也尝试过传入设备本身

var connection = new BluetoothSerial(selectedDevice);

还是没有运气。

有人可以解释名称差异以及为什么它不与第二种方法连接。提前致谢。

【问题讨论】:

    标签: c# bluetooth arduino windows-10-universal windows-10-iot-core


    【解决方案1】:

    BluetoothSerial 类调用RfcommDeviceService.FromIdAsync(String),其中字符串表示RFCOMM 服务实例(一个服务)。

    BluetoothDevice.FromIdAsync(String) 接受代表经典基带(设备)的远程蓝牙设备实例的字符串。

    如果您想要来自 RfcommDeviceService 的 BluetoothDevice,请使用 RfcommDeviceService.Device 属性。

    如果您想查找蓝牙设备的服务,请使用BluetoothDevice.RfcommServices 属性。

    【讨论】:

      【解决方案2】:

      第一个api BluetoothSerial 实际操作在蓝牙的RFCOMM 设备上。你可以找到它的实现here

      第二个api BluetoothDevice实际上是在蓝牙耳机等经典蓝牙设备上运行的。

      它们实现了不同的蓝牙协议,分别属于 Windows 运行时的Windows.Devices.Bluetooth.RfcommWindows.Devices.Bluetooth 命名空间。

      基于以上原因,你将BluetoothDevice传递给BluetoothSerial接口,得到失败,是合理的。

      至于设备名,我认为由于调用两种设备api的结果表示不同,所以不兼容。

      【讨论】:

      • 谢谢你,丽塔,明白你关于 Rfcomm 和 BluetoothDevice 类之间区别的观点。至于名称,我尝试更改 HC-05 蓝牙模块的名称,除了我的应用程序仍然使用第一种方法显示 Dev B 之外,它在所有地方都更新了。如果我发现了什么,我会继续调查并更新主题。
      猜你喜欢
      • 2020-09-14
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多