Author Topic: BLE in Unity always closes after 30s (encryption issue?)  (Read 4867 times)

Offline HenrikF

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
BLE in Unity always closes after 30s (encryption issue?)
« on: March 26, 2021, 12:45:58 PM »
Hello!

I have an issue with a certain BLE device always closing the connection after 30 seconds.

I am using BT framework in Unity to keep a BLE connection alive during play.
The game and all tests I have made are running on Windows 10. I am using Unity version 2018.4.12f1 and a Bluetooth USB adapter MPOW BH456A (5.0).

I manage to connect to the device and even subscribe to new values that are updated every one second. However, after 30 seconds the device closes the connection with the reason
0X00030008 WCL_E_CONNECTION_CLOSED The connection has been closed.

I believe that I need to perform a Bonding with the device. According to documentation, the connector (BT framework/unity) needs to
respond to an encryption request by encrypting the connection. Perhaps this fails and the connection is dropped.

From what I can see on the connection status, the device seems to think that the connection is not encrypted.

Has anyone had a similar problem or has information about if the Bluetooth Framework responds correctly to encryption requests?

I can unfortunately not say too much about the device since we have signed an NDA with the manufacturers.

Thankful for your response.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #1 on: March 26, 2021, 01:34:04 PM »
Hello,

To enable encryption you need to pair with your device before connection. However there is known bug (we have been reported few times but were not able to reproduce it on our side) in Win 10 20H2 when it drops connection with paired devices.

Offline HenrikF

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #2 on: March 29, 2021, 10:34:08 AM »
Hi Mike!
You are right, it was as simple as just pairing the device before connection. Thank you!
Is it possible to use the Unity wrapper of Bluetooth Framework to perform the pairing automatically/programmatically? Or is it that typically performed using other libraries when using Unity/C#?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #3 on: March 29, 2021, 10:46:36 AM »
Of course it is possible (cause Bluetooth Framework supports it). But it needs some modification of wrapper (adding pairing code).

P.S. Please note that handling pairing requests is not supported for BLE devices. That mean that you have to call Pair() method to pair device. For classic Bluetooth devices you can simple try to connect and if pairing required Windows (and Bluetooth Framework) fires the pairing events. For BLE you always have to call Pair() before connect.

Offline HenrikF

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #4 on: March 29, 2021, 03:50:07 PM »
I wrote a wrapper for RadioRemotePair and GetRemotePair. I run RadioRemotePair only if the device is not already paired.
It seems like the pairing is working, but when I afterwards run "connect", the device successfully connects, but then instantly responds with "0X00030008 WCL_E_CONNECTION_CLOSED".
I realize it is probably difficult to say what could cause this without knowing more about the device, but do you have any ideas?
I am using WCL-for-unity from 2019-11-18.
See some code snippets from below.

Code: [Select]
__declspec(dllexport) int __stdcall RadioRemotePair(CwclBluetoothRadio* Radio, __int64 Address)
{
if (Radio == NULL)
return WCL_E_INVALID_ARGUMENT;

return Radio->RemotePair(Address);
}

__declspec(dllexport) bool __stdcall GetRemotePaired(CwclBluetoothRadio* Radio, __int64 Address)
{
if (Radio == NULL)
return WCL_E_INVALID_ARGUMENT;

bool paired = false;
Radio->GetRemotePaired(Address, paired);
return paired;
}

Code: [Select]
private void FManager_OnDeviceFound(System.Object sender, IntPtr Radio, long Address)
    {
        try
        {
            if (MyGameConfigs.getAvaialablePoXAdresses().Contains(Address))
            {
                UnityEngine.Debug.Log("Checking is pox paired " + Address.ToString("X12"));
                if (FManager.IsRemotePaired(Radio, Address) == false)
                {
                    UnityEngine.Debug.Log("Pairing to pox " + Address.ToString("X12"));
                    Int32 Res1 = FManager.RemotePair(Radio, Address);
                    if (Res1 != BluetoothErrors.WCL_E_SUCCESS)
                    {
                        UnityEngine.Debug.Log("Failed to pair: 0x" + Res1.ToString("X8"));
                    } else
                    {
                        UnityEngine.Debug.Log("Pox successfully paired " + Address.ToString("X12"));
                    }
                } else
                {
                   UnityEngine.Debug.Log("Pox is already paired " + Address.ToString("X12"));
                }

                UnityEngine.Debug.Log("Connecting to pox " + Address.ToString("X12"));

                Int32 Res = FClientPox.Connect(Radio, Address);
                if (Res != BluetoothErrors.WCL_E_SUCCESS)
                {
                    UnityEngine.Debug.Log("Failed to connect: 0x" + Res.ToString("X8"));
                }
            }
        }
        catch (Exception ex)
        {
            UnityEngine.Debug.Log("Something went wrong with connecting to the device!");
            UnityEngine.Debug.LogError(ex.Message);
        }
    }

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #5 on: March 29, 2021, 04:01:34 PM »
It is really hard to say what can be wrong but we did lot of changes in WCL since that day and few fixes were regarding connection issue. I am not 100% sure that it has been fixed but I recommend to try to use latest Bluetooth Framework version first.

Also, make sure that you do not use Arduino Nano 33 based device. If so - you need to update the BLE lib for that platform cause there was a bug that causes such problme on Win 10 20H2

Offline HenrikF

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #6 on: April 13, 2021, 05:22:45 PM »
Hi again Mark,

I tried with the latest framework version, but it gave no different result. I will try to check if this can be due to an Arduino Nano 33 board being used in the device.

I did manage to manually pair the device with my PC, but now the device automatically connects to the PC as soon as the device is turned on. Is it possible to retrieve and manipulate this connection using the BT framework? I guess what I am asking is: Is it possible to build a valid GattClient object that can retrieve services etc. if you have an adress to a device that you know is already connected?
I tried to do this by manually setting the adress of a GattClient object, but when reading services I just got "WCL_E_CONNECTION_NOT_ACTIVE".

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #7 on: April 13, 2021, 06:01:02 PM »
Hi,

It should never happen that paired BLE device automatically connects when turoned ON. It really would be great to know more about your device (if there any confidintial information its better to e-mail to support@btframework.com instead of posting it here).

Check Error parameter passed to OnConnect event to get more information about what happened during connection and why you get WCL_E_CONNECTION_NOT_ACTIVE.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: BLE in Unity always closes after 30s (encryption issue?)
« Reply #8 on: April 27, 2021, 08:01:01 AM »
By this link you can find the demo code that shows how to pair with a GATT device and prevent disconnection and errors during connection:

https://github.com/btframework/GattAuth

 

Sitemap 1 2 3 4 5 6 7