【发布时间】:2021-11-23 08:36:06
【问题描述】:
我正面临着一种非常奇怪的三向头痛。我使用 Unity Engine 和 BrainLink 蓝牙设备作为输入源。我使用名为 Neurosky.ThinkGear 的库通过代码自动连接到 BrainLink 设备,到目前为止,这两者可以正常工作,但这是假设设备已通过蓝牙和其他设备窗口手动配对。
现在我被要求自动PAIR设备,这就是我遇到障碍的地方。因为使用 Unity Engine 我不能使用 Windows 运行时的东西(如 Windows.Enumeration.Devices),所以我决定为蓝牙设备使用 InTheHand 32Feet 解决方案,它似乎有点工作。如果该设备尚未在蓝牙和其他设备中列出并且它也被列为已配对,则该设备将显示为蓝牙和其他设备中的列表。问题是,当通过代码而不是手动配对时,处理连接到设备的库(前面提到的 Neurosky.ThinkGear)无法连接到设备。只有当设备被移除并通过蓝牙和其他设备窗口重新配对时,它才会连接。
我目前正在测试的代码如下:
private void Start()
{
Debug.Log("Operation Start");
//btClient is a class field
btClient = new BluetoothClient();
//Search for existing paired devices that contain "brainlink" in their name
BluetoothDeviceInfo btDevice = CheckExistingPairedDevices();
if (btDevice == null)
{
Debug.Log("No paired device found, trying to discover");
//Try to discover devices in range with "brainlink" in their name
btDevice = TryDiscoverDevice();
}
if(btDevice!= null)
{
Debug.Log("Found Device " + btDevice.DeviceName+", checking for pairing");
bool paired = AttemptPair(btDevice);
Debug.Log("Pair Status: " + paired);
}
else
{
Debug.Log("Could not discover device");
}
CloseClient();
}
这是处理配对的方法。目前,我从不向 pin 传递值,但它存在以防我将来需要支持其他设备。
private bool AttemptPair(BluetoothDeviceInfo btDevice, string pin = null)
{
//Check if this device has been paired before
if (btDevice.Authenticated)
return true;
bool result = BluetoothSecurity.PairRequest(btDevice.DeviceAddress, pin);
btDevice.Refresh();
return result;
}
【问题讨论】:
-
它看起来像 Neurosky。ThinkGear 使用 vCOM 与您的蓝牙设备通信。在这种情况下,通过 32feet 配对不会安装 vCOM。
-
天哪,你完全正确!我完全忘记了 Neurosky。ThinkGear 使用的是 vCOM。我的脚本甚至记录了它使用的端口,这是加倍的尴尬!我按照这条线索做了一些挖掘,发现我只需要在配对之前添加这一行:btDevice.SetServiceState(BluetoothService.SerialPort, true); 添加后,我能够配对并连接到设备正常!非常感谢!
标签: c# unity3d bluetooth 32feet