如何编程配对蓝牙设备



我最近购买了一个lilypad simblee ble板,我想以编程方式将其配对与我的计算机(使用C#中的32feet.net库)。

我知道" 如何编程配对蓝牙设备"已经在stackoverflow上询问(例如,在这里),但是由于某些原因,我所有尝试配对设备编程性失败了。确实,我成功将设备与Windows 10设置面板中的" 管理蓝牙设备"窗口配对em>>> 蓝牙)。

首先,我不知道与设备一起使用的配对方法( legacy ssp )。Windows从来没有问过我的别针或其他东西,所以我想是SSP,但我不确定。

我在Google上搜索了如何做 ssp 配对请求与32feet.net:我找到了。

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

我的代码:

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
        }
    }
}

为什么配对请求失败?

您链接的问题的答案有一个合理的建议...您阅读了吗?

您也应该查看这个问题。

32feet库是围绕传统配对而构建的,因此您要么需要知道要连接到的设备的销钉,要么为其提供了空,以获取弹出窗口以输入PIN。

它还说,32feet使用的Windows功能是在较新版本的Windows中弃用的。如果这是真的,那么它立即失败的原因是,您在配对请求中传递了一个空PIN,并且要继续进行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 });

最新更新