Bluetooth Pairing (SSP) on Windows 10 with 32feet.NET

眉间皱痕 提交于 2019-12-03 18:00:31

问题


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

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

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.

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.

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

Any advice would be much appreciated.

UPDATE: The comment from bare_metal has helped me get a bit further

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.


回答1:


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

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.

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/en-us/library/windows/apps/mt168401.aspx

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



来源:https://stackoverflow.com/questions/36919276/bluetooth-pairing-ssp-on-windows-10-with-32feet-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!