c# - 如何以编程方式配对蓝牙设备

标签 c# bluetooth windows-10 bluetooth-lowenergy 32feet

我最近买了一个Lilypad Simblee BLE Board我想以编程方式将它与我的计算机配对(使用 C# 中的 32feet.NET 库)。

我知道 StackOverflow 上已经有人问过“如何以编程方式配对蓝牙设备”(例如 here),但是由于某种原因,我尝试配对设备的所有尝试以编程方式失败。事实上,我成功地将设备与 Windows 10 设置面板中的“管理蓝牙设备”窗口配对(设置> 设备> 蓝牙)。

首先,我不知道 pairing method (legacySSP)与我的设备一起使用。 Windows 从未要求我提供 PIN 或其他信息,所以我猜它是 SSP,但我不确定。

我在 Google 上搜索了如何使用 32feet.NET 进行 SSP 配对请求:我找到了 this .

但是,一旦它发现了我的设备(设备发现工作正常),配对请求立即失败。

我的代码:

using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;

namespace HLK_Client
{
    class HLKBoard
    {
        public event HLKBoardEventHandler HLKBoardConnectionComplete;
        public delegate void HLKBoardEventHandler(object sender, HLKBoardEventArgs e);

        private BluetoothClient _bluetoothClient;
        private BluetoothComponent _bluetoothComponent;
        private List<BluetoothDeviceInfo> _inRangeBluetoothDevices;
        private BluetoothDeviceInfo _hlkBoardDevice;
        private EventHandler<BluetoothWin32AuthenticationEventArgs> _bluetoothAuthenticatorHandler;
        private BluetoothWin32Authentication _bluetoothAuthenticator;


        public HLKBoard()
        {
            _bluetoothClient = new BluetoothClient();
            _bluetoothComponent = new BluetoothComponent(_bluetoothClient);
            _inRangeBluetoothDevices = new List<BluetoothDeviceInfo>();
            _bluetoothAuthenticatorHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(_bluetoothAutenticator_handlePairingRequest);
            _bluetoothAuthenticator = new BluetoothWin32Authentication(_bluetoothAuthenticatorHandler);

            _bluetoothComponent.DiscoverDevicesProgress += _bluetoothComponent_DiscoverDevicesProgress;
            _bluetoothComponent.DiscoverDevicesComplete += _bluetoothComponent_DiscoverDevicesComplete;
        }


        public void ConnectAsync()
        {
            _inRangeBluetoothDevices.Clear();
            _hlkBoardDevice = null;
            _bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, false, null);
        }


        private void PairWithBoard()
        {
            Console.WriteLine("Pairing...");

            bool pairResult = BluetoothSecurity.PairRequest(_hlkBoardDevice.DeviceAddress, null);

            if (pairResult)
            {
                Console.WriteLine("Success");
            }

            else
            {
                Console.WriteLine("Fail"); // Instantly fails
            }
        }


        private void _bluetoothComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
        {
            _inRangeBluetoothDevices.AddRange(e.Devices);
        }

        private void _bluetoothComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
        {
            for (int i = 0; i < _inRangeBluetoothDevices.Count; ++i)
            {
                if (_inRangeBluetoothDevices[i].DeviceName == "HLK")
                {
                    _hlkBoardDevice = _inRangeBluetoothDevices[i];
                    PairWithBoard();

                    return;
                }
            }

            HLKBoardConnectionComplete(this, new HLKBoardEventArgs(false, "Didn't found any \"HLK\" discoverable device"));
        }

        private void _bluetoothAutenticator_handlePairingRequest(object sender, BluetoothWin32AuthenticationEventArgs e)
        {
            e.Confirm = true; // Never reach this line
        }
    }
}

为什么配对请求失败?

最佳答案

您链接的问题的答案有一个似是而非的建议……您读过了吗?

你还应该看看 this question as well .

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.

它还说 32feet 使用的 windows 函数在较新版本的 windows 中已被弃用。如果这是真的,它立即失败的原因是因为您在配对请求中传递了一个空 pin,要继续,窗口需要显示一个不再存在的对话框。

如果您尝试连接引脚“0000”或“1234”会怎样?

我正在查看 32feet.net 中 WindowsBluetoothSecurity.cs 的源代码,如果配对请求失败,它会将错误代码记录到 Debug.WriteLine,您是否可以在此处发布该错误代码?

解决此问题的一个好方法可能是导入 BluetoothAuthenticateDeviceEx 并手动使用它来完成配对请求。如果您不想手动执行此操作,看起来在最新版本的 32feet 源代码中,实际上有一个 SSP 配对方法使用了此方法,但它不是公开的,也没有在任何地方使用,因此您需要访问通过反射:

typeof(BluetoothSecurity)
    .GetMethod("PairRequest", BindingFlags.Static | BindingFlags.NonPublic)
    .Invoke(null, new object[] { _hlkBoardDevice.DeviceAddress, BluetoothAuthenticationRequirements.MITMProtectionNotRequired });

关于c# - 如何以编程方式配对蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42701793/

相关文章:

c# - 如何使用 .NET 签署构建?

java - 应用程序在蓝牙发现期间崩溃

rabbitmq - Windows上的rabbitmq-plugins的"Directory name is invalid."等

python - 字体损坏

python - 如何在 cythonize 脚本中包含 numpy?

c# - 在 xamarin 中创建一个 cocossharp 项目

c# - 使用不同的服务器验证 token ?

c# - 将文本和 html 标签中的字符串拆分为数组

蓝牙协议(protocol)栈实现

java - 由于空指针错误,Android 蓝牙应用程序无法运行