c# - 使用 32feet.NET 在 Windows 10 上进行蓝牙配对 (SSP)

标签 c# bluetooth 32feet

我刚刚开始一个项目,需要我将 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;

        }
    }

当我从我的手机启动配对时,这工作正常,事件被触发,消息框弹出并且配对成功。

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

最佳答案

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

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

根据我浏览 32feet 反编译源的研究,32feet 似乎不支持 SSP,只是支持其他 - 但这可能只是提供的蓝牙堆栈实现需要更新 - 或者您需要创建自己的 -我还是不确定。

您可能想要查看 Microsoft 为 .NET 提供的库而不是这个第 3 方,我能够使用他们在 Github 上的示例成功连接并配对我的所有设备。

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

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

关于c# - 使用 32feet.NET 在 Windows 10 上进行蓝牙配对 (SSP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919276/

相关文章:

iphone - 如何通过蓝牙将 iOS 视频流式传输到另一台设备?

iPhone - 是否可以通过蓝牙编写应用程序来连接非 iPhone 设备?

c# - 验证价格的正则表达式

c# - Azure downloadtostreamasync 方法挂起

java - 在 PC 上运行的用于监听 Android 设备的 Bluez 应用程序

c# - 捕获文件在 C# 中通过蓝牙发送

c# - 使用 32feet.net 使用独特的服务协议(protocol)连接到 BTLE 设备

c# - 为什么使用 Write(Byte[]) 而不是使用 foreach 和 Write(Byte) 用 BinaryWriter 发送数据要快得多?

c# - Asp.Net Global.asax获取当前请求的Page对象

c# - C# 中是否有类似于 C 中的 fflush() 的东西?