Windows 10 UWP与Arduino的蓝牙连接

问题描述:

我想通过UWP应用程序找到找到并通过蓝牙连接Arduino的方法,因此我找到了两种方法(找不到任何官方的MS文档) 所以一个方法是Windows 10 UWP与Arduino的蓝牙连接

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

     } 

这将返回名为“开发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(这实际上是Windows在蓝牙设置和其他地方显示的名称)。但是,如果我将此设备信息传递给上面的Connection代码,则它不会连接。我试着在

var connection = new BluetoothSerial(selectedDevice.Name); 

使用设备名称,但它不工作,我也试过路过的设备本身

var connection = new BluetoothSerial(selectedDevice); 

仍然没有运气。

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

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

第二个api BluetoothDevice实际上在蓝牙耳机等传统蓝牙设备上运行。

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

基于以上原因,您将BluetoothDevice通过BluetoothSerial接口并获得失败,这是合理的。

至于设备名称,我认为,因为它在调用两种类型的设备API的结果中有不同的表示,所以它们是不兼容的。

+0

谢谢你Rita, 了解Rfcomm和BluetoothDevice类之间的区别。 至于名称,我尝试更改HC-05蓝牙模块的名称,除了我的应用程序以外,它的更新位置仍然以第一种方法显示Dev B。如果我找到了某些东西,我会继续研究并更新线程。 –