【发布时间】: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