Skip to content

Commit

Permalink
Merge pull request #2 from SuessLabs/feature/Battery
Browse files Browse the repository at this point in the history
Device Battery Levels
  • Loading branch information
DamianSuess authored Jul 26, 2022
2 parents 81171fb + f7ec92e commit f2ddd39
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 46 deletions.
18 changes: 18 additions & 0 deletions src/Linux.Bluetooth/Constants/BluezConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Linux.Bluetooth
{
public static class BluezConstants
{
public const string DbusService = "org.bluez";

public const string AdapterInterface = "org.bluez.Adapter1";
public const string BatteryInterface = "org.bluez.Battery1";
public const string DeviceInterface = "org.bluez.Device1";
public const string GattServiceInterface = "org.bluez.GattService1";
public const string GattManagerInterface = "org.bluez.GattManager1";
public const string GattCharacteristicInterface = "org.bluez.GattCharacteristic1";
public const string GattDescriptorInterface = "org.bluez.GattDescriptor1";
public const string LeAdvertisementInterface = "org.bluez.LEAdvertisement1";
public const string LeAdvertisingManagerInterface = "org.bluez.LEAdvertisingManager1";
}

}
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
namespace Linux.Bluetooth
{
public static class BluezConstants
{
public const string DbusService = "org.bluez";
public const string AdapterInterface = "org.bluez.Adapter1";
public const string DeviceInterface = "org.bluez.Device1";
public const string GattServiceInterface = "org.bluez.GattService1";
public const string GattManagerInterface = "org.bluez.GattManager1";
public const string GattCharacteristicInterface = "org.bluez.GattCharacteristic1";
public const string GattDescriptorInterface = "org.bluez.GattDescriptor1";
public const string LeAdvertisementInterface = "org.bluez.LEAdvertisement1";
public const string LeAdvertisingManagerInterface = "org.bluez.LEAdvertisingManager1";
}

// https://www.bluetooth.com/specifications/gatt/

public static class GattConstants
{
//// Device Information

public const string DeviceInformationServiceUUID = "0000180a-0000-1000-8000-00805f9b34fb";
public const string ModelNameCharacteristicUUID = "00002a24-0000-1000-8000-00805f9b34fb";
public const string ManufacturerNameCharacteristicUUID = "00002a29-0000-1000-8000-00805f9b34fb";

//// Current Time

public const string CurrentTimeServiceUUID = "00001805-0000-1000-8000-00805f9b34fb";
public const string CurrentTimeCharacteristicUUID = "00002a2b-0000-1000-8000-00805f9b34fb";

// Battery Service
// BlueZ presents this service a separate interface, Battery1.
// public const string BatteryServiceUUID = "0000180f-0000-1000-8000-00805f9b34fb";
// public const string BatteryLevelCharacteristicUUID = "00002a19-0000-1000-8000-00805f9b34fb";

// Apple Notification Center Service (ANCS)
// https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/AppleNotificationCenterServiceSpecification/Introduction/Introduction.html
public const string ANCServiceUUID = "7905f431-b5ce-4e99-a40f-4b1e122d00d0";

// TODO: Lowercase these.
public const string ANCSNotificationSourceUUID = "9FBF120D-6301-42D9-8C58-25E699A21DBD";
public const string ANCSControlPointUUID = "69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9";
public const string ANCSDataSourceUUID = "22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB";
}
}
namespace Linux.Bluetooth.Constants
{
/// <summary>
/// GATT Constants based on, https://www.bluetooth.com/specifications/gatt/
/// </summary>
public static class GattConstants
{
//// Device Information

public const string DeviceInformationServiceUUID = "0000180a-0000-1000-8000-00805f9b34fb";
public const string ModelNameCharacteristicUUID = "00002a24-0000-1000-8000-00805f9b34fb";
public const string ManufacturerNameCharacteristicUUID = "00002a29-0000-1000-8000-00805f9b34fb";

//// Current Time

public const string CurrentTimeServiceUUID = "00001805-0000-1000-8000-00805f9b34fb";
public const string CurrentTimeCharacteristicUUID = "00002a2b-0000-1000-8000-00805f9b34fb";

// Battery Service
// BlueZ presents this service a separate interface, Battery1.
// public const string BatteryServiceUUID = "0000180f-0000-1000-8000-00805f9b34fb";
// public const string BatteryLevelCharacteristicUUID = "00002a19-0000-1000-8000-00805f9b34fb";

// Apple Notification Center Service (ANCS)
// https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/AppleNotificationCenterServiceSpecification/Introduction/Introduction.html
public const string ANCServiceUUID = "7905f431-b5ce-4e99-a40f-4b1e122d00d0";

// TODO: Lowercase these.
public const string ANCSNotificationSourceUUID = "9FBF120D-6301-42D9-8C58-25E699A21DBD";

public const string ANCSControlPointUUID = "69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9";
public const string ANCSDataSourceUUID = "22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB";
}
}
31 changes: 30 additions & 1 deletion src/Linux.Bluetooth/Extensions/DeviceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ namespace Linux.Bluetooth.Extensions
{
public static class DeviceExtensions
{
/// <summary>Get battery information for the </summary>
/// <param name="device">Device object.</param>
/// <example>
/// var battery = await device.GetBatteryAsync();
/// var percentage = await battery.GetPercentageAsync();
/// </example>
/// <returns>Battery or null if unavailable.</returns>
public static async Task<IBattery1> GetBatteryAsync(this IDevice1 device)
{
try
{
// TODO: Create Battery class with OnPropertyChanges for event notification subscriptions
return await GetBatteryInternalAsync(BluezConstants.BatteryInterface, device);
}
catch
{
return null;
}
}

/// <summary>Get a GATT Service with the supplied Service UUID.</summary>
/// <param name="device">Device object.</param>
/// <param name="serviceUuid">UUID of the Service.</param>
Expand All @@ -33,7 +53,7 @@ public static async Task<IGattService1> GetServiceAsync(this IDevice1 device, st
/// <returns>Collection of Gatt Services.</returns>
public static Task<IReadOnlyList<IGattService1>> GetServicesAsync(this IDevice1 device)
{
return BlueZManager.GetProxiesAsync<IGattService1>(BluezConstants.GattServiceInterface, device);
return BlueZManager.GetProxiesAsync<IGattService1>(BluezConstants.GattServiceInterface, device);
}

/// <summary>Wait for Device's Property and specified value to resolve.</summary>
Expand Down Expand Up @@ -104,5 +124,14 @@ private static (Task, IDisposable) WaitForPropertyValueInternal<T>(IDevice1 obj,

return (taskSource.Task, watcher);
}

private static async Task<IBattery1> GetBatteryInternalAsync(string batteryInterface, IDevice1 device)
{
var battery = await Task.Run(() =>
Connection.System.CreateProxy<IBattery1>(BluezConstants.DbusService, device.ObjectPath)
);

return battery;
}
}
}

0 comments on commit f2ddd39

Please sign in to comment.