Wireless Communication Library Support Forum

Frameworks => Bluetooth Framework => Topic started by: KCorazza on June 30, 2021, 07:44:51 AM

Title: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 07:44:51 AM
Hi all,

I have a lot of difficulties to connect to a BT devices. Behavior is not regular. Sometime I was able to connect but today for example doesn't work anymore and Connect return error 0x50041.

I really don't know the reason of that. The same code is used to connect with other devices without any single issue.

Can you give me some hint to help to solve this problem ?

Thank you in advance. Best regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 07:49:18 AM
Hi,

The error 0x00050041 (WCL_E_BLUETOOTH_RFCOMM_DOWN) means: "The RFCOMM received DM response" (https://www.btframework.com/errors.htm)

This error usually appears in case of Authentication (pairing) and/or Encription was failed. Make sure your device has been paired correctly and the link key has not been reset on both sides.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 09:45:44 AM
Hi Milke,

thank you for the quick reply.

When you say "pair" what do you mean exactly ?

1. Pair the device with Windows
2. Pair the device with RemotePair method ? In this case pairing has to be done before or after the connect ?

I didn't do any pairing because I understood that there is an autopairing process available.

Waiting for your kind reply. Thank you.

Regards. Keven.

Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 10:05:17 AM
Me again,

I found this workaround that of course I don't like but that works and maybe allow you to understand more:

* I pair the device using Windows
* when pairing is completed in Windows I found two devices paired:

1. Bluetooth LE Device <address>
2. ZN0666210001

* I have to remove the first from Windows ("Bluetooth LE Device")

If I do the above steps then Connect is working properly.

Question is how to make this process automatic within the application without to require to pair with Windows.

Thanks. Regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 10:06:51 AM
Hi

It looks your device is mixed one (supports both BLE and RFCOMM). So you shoudl discover your device as classic and then call RemotePair method. Or you can simple try to connect and if authentication is required it will be done automatically. Take a look on RfCommClient demo to find out how to do that.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 10:14:23 AM
Thanks,

If I run Connect directly it doesn't work and so I suppose that I have to use RemotePair.

What is the right command sequence ? Connect and then RemovePair or RemotePair and then Connect ?

Thanks. Regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 10:18:50 AM
Pairing first. Any way, Connect to Classic device's MAC will force pairing if it is required by the device. Of course you must handle all pairing events.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 10:32:54 AM
Sorry to bother you.

I am trying to Pair and then Connect:

Code: [Select]
            try
            {
                wclBluetoothRadio Radio = GetSelectedRadio();
                if (Radio != null)
                {
                    m_BTClient.Address = Convert.ToInt64(sAddress, 16);
                    m_BTClient.Authentication = false;
                    m_BTClient.Encryption = false;
                    m_BTClient.Channel = iChannel;
                    m_BTClient.Service = wclUUIDs.SerialPortServiceClass_UUID;

                    //-
                   
                    iResult = Radio.RemotePair(m_BTClient.Address);
                   
                    //-

                    m_bConnecting = true;
                    iResult = m_BTClient.Connect(Radio);
                    if (iResult == wclErrors.WCL_E_SUCCESS)
                    {
                        System.Threading.Thread.Sleep(500);

                        while (m_bConnecting)
                        {
                            Application.DoEvents();
                        }

                        bool bConnected2;
                        Radio.GetRemoteConnectedStatus(m_BTClient.Address, out bConnected2);

                        bConnected2 = (m_BTClient.State == wclClientState.csConnected);
                        bRes = bConnected2;
                    }
                    else
                        bRes = false;
                }
            }
            catch (Exception e)
            {
                bRes = false;
            }

            return bRes;


I also subscribed all the events:

Code: [Select]
                BTManager = new wclBluetoothManager();
                BTManager.OnPinRequest += BTManager_OnPinRequest;
                BTManager.OnAuthenticationCompleted += BTManager_OnAuthenticationCompleted;
                BTManager.OnConfirm += BTManager_OnConfirm;
                BTManager.OnPasskeyNotification += BTManager_OnPasskeyNotification;
                BTManager.OnPasskeyRequest += BTManager_OnPasskeyRequest;
                BTManager.OnProtectionLevelRequest += BTManager_OnProtectionLevelRequest;
                BTManager.OnStatusChanged += BTManager_OnStatusChanged;
                BTManager.Open();

But the only event received is OnAuthenticationCompleted with error 327722 (0x5002A) and then connect return again 327702 (0x50016).

I really don't know how to proceed and how to fix this. Your help is very much appreciated.

Thanks. regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 10:46:54 AM
m_BTClient.Authentication = true;

If you set it to false the authentication on the client side will be disabled. Also, unpair your device first.

If you want to use RemotePair() it is always good idea to call RemoteUnpair() first (without analizing returning code) to make sure device is unpaired. If a device was paired - it unpairs a device. If it was not paired the method simple returns error (and you can ignore it).

Next call to RemotePair().

Also you may leave it all to auto pairing during connect. However if device was paired and the link key was lost on any side you will get the connection (and authentication) error. In this case calling unpair may also help.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 11:30:06 AM
Thank you.

I set Authentication to true but behavior is the same as before. I also called RemoteUnpair before the RemotePair but no success.

Event OnAuthenticationCompleted is fired with error 327722 (0x5002A) and then event OnConnect with error 327702 (0x50016).


Other suggestions ?

I am sorry, should be easy and straightforward but for some reason this BT device is really different from all the others...


Thanks again. Regards.

Keven Corazza


Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 11:35:33 AM
Are you sure the device supports RFCOMM connection? Can you try to check its services using BluetoothManager demo?
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 11:47:32 AM
Here in attach a PDF with the list of services.

As said before, if I do a manual pairing with Windows and I remove from the list of associate devices the "Bluetooth LE Device" and I keep only the main one, then it works.

Thanks. Keven.
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 11:54:29 AM
All looks OK. Can you pair through this demo app?
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 12:02:17 PM
I did an additional test on another PC. Previous tests have been done in my notebook with integrated BT.

On a desktop PC, without BT, I have used an external BT adapter (SENA SD100) and with that one the RemotePair it works.

My feeling is that the problem is related somehow to the fact that this device looks to be used as a classic device but also as a LE.

I hope that above info could help you...

Thanks.

Keven Corazza

Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 12:03:48 PM
Yes, I am able to Pair with the demo app; I press Refresh button to have the status updated.

But when I have to Unpair two times and not just one.

This demo app, doesn't do any Connect, so I don't know if Connect works or not.

Thanks.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 12:08:16 PM
Yes, the problem may appear if the device uses the same MAC for LE and Classic connection. It may not work on Windows with some Bluetooth hardware.

You can pair from BluetoothManager demo and then try to connect from RfCommClient demo to make surtre the connection works.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 12:29:16 PM
Please check the image in attach.

I wasn't able to pair anymore with error 50089 or error 50015. Suddendly I was able to Pair but look at the image, has been paired as BLE and this should not be.

Now if I try to unpair, it remains paired but no more with BLE but with Classic. A second unpair is necessary.

I don't know if this make sense or if this help you to understand the issue.

Thanks.

Keven Corazza


Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 01:34:59 PM
It looks like device uses the same MAC for BLE and classic. And only after pairing it switches to the other MAC.
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 01:59:35 PM
By the way, try to change Io Capabilities to iocapDisplayKeyboard.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 02:26:57 PM
Hi,

I have modified the demo as follow:

Code: [Select]
        private void btUnpair_Click(object sender, EventArgs e)
        {
            m_BTClient.Disconnect();


            if (lvDevices.SelectedItems.Count == 0)
                MessageBox.Show("Select device", "Warning", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            else
            {
                ListViewItem Item = lvDevices.SelectedItems[0];
                wclBluetoothRadio Radio = (wclBluetoothRadio)Item.Tag;
                Int64 Address = Convert.ToInt64(Item.SubItems[1].Text, 16);

                Int32 Res = Radio.RemoteUnpair(Address);
                if (Res != wclErrors.WCL_E_SUCCESS)
                    ShowError(Res);

                RefreshDevice(Item);
            }
        }

        private void btPair_Click(object sender, EventArgs e)
        {
            if (lvDevices.SelectedItems.Count == 0)
                MessageBox.Show("Select device", "Warning", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            else
            {
                ListViewItem Item = lvDevices.SelectedItems[0];
                wclBluetoothRadio Radio = (wclBluetoothRadio)Item.Tag;
                Int64 Address = Convert.ToInt64(Item.SubItems[1].Text, 16);

                Int32 Res = Radio.RemotePair(Address);
                if (Res != wclErrors.WCL_E_SUCCESS)
                    ShowError(Res);
                else
                {
                    m_BTClient.Address = Address;
                    m_BTClient.Authentication = false;
                    m_BTClient.Encryption = false;
                    //m_BTClient.Timeout = 10;
                    m_BTClient.Channel = 11;
                    m_BTClient.Service = wclUUIDs.SerialPortServiceClass_UUID;

                    int iResult = m_BTClient.Connect(Radio);
                    if (iResult == wclErrors.WCL_E_SUCCESS)
                    {
                    }
                }
            }
        }

        private void BTClient_OnConnect(object Sender, int Error)
        {
            if (Error == wclErrors.WCL_E_SUCCESS)
            {
                MessageBox.Show("CONNECTED!", "Warning", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show("ERROR: " + Error.ToString(), "Warning", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
        }



Looking at the behaviors:

* RemotePair: two pairing are done, one for Classic and one for LE
* Connect: it looks that connection on Classic is done but if you query the status GetRemoteConnectedStatus it return false because LE is checked.

Is it possible to have one single behavior by excluding LE ?

I am not able to make it working in a reliable and repetitive way. I really need an help on your side.

Thanks. Regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 02:33:50 PM
Unfortunatel there is nothing we can do while device uses single MAC for classic and LE.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 03:14:28 PM
I have followed your suggestion to set  Io Capabilities to iocapDisplayKeyboard.

It doesn't work but now I am not able to discover the device. Neither from your demo application.

Switched off and on, doesn't help.

Any idea if IoCapabilities changed something in the device ?

Thanks. Regards.

Keven Corazza


Title: Re: Connection - Error 0x50041
Post by: KCorazza on June 30, 2021, 03:30:17 PM
It is my PC that is no more able to discover the device. I can do from other PCs but no more from this one.

Even after a restart...

Regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on June 30, 2021, 06:55:34 PM
Probably there is something with your Bluetooth adapter. IoCaps does nothing except selecting pairing methods.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on July 01, 2021, 07:46:39 AM
Good morning,

me again. I confirm that since the time that I tried to change IoCapabilities parameter, my PC is no more able to discover the BT device. Same application on another PC is able to discover the device.

Do you save some settings in some registry  ?
Why is this possible ?
How can I restore and return to the previous situation in which I was able to discover the device ?

Thank you in advance for your help.

Regards.

Keven Corazza

Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on July 01, 2021, 07:51:54 AM
Hi,

Changing IoCap does nothing with registry as well as with any other settings. The IoCap affects only pairing methods fflags used in call to system Pair(). Nothing more.

probably you have third-party Bluetooth driver installed on this PC that blocks system calls (it is more or less common situation).
Title: Re: Connection - Error 0x50041
Post by: KCorazza on July 01, 2021, 07:58:48 AM
Ok,

any suggestion on how to recover from this situation ? I am really blocked and I cannot use your library in this PC.

Thanks. Keven.

Title: Re: Connection - Error 0x50041
Post by: KCorazza on July 01, 2021, 08:00:05 AM
By the way, BT adapter is integrated in the notebook, we are not talking about an external BT adapter with drivers and so on.

Thanks. Regards.

Keven.
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on July 01, 2021, 08:04:03 AM
Any BT adapter requires driver (as any hardware does). Some vendors installs third-party Bluetooth driver extension. Some adapter (usually on Toshiba) uses 2-in-1 WiFi/BT modules that also requires filter driver.

Make sure there is no any third party Bluetooth drivers installed. Delete MS BT driver from Device Manager and let Windows reinstalls it.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on July 01, 2021, 08:52:40 AM
Sorry if I insist, but I have just installed another BT library (IPWorks Bluetooth 2020 .NET Edition Website  Support from nsSoftware) and it works. I am able to discover my device and connect.

I don't know where is the problem but it looks something related to your library.

I bought your library just two weeks ago and I would keep it and don't have to switch to another library.

Please let me know. Thanks.

Regards. Keven.
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on July 01, 2021, 09:05:03 AM
First at all IPWorks you referred to supports only Classic devices and does not support BLE. But you were talking about BLE discovering and connection. It is absolutely different things.

It looks like you are doing something wrong. If you have TeamViewer lets check what can be wrong on your side.
Title: Re: Connection - Error 0x50041
Post by: KCorazza on July 01, 2021, 11:00:34 AM
Hi,

this device supports both but I am not interested to BTLE. I always talked about Classic connection.

If you provide me an e-mail I can send you a Microsoft Teams invitation so we can speak, you can view my screen and so on.

Thanks. Regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on July 01, 2021, 11:03:58 AM
Its better to use TeamViewer. You can e-mail to support@btframework.com
Title: Re: Connection - Error 0x50041
Post by: KCorazza on July 01, 2021, 02:01:27 PM
Hi,

I sent you the invitation to join the call in Teams.

Thanks. Regards.

Keven Corazza
Title: Re: Connection - Error 0x50041
Post by: Mike Petrichenko on July 01, 2021, 03:58:28 PM
Hi,

Sorry, we have been on meeting. Please do not publish TeamViewer (or any other private) information here. Send to support@btframework.com or directly to me mike@btframework.com