Windows 10上使用32feet.NET的蓝牙配对(SSP)

我刚刚开始了一个项目,要求我将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 authHandler = new EventHandler(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; } } 

当我从手机启动配对时,此function正常,触发事件,弹出消息框并且配对成功。

但是,当我从平板电脑启动配对时,事件处理程序永远不会被触发,因此配对失败。

我认为这里的问题是32feet库是围绕传统配对构建的,因此您需要知道要连接的设备的引脚,或者为它提供null以获得弹出窗口以输入引脚。 该对话框可能没有通过新版本的Windows – 不确定,但32feet库包含的本机函数的文档说,如果开发比Vista更新,则调用另一种方法。

https://msdn.microsoft.com/en-us/library/windows/desktop/aa362770(v=vs.85).aspx

从我的研究浏览32feet的反编译源,它可能看起来像32feet不支持SSP,只是其他 – 但可能只是提供的蓝牙堆栈实现需要更新 – 或者你需要创建自己的 – 再次我不确定。

您可能希望查看Microsoft提供的.NET库而不是第三方,我可以使用他们的Github示例成功连接并与我的所有设备配对。

https://msdn.microsoft.com/en-us/library/windows/apps/mt168401.aspx

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing/cs