Beispiel #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);
                }
            }

            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);
        }
Beispiel #2
0
        public static Task <IDisposable> WatchDevicesAddedAsync(this IAdapter1 adapter, Action <Device> handler)
        {
            async void OnDeviceAdded((ObjectPath objectPath, IDictionary <string, IDictionary <string, object> > interfaces) args)
            {
                if (BlueZManager.IsMatch(BluezConstants.DeviceInterface, args.objectPath, args.interfaces, adapter))
                {
                    var device = Connection.System.CreateProxy <IDevice1>(BluezConstants.DbusService, args.objectPath);

                    var dev = await Device.CreateAsync(device);

                    handler(dev);
                }
            }

            var objectManager = Connection.System.CreateProxy <IObjectManager>(BluezConstants.DbusService, "/");

            return(objectManager.WatchInterfacesAddedAsync(OnDeviceAdded));
        }
Beispiel #3
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)));
        }