我最近购买了一个Lilypad Simblee BLE Board,我想用编程的方式将其与我的计算机配对(使用C#中的32feet.NET库)。
我知道“如何以编程方式配对蓝牙设备”已经在StackOverflow上被问过(例如这里),但出于某种原因,我所有尝试以编程方式配对设备的尝试都失败了。实际上,我成功地将设备与Windows 10设置面板中的“管理蓝牙设备”窗口进行了配对(设置>设备>蓝牙)。
首先,我不知道我的设备应该使用配对方法(传统的或SSP)。Windows从未要求我输入PIN码之类的东西,所以我猜是SSP,但我不确定。
我在Google上搜索如何使用32feet.NET进行SSP配对请求:我找到了这个。
然而,一旦它发现我的设备(设备发现正常工作),配对请求立即失败。
我的代码:
为什么配对请求失败?
我知道“如何以编程方式配对蓝牙设备”已经在StackOverflow上被问过(例如这里),但出于某种原因,我所有尝试以编程方式配对设备的尝试都失败了。实际上,我成功地将设备与Windows 10设置面板中的“管理蓝牙设备”窗口进行了配对(设置>设备>蓝牙)。
首先,我不知道我的设备应该使用配对方法(传统的或SSP)。Windows从未要求我输入PIN码之类的东西,所以我猜是SSP,但我不确定。
我在Google上搜索如何使用32feet.NET进行SSP配对请求:我找到了这个。
然而,一旦它发现我的设备(设备发现正常工作),配对请求立即失败。
我的代码:
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
}
}
}
为什么配对请求失败?
BluetoothAuthenticateDeviceEx()
(在C++中避免在C#中出现编组错误),但它失败并显示错误代码1450,即ERROR_NO_SYSTEM_RESOURCES
(代码:https://dev59.com/uWXWa4cB1Zd3GeqPMWAd#18466409)。此外,我尝试了你的代码,但是`GetMethod()`返回`null`。然而,我能够通过反射获取到`WindowsBluetoothSecurity`类。我使用的是32feet.NET稳定版本3.5.0。有什么想法吗? - GuiTeKWindows.Devices.Bluetooth.GenericAttributeProfile
命名空间。 - GuiTeK