我刚开始一个项目,要求我将Windows 10平板电脑与另一个蓝牙设备配对.
我决定从一个简单的Windows窗体应用程序开始,熟悉这个过程.我在我的解决方案中添加了32feet.NET NuGet包,并且很快就成功搜索了设备并填充了列表框.
client = new BluetoothClient();
devices = client.DiscoverDevices();
if (devices.Length > 0)
{
foreach (var device in devices)
{
lstBTDevices.Items.Add(device.DeviceName);
}
}
else
{
MessageBox.Show("Unable to detect any bluetooth devices");
}
Run Code Online (Sandbox Code Playgroud)
然后我添加了一个事件处理程序,以便我可以选择一个检测到的设备并尝试与它配对.
private void LstBTDevices_SelectedIndexChanged(object sender, EventArgs e)
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (MessageBox.Show(String.Format("Would you like to attempt to pair with {0}?", selectedDevice.DeviceName), "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, "123456"))
{
MessageBox.Show("We paired!");
}
else
{
MessageBox.Show("Failed to pair!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的带有廉价蓝牙2.0适配器的Windows7台式电脑上,这会导致我的手机上出现一个弹出窗口,要求我输入密码.当我输入"123456"时,配对成功.
但是,这就是问题的起点.然后我拿起我的应用程序并在我的Windows10平板电脑上运行它,现在当我选择我的手机时,它会在我的手机上显示一个弹出窗口,其中包含一个随机的6位数密码,并显示一条消息,它应该与我平板电脑屏幕上显示的内容相匹配,配对/取消按钮作为选项.按任一按钮都会导致失败. …