【问题标题】:Pairing Bluetooth device with InTheHand 32Feet shows as paired, but can't connect将蓝牙设备与 InTheHand 32Feet 配对显示为已配对,但无法连接
【发布时间】: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


【解决方案1】:

我对您的设备/工具的了解为零,但我知道要建立蓝牙连接,我们需要先发现设备

原因是这样的发现创建了一个对象,该对象随后用于蓝牙操作(例如,配对、连接)。

如果不是,该设备将显示为蓝牙和其他设备中的列表 已经并且它也被列为已配对。

我认为,您的意思是以前配对的设备。出现在列表中的设备可能并不意味着当前已发现该设备。我建议您在首先执行发现的地方相应地更改您的代码。

【讨论】:

  • 好的,我尝试了你的建议。现在代码将始终尝试发现设备并尝试配对而不检查设备,而不是检查现有配对。在 AttemptPair() 中进行了身份验证,但我仍然遇到同样的问题。
猜你喜欢
  • 2013-05-24
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多