Exemplo n.º 1
0
        public static async Task <Device> GetDeviceAsync(this IAdapter1 adapter, string deviceAddress)
        {
            var devices = await BlueZManager.GetProxiesAsync <IDevice1>(BluezConstants.DeviceInterface, adapter);

            var matches = new List <IDevice1>();

            foreach (var device in devices)
            {
                if (String.Equals(await device.GetAddressAsync(), deviceAddress, StringComparison.OrdinalIgnoreCase))
                {
                    matches.Add(device);
                }
            }

            // BlueZ can get in a weird state, probably due to random public BLE addresses.
            if (matches.Count > 1)
            {
                throw new Exception($"{matches.Count} devices found with the address {deviceAddress}!");
            }

            var dev = matches.FirstOrDefault();

            if (dev != null)
            {
                return(await Device.CreateAsync(dev));
            }
            return(null);
        }
Exemplo n.º 2
0
        public static async Task <IGattService1> GetServiceAsync(this IDevice1 device, string serviceUUID)
        {
            var services = await BlueZManager.GetProxiesAsync <IGattService1>(BluezConstants.GattServiceInterface, device);

            foreach (var service in services)
            {
                var uuid = await service.GetUUIDAsync();

                // Console.WriteLine($"Checking {uuid}");
                if (String.Equals(uuid, serviceUUID, StringComparison.OrdinalIgnoreCase))
                {
                    return(service);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public static async Task <List <string> > GetAllCharacteristicAsync(this IGattService1 service)
        {
            var result          = new List <string>();
            var characteristics =
                await BlueZManager.GetProxiesAsync <IGattCharacteristic1>(BluezConstants.GattCharacteristicInterface,
                                                                          service);

            Debug.WriteLine($"{characteristics.Count} characteristics found");
            foreach (var characteristic in characteristics)
            {
                var uuid = await characteristic.GetUUIDAsync();

                //var ch = await GattCharacteristic.CreateAsync(characteristic);
                result.Add(uuid);
            }

            return(result);
        }
Exemplo n.º 4
0
        public static async Task <GattCharacteristic> GetCharacteristicAsync(this IGattService1 service, string characteristicUUID)
        {
            var characteristics = await BlueZManager.GetProxiesAsync <IGattCharacteristic1>(BluezConstants.GattCharacteristicInterface, service);

            foreach (var characteristic in characteristics)
            {
                var uuid = await characteristic.GetUUIDAsync();

                // Console.WriteLine($"Checking {uuid}");
                if (String.Equals(uuid, characteristicUUID, StringComparison.OrdinalIgnoreCase))
                {
                    var ch = await GattCharacteristic.CreateAsync(characteristic);

                    return(ch);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public static async Task <IReadOnlyList <Device> > GetDevicesAsync(this IAdapter1 adapter)
        {
            var devices = await BlueZManager.GetProxiesAsync <IDevice1>(BluezConstants.DeviceInterface, adapter);

            return(await Task.WhenAll(devices.Select(Device.CreateAsync)));
        }
Exemplo n.º 6
0
 public static Task <IReadOnlyList <IGattService1> > GetServicesAsync(this IDevice1 device)
 {
     return(BlueZManager.GetProxiesAsync <IGattService1>(BluezConstants.GattServiceInterface, device));
 }
Exemplo n.º 7
0
 public static Task <IReadOnlyList <IGattCharacteristic1> > GetCharacteristicsAsync(this IGattService1 service)
 {
     return(BlueZManager.GetProxiesAsync <IGattCharacteristic1>(BluezConstants.GattCharacteristicInterface,
                                                                service));
 }