Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...