Author Topic: Getting services from a device which is not connected  (Read 5212 times)

Offline decay

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Getting services from a device which is not connected
« on: July 24, 2020, 09:57:49 AM »
I am trying out the library (.Net version), and I know the device I want to connect to by MAC address, and when I call
Code: [Select]
wclGattClient.Connect it works fine and returns no error even when the device is not turned on, but when I go to retrieve services from the device via
Code: [Select]
wclGattClient.ReadServices() with the flag
Code: [Select]
wclGattOperationFlag.goReadFromCache or any of the other two flags, the returned services are null and the error returned is
Code: [Select]
WCL_E_CONNECTION_NOT_ACTIVE.  If the device is powered on, then it gets services.  The problem is that this device will be powered off most of the time, and then turned on.  I want to read a characteristic from the device when it executes its function after it is turned on.

In the demo app, I noticed the discovery process does not pick up the device even when I specify reading from cache.

Using the standard UWP and even the native Win32 BLE, this is not a problem, but it is a lot more code, which is why I am exploring this library.  For instance, the Windows 10 BLE Explorer sees the device during enumeration when it is not turned on.  Is this a limitation of BTFramework? Or I am missing something

Thanks.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Getting services from a device which is not connected
« Reply #1 on: July 24, 2020, 11:24:27 AM »
Hello,

First make sure that the Connect On Read is not checked (in demo app).

Bluetooth Framework provides 2 "connection methods". The first one (when ConnectOnRead is false) executes real connection to the device during connection procedure. The second one (when ConnectOnRead is true and this is default behaviour of any Windows GATT API) when connection executed on first service read attempt.


The WCL_E_CONNECTION_NOT_ACTIVE erro rmeans that you did not connected to device (https://www.btframework.com/errors.htm). It may appears when you call ReadService right after Connect() and does not wait for OnConnect event.

You should always process OnConnect and analize connection result (the Error parameter) and only if Error is WCL_E_SUCCESS you can read services and other things.

You can take a look on how it should be done by this link: https://github.com/btframework/WeDo/blob/master/NET%20Edition/WeDo%20Framework/wclWeDoHub.cs
Also there you can find how to find BLE devices without discovering: https://github.com/btframework/WeDo/blob/master/NET%20Edition/WeDo%20Framework/wclWeDoWatcher.cs

Offline decay

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Getting services from a device which is not connected
« Reply #2 on: July 24, 2020, 08:01:25 PM »

First make sure that the Connect On Read is not checked (in demo app).

It's not.  It is paired with the computer and does not show up unless it is powered up before the discovery process.

Quote
The WCL_E_CONNECTION_NOT_ACTIVE erro rmeans that you did not connected to device (https://www.btframework.com/errors.htm). It may appears when you call ReadService right after Connect() and does not wait for OnConnect event.

You should always process OnConnect and analize connection result (the Error parameter) and only if Error is WCL_E_SUCCESS you can read services and other things.


The OnConnect event fires just fine, but the Error parameter is 331868 when the device is powered off


Quote
You can take a look on how it should be done by this link: https://github.com/btframework/WeDo/blob/master/NET%20Edition/WeDo%20Framework/wclWeDoHub.cs
Also there you can find how to find BLE devices without discovering: https://github.com/btframework/WeDo/blob/master/NET%20Edition/WeDo%20Framework/wclWeDoWatcher.cs

I will look.  Thanks.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Getting services from a device which is not connected
« Reply #3 on: July 24, 2020, 08:04:46 PM »
That is absolutely correct behaviour when ConnectOnRead is set to false. Because Bluetooth Framework executes real connection to device. So if device is truend off it can't connect to it.

Set ConnectOnRead  to true and then connection will work. However read services will be failed if device is turned off.

P.S. It is bad idea to work with paired device on Windows platform. Pair with it only when it is really required. Andpair only when needed. Do not pair with any other cases.

Offline decay

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Getting services from a device which is not connected
« Reply #4 on: July 24, 2020, 08:39:36 PM »
If I check "Connect On Read" in the demo app, it still does not show up in discovery. 

Unfortunately, I don't think the library is going to work for our use case. 

As far as working with paired devices on Windows, it has been both very stable in UWP (see Windows Bluetooth LE Explorer) and the lower level Win32 BLE code.  It is just a lot of extra code I was hoping to avoid, especially if we bring regular Bluetooth devices under our umbrella.  I was initially trying this in Linux, and it was losing pairing on a regular basis.  I have had no such problems in Windows.

Thanks for your help.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Getting services from a device which is not connected
« Reply #5 on: July 24, 2020, 08:44:54 PM »
Of course it does not because device is turned off. You have to enumerate paired devices. Refer to Bluetooth Manager to find out how to enumerate paired devices.


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Getting services from a device which is not connected
« Reply #6 on: July 24, 2020, 10:29:19 PM »
I created a simple application that shows how to do what you need with Bluetooth Framework: https://github.com/btframework/PairedGatt

Copy Bluetooth Framework DLLs into Lib folder.

 

Sitemap 1 2 3 4 5 6 7