Wireless Communication Library Support Forum

Frameworks => Bluetooth Framework => Topic started by: Leo1986 on April 18, 2020, 12:49:49 PM

Title: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 18, 2020, 12:49:49 PM
Hi Mike,

I have created a hosted service in ASP.Net Core which continuously scans for devices and reads a couple of characteristics from each one. It mostly works, but sometimes, it seems that the service can't connect to some clients. The firmware on the devices has been built by a novice programmer so I have no idea if it's their firmware or something I am doing wrong.

A little background: I have a service class which which has a method Run (this is executed by the ExecuteAsync method in the hosted service) and I have a device class which starts connecting to devices via a Run method. The service class contains a List<Task> to hold all the device classes and a SemaphoreSlim to wait until all the device classes have finished connecting and reading characteristics. I use Task.WaitAll to wait until all device classes have finished. Each device class also has a SemaphoreSlim so the Task.WaitAll knows that it is still processing.

I have no idea if this approach is wrong as I did it a little different than your examples, which use wclThread to handle concurrent connections. Can you please advice?
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 18, 2020, 01:04:44 PM
Hi,

Take a look on this post: https://forum.btframework.com/index.php?topic=3520.msg9021#msg9021

Also take a look on Apc and Console demos to find out how to use Bluetooth Framework in async operations/threads.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 01:57:53 PM
Hi Mike,

I have taken your APC demo and modified it a little so I can connect to multiple clients sequentially. I don't use threads or anything and I still get the same errors. The devices I am trying to connect to have a button which the user can press x times. If the user has finished pressing the buttons, the device starts sending notifications with how many times the button was pressed. It does this for 1 minute, then it turns off it's BLE module and stops sending.

At this point, I think the problem lies with the device. It's firmware was custom-built by our junior dev and I think they are not closing the connection properly on their end. Or maybe it has to do with the Windows BT stack. I have attached the modified demo, can you please confirm that this should be working?

Thank you.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 19, 2020, 02:06:59 PM
Using skNone is not recommended. Use skApc or skDefault.

Wahiting for connection (state changes) is also bad idea. Use OnConnect event. You can use this with skApc and manual reset events objects that are set when OnConnect fired.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 02:57:38 PM
I just noticed that it was the GattClient demo, sorry about that. Nonetheless, I change it to APC, removed the ManualResetEvent and added the WaitOperation method you used in your APC demo and got the same result. It seems no matter what I try, I always randomly get the exact same error while trying to connect. I will try to shed some light on this on Tuesday when I get back to work. Thank you again.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 19, 2020, 03:09:34 PM
I am creating test/demo code for you. Give me some time for that.

Actually errors mean:

* 0X00030008 - connection close. You are trying to communicate with not connected device.
* 0X0005105C - device not found. The device is not available. It may appears if device was discovered but also was turned off after it has been discovered but before connect operation executed.

All errors list is there: https://www.btframework.com/errors.htm

I'll upload demo code as new post/answer soon.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 03:52:17 PM
I also tried your ConcurrentBleConnections demo from here: https://github.com/btframework/ConcurrentBleConnections (https://github.com/btframework/ConcurrentBleConnections).

I still get the same errors as before. I only get 0X0005105C when the device is found in FManager_OnDeviceFound event and turns itself off right after that. So it's expected the client can't connect to it when the FManager_OnDiscoveringCompleted event fires and doesn't seem to be a problem. But I also get 0X00030008 randomly, I didn't even change a line of code from your example. So I guess unless you changed something in the library that hasn't been adjusted in the demo vb app, the problem definitely lies with the device's firmware.

If you could provide an example, that would be great. But I don't want to bother you too much, so only if it's not a problem. :)
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 19, 2020, 04:36:40 PM
Hi,

Here is the test/demo code.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 05:11:31 PM
I tested your demo and it works as expected. Sadly, I still get random error 0X00030008 when trying to connect. When starting discovering, the gatt client sees the device and tries to connect to it. It then subscribes, receives values a few times and then errors out. Sometimes, it doesn't even manage to subscribe or even connect. After erroring out, it is discovered again by the client and the whole process repeats itself. It sometimes manages to stay subscribed until the device turns itself off. So it is very inconsistent, which I can confirm when I did my tests before.



Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 19, 2020, 05:22:13 PM
If you got this error inside OnDisconnected() then it means that device closed the connection.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 05:39:45 PM
It happens mostly in Client_OnConnect event so it doesn't even manage to connect or the connection is closed right after opening.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 19, 2020, 06:08:13 PM
Actually there is no way to get WCL_E_CONNECTION_CLOSED error inside OnConnect event handler. This error reported only in OnDisconnect event handler.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 06:31:10 PM
Please see this image: https://imgur.com/KGZj0qE (https://imgur.com/KGZj0qE)

