Author Topic: Connecting to multiple clients, sometimes getting 0X00030008 and 0X0005105C  (Read 10350 times)

Leo1986

  • Guest
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?
« Last Edit: April 18, 2020, 12:53:55 PM by Leo1986 »

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
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.

Leo1986

  • Guest
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.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
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.

Leo1986

  • Guest
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.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
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.

Leo1986

  • Guest
I also tried your ConcurrentBleConnections demo from here: 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. :)

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Hi,

Here is the test/demo code.

Leo1986

  • Guest
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.




Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
If you got this error inside OnDisconnected() then it means that device closed the connection.

Leo1986

  • Guest
It happens mostly in Client_OnConnect event so it doesn't even manage to connect or the connection is closed right after opening.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Actually there is no way to get WCL_E_CONNECTION_CLOSED error inside OnConnect event handler. This error reported only in OnDisconnect event handler.

Leo1986

  • Guest
Please see this image: 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.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Interesting. But any way, it means that a remote device terminates connection (it sends disconnect right after connect request).

Leo1986

  • Guest
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.


 

Sitemap 1 2 3 4 5 6 7