Timeular / noble-winrt

Noble Windows 10 bindings using the official C++/WinRT API
MIT License
20 stars 25 forks source link

Is it possible to manually add a service/char to the winrt cache? #20

Closed pursual closed 4 years ago

pursual commented 4 years ago

Noble has a caching mechanism for services/chars. See the examples folder for cache-gatt. For a binding to support this, it needs to have an addService and addCharacteristic method.

Do we know if it would be possible to create a windows GattDeviceService or GattCharacteristic manually, and inject it into cachedServices/Chars?

I looked at service, and the noble representation is

 {
      "startHandle": 13,
      "endHandle": 21,
      "uuid": "a3e68a834c4d4778bd0a829fb434a7a1"
}

I was unable to determine how I would create this manually from looking at: https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.genericattributeprofile.gattdeviceservice?view=winrt-19041

I assume you would need to call some GattDeviceService constructor, and then somehow set the uuid and handles? Sorry, C++ is not my forte.

geovie commented 4 years ago

Hi, we already cache services and characteristics after the first access see here

PeripheralWinrt::GetService will first check if the service is already in the cache and if not it will ask windows to get the service 'from the device' and cache it

pursual commented 4 years ago

Yes, I'm referring to manually adding to the cache, without any access at all to the device. This is useful in situations where you are always connecting to the same type of device, and want to import the services from a file, as opposed to having to discover them. It's faster. You can see how it works in the examples I mentioned in noble.

geovie commented 4 years ago

Windows takes care of the services, what you could use is GetGattServicesForUuidAsync with BluetoothCacheMode = Cached to try to get the service without asking the device and if that fails try it with BluetoothCacheMode = Uncached

pursual commented 4 years ago

This is a different concept. This is telling winrt what services exist, not asking it. Please look at the example code in noble.

geovie commented 4 years ago

Could you provide a link to the code? I can't find in the noble example directory.

Anyway unfortunately I don't think that this is possible with winrt as Windows takes care of the service discovery and caching.

pursual commented 4 years ago

Sorry, its in the @abandonware fork, which is probably the most popular now. https://github.com/abandonware/noble/tree/master/examples

I came to the same conclusion that it might be hard to create a windows service object outside of discovery. There does appear to be a few constructors.

pursual commented 4 years ago

Closing, this is probably not possible with any binding other than the hci.