Unless I am missing something here, that error seems to coming into the Client_OnConnect event. I tried to connect two devices, one connected successfully, that's the one which is sending all the values. The other errored out as you can see in the image.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 19, 2020, 06:41:40 PM
Interesting. But any way, it means that a remote device terminates connection (it sends disconnect right after connect request).
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 19, 2020, 07:06:26 PM
Yes, it is strange indeed, especially if this a really rare error. I am currently chatting with our dev who wrote the firmware running on the devices. They said that they should "probably secure the advertising mode" of the gatt server they wrote. I have no idea what that means but I will send them an .exe of my service and hope they figure it out.

Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 20, 2020, 10:17:02 AM
Hi,

I have updated the code by adding some delay (2 seconds) aftr device disconnected. So it will not connect immediately and gives device 2 seconds to restore its state after disconnection. I think that was a problem with your device.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 20, 2020, 07:25:57 PM
Hi Mike,

sadly, I still get the same error. Our junior dev debugged his firmware and they said that the service tries to connect to the device, then after connected immediately disconnects and then tries to connect again. As if it is stuck inside a loop.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 20, 2020, 07:29:06 PM
Hi,

Unfortunately I have no idea how your device works but I tested it here with Lego (2 WeDo Hubs and 2 BOOST hubs), Raspberry pi running bleno and Android cells running BLE Emulator and smart bracellet. Total with 10 devices running at the same time. No one issue.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 20, 2020, 07:35:23 PM
Yes, I can confirm that. Just to check, did you also try connecting and disconnecting then connecting again? I tested it with an android app where I can open a GATT server and the service has no problems connecting and disconnecting, even if I do it over and over rapidly. So, the problem lies either with the BT chip or the firmware.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 20, 2020, 09:15:58 PM
Yes, just tested it also. No problem. Try to increase delay between reconnect. I used 2 seconds but may be device needs more.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 20, 2020, 11:33:05 PM
I tried it with a delay of 10 seconds, I also tried using a different bluetooth adapter and screwing around with drivers. All yielded the same results. I sent the service to my colleagues because they also have some of our devices lying around, they also get the same error. We use a Microchip RN4871 and I told our junior dev to post on their forums, maybe he configured the firmware incorrectly. Hopefully, that will yield some results as I have no ideas left on what to do.

Very strange issue.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 21, 2020, 07:58:41 PM
Hi Mike,

Our junior dev made a few changes to their firmware and I also made some changes in the service. Before, when I received a value in OnCharacteristicChanged, I sent the shutdown signal to the device inside that event. I think the service didn't like that. So I refactored a bit and now I unsubscribe the characteristic and send the shutdown signal immediately after subscribing. I also brought home a high-quality bluetooth dongle instead of using the onboard adapter of my 2015-ish laptop.

I don't get 0X00030008 errors anymore, just 0X0005105C and I think this has to do with interference from other devices because the fewer devices I see when scanning in Windows (at night, early morning), the more rare the 0X0005105C errors become. But these errors aren't game-breaking because the next scan cycle picks up the device again and connects to it successfully.

Anyway, now I have another problem. I do not receive any values anymore because obviously I unsub immediately (on line 154 in the file attached file) from the characteristic. Is there any way to wait for the first value to come into OnCharacteristicChanged event before doing this?
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 21, 2020, 08:46:34 PM
Hi,

That is correct that you do not receive notifications because you unsubscribed.

There should no be any issue with writing inside value changed event handler (we do it in our WeDo framework without any problem: https://github.com/btframework/WeDo)

Also it looks like your device has random MAC or you did not filter device's correctly. That is why you got 0X0005105C  error.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 22, 2020, 08:28:23 AM
By the way, I have been thinking about the issue with your device and: Is your device still advertising when you connect to it?
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 22, 2020, 10:07:05 AM
Hi Mike,

The workflow of our service and devices looks like this:


So in other words, the device should not advertise when the service tries to connect to it again. The 5 second delay should ensure that all devices have turned off and stopped advertising.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 22, 2020, 10:15:52 AM
For me (I can guess) it looks like device keeps advertising when your service connects to it. As my code stores connected MAC even advertising received again it does not try to connect after first connection attempt. However, once you disconnects from device and device keeps advertising (and probably changes it MAC) the code sees the device is available and tries to connect to it again. However device may be in "disconnecting" state and rejects new connection immediately so you got hte connection error.

Usually BLE device stops advertising right after connection established and starts advertising again only when it "completly" disconnected.

So make sure your device is not advertising when your service connected to it first. Then we can think what to do with it.
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Leo1986 on April 29, 2020, 10:47:50 AM
Hi Mike,

we still haven't managed to solve the problem. We will now try to debug the firmware of our devices to see what is happening when exactly. Independently, I wanted to ask you if your library supports transparent UART?
Title: Re: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C
Post by: Mike Petrichenko on April 29, 2020, 10:52:45 AM
Bluetooth Framework supports any GATT service. It does not matter how it is implemented on device's side if it follows BLE GATT service specification.