示例#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);
        }
示例#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);
        }
示例#3
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);
        }
示例#4
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)));
        }