0

I'm facing a very strange three-way headache. I'm using Unity Engine and a BrainLink Bluetooth device as a source of input. I connect to the BrainLink device automatically via code using a library called Neurosky.ThinkGear and so far these two work fine together, but that's assuming the device has been paired manually via Bluetooth & Other Devices window.

Now I've been asked to also PAIR the device automatically, and this is where I hit a snag. Because using Unity Engine I can't use the windows runtime stuff (like Windows.Enumeration.Devices), I've decided to use the InTheHand 32Feet solution for Bluetooth devices and it seems to kind of work. The device appears as listed in Bluetooth & Other Devices if it wasn't already and it's listed as Paired as well. The problem is that when paired via code and not manually, the library that handles connecting to the device (aforementioned Neurosky.ThinkGear) can't connect to the device. It only connects if the device is removed and paired again via Bluetooth & Oher Devices window.

The Code I'm currently testing is as follows:

 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();
}

This is the method that handles pairing. At the moment, I never pass a value to pin but it's there just in case I need to support other devices in the future.

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;
}
  • 1
    It look slike the Neurosky.ThinkGear uses vCOM to communicate with your Bluetooth device.In this case pairing via 32feet does not install vCOM. – Mike Petrichenko Nov 23 '21 at 09:55
  • Oh dang, you were completely right! I totally forgot the Neurosky.ThinkGear was using vCOM. My script even logs which port it uses, which is doubly embarrassing! I did some digging following this lead and found out I just needed to add this line before pairing: **btDevice.SetServiceState(BluetoothService.SerialPort, true);** After adding it, I was able to pair and connect to the device properly! Thanks you so much! – The Manly Fairy Nov 23 '21 at 10:52

1 Answers1

0

I have zero knowledge about your devices/tools, but what I know is that to establish a Bluetooth connection, we need to discover devices first.

The reason is that such a discovery creates an object that is later used on Bluetooth actions (e.g., pairing, connecting).

The device appears as listed in Bluetooth & Other Devices if it wasn't already and it's listed as Paired as well.

I think by this, what you mean is previously paired devices. The device appearing on the list might not mean that the device is currently discovered. I suggest change your code accordingly where you perform discovery first.

Mr. Panda
  • 485
  • 3
  • 14
  • Ok, I tried your suggestion. Instead of checking for existing pairings, now the code will always attempt to discover the device and attempt to pair without checking device.Authenticated in AttemptPair(), but I'm still getting the same problem. – The Manly Fairy Nov 23 '21 at 10:37