我最近买了一个Lilypad Simblee BLE Board,我想以编程方式将它与我的电脑配对(使用C#中的32feet.NET库) .
我知道StackOverflow(例如here)已经询问了“如何以编程方式配对蓝牙设备”,但出于某种原因,我所有尝试配对设备 programatically 都失败了 . 的确, I successfully paired the device with the "Manage Bluetooth devices" window in Windows 10 Settings panel (设置>设备>蓝牙) .
首先,我不知道pairing method(遗留系统或SSP)与我的设备一起使用 . Windows从未向我询问过PIN或其他内容,所以我猜它是不确定的's SSP, but I' .
我在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
}
}
}
知道为什么配对请求失败了吗?
1 回答
你联系的问题的答案有一个看似合理的建议......你读过它了吗?
你也应该看this question as well .
它还说32feet使用的Windows函数在较新版本的Windows中已被弃用 . 如果这是真的,那么它立即失败的原因是因为你在配对请求中传递了一个空引脚并且为了继续它需要显示一个不再存在的对话框 .
如果您尝试连接“0000”或“1234”引脚会发生什么?
我正在查看32feet.net中
WindowsBluetoothSecurity.cs
的源代码,我看到配对请求是否失败,它将错误代码记录到Debug.WriteLine
,您是否有可能在此处发布该错误代码?解决此问题的一个好方法可能是导入
BluetoothAuthenticateDeviceEx
并手动使用它来完成配对请求 . 如果你不公开,那么's not used anywhere so you'将需要通过反射访问它: