【问题标题】:Bluetooth Pairing (SSP) on Windows 10 with 32feet.NET带有 32feet.NET 的 Windows 10 上的蓝牙配对 (SSP)
【发布时间】:2016-04-28 15:34:00
【问题描述】:

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

        }
    }

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

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

【问题讨论】:

  • 第一次与 PINCODE 配对称为传统配对,而具有随机 6 位数字的配对称为安全简单配对。在 SSP 中验证发起者和响应者,需要同时按下配对/接受按钮。您是否也在 Windows10 中看到了接受/拒绝按钮? ,如果是,您是否从两个设备都接受了?,需要多长时间才能注意到配对失败(20 秒)?
  • 不,我在 Windows10 平板电脑上没有看到接受/拒绝选项或密码。将尝试改变方法并查看 Windows.Devices.Bluetooth 命名空间而不是 32feet.NET。
  • 我也有同样的问题,求帮助!
  • @VikramBose,对不起,没有帮助,项目被移到优先级列表中,所以没有花太多时间在上面。我确实有机会简要了解一下 Windows.Devices 命名空间,但由于我的开发环境受到一些 IT 限制,这有点不适合。有人也很好地对这个问题进行了赏金,但它仍然没有吸引任何答案,所以我想这是一个没有多少人正在研究的领域。

标签: c# bluetooth 32feet


【解决方案1】:

我认为这里的问题是 32feet 库是围绕传统配对构建的,因此您要么需要知道要连接的设备的引脚,要么为它提供 null 以获取弹出窗口以输入一个别针。该对话框可能无法进入新版本的 Windows - 对此不确定,但 32feet 库包装的本机函数的文档说,如果为比 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

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 2022-11-26
    • 1970-01-01
    • 2020-09-24
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多