我刚刚开始了一个项目,该项目需要我将 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");
}
然后我添加了一个事件处理程序,这样我就可以选择一个检测到的设备并尝试与之配对。
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!");
}
}
}
在我的带有廉价蓝牙 2.0 适配器的 Windows7 台式电脑上,这会导致我的手机上出现一个弹出窗口,要求我输入密码。当我输入“123456”时,配对成功。
然而,这就是问题开始的地方。然后我拿起我的应用程序并在我的 Windows10 平板电脑上运行它,现在当我选择我的手机时,它会导致我的手机上出现一个带有随机 6 位数密码的弹出窗口,并显示一条消息,它应该与我的平板电脑屏幕上显示的内容相匹配,带有配对/取消按钮作为选项。按下任一按钮都会导致失败。
这是我做错了吗?32feet.NET 不支持的驱动程序?
任何建议将不胜感激。
更新:bare_metal 的评论帮助我更进一步
我添加了一个 BluetoothWin32Authentication 事件处理程序并添加了一个按钮来启动 SSP 配对:
EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
private void btnPairSSP_Click(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)
{
Task t = new Task(PairBluetoothTask);
t.Start();
}
}
private void PairBluetoothTask()
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, null))
{
MessageBox.Show("We paired!");
}
else
{
MessageBox.Show("Failed to pair!");
}
}
private void handleAuthRequests(object sender, BluetoothWin32AuthenticationEventArgs e)
{
switch (e.AuthenticationMethod)
{
case BluetoothAuthenticationMethod.Legacy:
MessageBox.Show("Legacy Authentication");
break;
case BluetoothAuthenticationMethod.OutOfBand:
MessageBox.Show("Out of Band Authentication");
break;
case BluetoothAuthenticationMethod.NumericComparison:
if(e.JustWorksNumericComparison == true)
{
MessageBox.Show("Just Works Numeric Comparison");
}
else
{
MessageBox.Show("Show User Numeric Comparison");
if (MessageBox.Show(e.NumberOrPasskeyAsString, "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Confirm = true;
}
else
{
e.Confirm = false;
}
}
break;
case BluetoothAuthenticationMethod.PasskeyNotification:
MessageBox.Show("Passkey Notification");
break;
case BluetoothAuthenticationMethod.Passkey:
MessageBox.Show("Passkey");
break;
default:
MessageBox.Show("Event handled in some unknown way");
break;
}
}
当我从手机启动配对时,一切正常,触发事件,弹出消息框,配对成功。
但是,当我从平板电脑启动配对时,永远不会触发事件处理程序,因此配对失败。