简体   繁体   English

与Arduino的Windows 10 UWP蓝牙连接

[英]Windows 10 UWP Bluetooth Connection with Arduino

I was trying to find ways to find and connect Arduino via Bluetooth through UWP app, so with lots of fiddling I found two ways(couldn't find any official MS documentation on that, unfortunately) So one method is 我试图找到通过UWP应用程序通过蓝牙找到和连接Arduino的方法,所以有很多摆弄我找到了两种方法(不幸的是找不到任何正式的MS文档)所以一种方法是

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

        }

This returns a Bluetooth device named "Dev B",and it is actually my arduino's Bluetooth module. 这将返回名为“Dev B”的蓝牙设备,它实际上是我的arduino的蓝牙模块。 Even though there are other devices available paired/unpaired this method always returns just "Dev B". 即使有其他设备可用配对/不配对,此方法始终只返回“Dev B”。

And then I use this function to connect with Arduino 然后我用这个函数连接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.";

And it works wonderfully and gets connected. 它工作得非常好,并且可以连接起来。 And everything is good till now 到目前为止一切都很好

So because I needed to find all the bluetooth devices available I found following method 因为我需要找到所有可用的蓝牙设备,我发现了以下方法

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

This gives me all the paired Bluetooth devices, but the name of Bluetooth module is now HC-05(This is actually the name that windows show in Bluetooth setting and everywhere else). 这给了我所有配对的蓝牙设备,但蓝牙模块的名称现在是HC-05(这实际上是Windows在蓝牙设置和其他地方显示的名称)。 But if I pass this device information to Connection code above it doesn't connect. 但是,如果我将此设备信息传递给上面的连接代码,则无法连接。 I tried using device name in 我尝试使用设备名称

var connection = new BluetoothSerial(selectedDevice.Name);

but it doesn't work, I also tried passing in device itself 但它不起作用,我也试过传入设备本身

var connection = new BluetoothSerial(selectedDevice);

still no luck. 仍然没有运气。

Can someone please explain the name difference and why its not connecting with second method. 有人可以解释名称差异以及为什么它不与第二种方法连接。 Thanks in advance. 提前致谢。

The first api BluetoothSerial actually operate on RFCOMM device of Bluetooth. 第一个api BluetoothSerial实际上在蓝牙的RFCOMM设备上运行。 You can find its implementation here . 你可以在这里找到它的实现。

The second api BluetoothDevice actually operate on classical Bluetooth device like Bluetooth headset. 第二个api BluetoothDevice实际上在蓝牙耳机等经典蓝牙设备上运行。

They are implementated different Bluetooth protocol and respectively belongs to Windows.Devices.Bluetooth.Rfcomm and Windows.Devices.Bluetooth namespace in Windows Runtime. 它们实现了不同的蓝牙协议,分别属于Windows运行时的Windows.Devices.Bluetooth.RfcommWindows.Devices.Bluetooth命名空间。

Based on above reason, you pass BluetoothDevice to BluetoothSerial interface and get failure, it is reasonable. 基于以上原因,您将BluetoothDevice传递给BluetoothSerial接口并获得失败,这是合理的。

As for the device name, I think since it has different representations in the results of calling two type of device api, so, they are incompatible. 至于设备名称,我认为因为它在调用两种类型的设备api的结果中有不同的表示,所以,它们是不兼容的。

The BluetoothSerial class calls RfcommDeviceService.FromIdAsync(String) where the string represents the RFCOMM service instance (a service). BluetoothSerial类调用RfcommDeviceService.FromIdAsync(String) ,其中字符串表示RFCOMM服务实例(服务)。

The BluetoothDevice.FromIdAsync(String) takes a string representing the remote Bluetooth device instance for classic baseband (a device). BluetoothDevice.FromIdAsync(String)采用表示经典基带(设备)的远程蓝牙设备实例的字符串。

If you want the BluetoothDevice from the RfcommDeviceService you use the RfcommDeviceService.Device property. 如果您需要来自RfcommDeviceService的BluetoothDevice,请使用RfcommDeviceService.Device属性。

If you want to find the services for a BluetoothDevice you use the BluetoothDevice.RfcommServices property. 如果要查找BluetoothDevice的服务,请使用BluetoothDevice.RfcommServices属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM