Windows 10与32feet.NET上的蓝牙配对(SSP) [英] Bluetooth Pairing (SSP) on Windows 10 with 32feet.NET

查看:233
本文介绍了Windows 10与32feet.NET上的蓝牙配对(SSP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚启动了一个项目,该项目要求我将Windows 10平板电脑与另一台蓝牙设备配对.

I've just started a project that will require me to pair a Windows 10 tablet with another bluetooth device.

我决定从一个简单的Windows窗体应用程序开始,以熟悉该过程.我在解决方案中添加了32feet.NET NuGet程序包,并很快成功地搜索了设备并填充了列表框.

I decided to start with a simple windows forms app to familiarise myself with the process. I added the 32feet.NET NuGet package to my solution, and quickly had success with searching for devices and populating a listbox.

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");
}

然后我添加了一个事件处理程序,以便可以选择检测到的设备并尝试与之配对.

I then added an event handler so I could select a detected device and attempt to pair with it.

    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"时,配对成功.

On my Windows7 desktop PC with cheap Bluetooth 2.0 adaptor this causes a popup to appear on my phone requesting I enter the pincode. When I enter "123456" the pairing is successful.

但是,这是问题开始的地方.然后,我拿起我的应用程序并在Windows10平板电脑上运行它,现在,当我选择手机时,它会在手机上弹出一个带有随机6位数密码的弹出窗口,并显示一条消息,该消息应与平板电脑屏幕上显示的内容匹配,并带有配对/取消按钮.按下任一按钮都将导致失败.

However, this is where the problem starts. I then take my application and run it on my Windows10 tablet, and now when I select my phone it causes a popup to appear on my phone with a random 6 digit pincode, and a message that it should match what is displayed on my tablet screen, with pair/cancel buttons as the options. Pressing either button results in a fail.

这是我做错了吗?是32feet.NET不支持的驱动程序?

Is this something i'm doing wrong? A driver not supported by 32feet.NET?

任何建议将不胜感激.

更新:bare_metal的评论帮助我进一步了解了

我添加了BluetoothWin32Authentication事件处理程序并添加了用于启动SSP配对的按钮:

I added a BluetoothWin32Authentication event handler and added a button to initiate an SSP pairing:

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;

        }
    }

当我通过手机启动配对时,这可以正常工作,触发事件,弹出消息框并且配对成功.

When I initiate pairing from my phone, this works fine, the event is triggered, the message box pops and pairing is successful.

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

However when I initiate pairing from the tablet, the event handler is never triggered, so pairing fails.

推荐答案

我相信这里的问题是32feet库是围绕旧式配对构建的,因此您要么需要知道要连接的设备的引脚,或者您将其提供为空值以获取一个弹出窗口以输入图钉.该对话框可能尚未进入Windows的新版本- 对此不确定,但是32feet库包装的本机函数的文档说如果要开发比Vista更新的版本,则调用另一种方法.

I believe the problem here is that the 32feet library is built around legacy pairing, so that you either need to know the pin of the device you are connecting to, or you supply it with a null to get a popup window to enter a pin. That dialog may not have made it through to the new version of windows - Not sure on this, but the documentation for the native function that the 32feet library wraps, says to call another method if developing for newer than Vista.

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

从我的研究中浏览了32feet的反编译源,看起来32feet不支持SSP,仅支持其他-但这可能只是所提供的蓝牙堆栈实现需要更新-或者您需要创建自己的-再次不确定.

From my research browsing through the decompiled sources of 32feet, it may look like 32feet doesn't support SSP, just others - but that may only be that the supplied bluetooth stack implementations need updating - or you need to create your own - again I am not sure.

您可能要研究Microsoft为.NET提供的库,而不是第3方,我可以使用Github中的示例成功连接并与我的所有设备配对.

You may want to look into Microsoft supplied libraries for .NET instead of this 3rd party, I was able to use their example from Github to successfully connect and pair with all my devices.

https://msdn.microsoft.com/zh-我们/library/windows/apps/mt168401.aspx

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

这篇关于Windows 10与32feet.NET上的蓝牙配对(SSP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