Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to cast object of type 'Linux.Bluetooth.IGattCharacteristic1Proxy' to type 'Linux.Bluetooth.GattCharacteristic' #408

Open
SydneyOwl opened this issue Apr 13, 2024 · 3 comments
Labels
bug Linux Linux or BlueZ related

Comments

@SydneyOwl
Copy link
Contributor

SydneyOwl commented Apr 13, 2024

Hello,
When trying to get all characteristics of service this will always occur on Ubuntu 22.04 but on windows won't:
image
Here's the code:

await device.Gatt.ConnectAsync();
        Console.WriteLine("Connected");
        var service = await device.Gatt.GetPrimaryServicesAsync();
        bool valid = false;
        if(service.Count!=0)
        {
            foreach (var gattService in service)
            {
               var characteristics =  await gattService.GetCharacteristicsAsync();        <----------------------Error cccurs here
               foreach (var gattCharacteristic in characteristics)
               {
                   Console.WriteLine(gattCharacteristic.Uuid.ToString());
                   if (gattCharacteristic.Uuid.ToString().ToLower().Contains(BLE.RW_CHARACTERISTIC_UUID))
                   {
                       characteristic = gattCharacteristic;
                       break;
                   }
               }
            }

and here's the sourcecode of PlatformGetCharacteristics:
image

Wondering if any one knows how to solve this? Thanks in advance!

@peterfoot peterfoot added Linux Linux or BlueZ related bug labels Apr 13, 2024
@SydneyOwl
Copy link
Contributor Author

SydneyOwl commented Apr 14, 2024

solved this issue by using GetPrimaryServiceAsync and GetCharacteristicAsync directly in my code. Here's what it looks like:

var service = await device.Gatt.GetPrimaryServiceAsync(
    BluetoothUuid.FromShortId(Convert.ToUInt16(BLE.RW_SERVICE_UUID.ToUpper(), 16)));
var character = await service.GetCharacteristicAsync(
    BluetoothUuid.FromShortId(Convert.ToUInt16(BLE.RW_CHARACTERISTIC_UUID.ToUpper(), 16)));

But then I stumbled upon a problem when I tried calling StartNotificationsAsync on Linux. It kept throwing a NotSupportedException, saying 'properties not supported':

character.CharacteristicValueChanged += Characteristic_CharacteristicValueChanged;
await character.StartNotificationsAsync();

The thing is, when I looked into the GattCharacteristic.linux.cs file, I noticed that the GetProperties method always returns a zero value. This means, no matter what device we're dealing with, it'll inevitably fail to register a callback function because of this:

GattCharacteristicProperties GetProperties()
{
    return 0;
}

Maybe this BLE library hasn't fully implemented support for Linux yet?

@peterfoot
Copy link
Member

I think there are a couple of issues which need addressing. The casting issue can be worked around by using the IGattCharacteristic1 throughout rather than the GattCharacteristic helper as it has no public constructor. This will require implementing the logic around property change notification to hook up the events.
The cross-platform code implemented a check that the characteristic supports either Notify or Indicate properties to allow you to start monitoring change events and this breaks the Linux implementation because it doesn't have a direct equivalent of the Properties property. However I believe the IGattCharacteristic1 now has a Flags property with these flags as strings so this can be converted to enum values. I don't have my Linux development environment setup but will look into this soon and hopefully get these issues resolved.

@DamianSuess
Copy link

DamianSuess commented Sep 4, 2024

@peterfoot, is there anything you'd like me to address/update on the Linux.Bluetooth library?

Let me know, I'm ramping up for the 6.0-pre release. If you'd like, we can do a video call too 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Linux Linux or BlueZ related
Projects
None yet
Development

No branches or pull requests

3 participants