Exemple #1
0
        private async Task <ManagedObject[]> GetObjectsWithInterface(string @interface)
        {
            var objects = await _objectManager.GetManagedObjectsAsync().ConfigureAwait(false);

            return(objects
                   .Where(x => x.HasInterface(@interface))
                   .Select(x => new ManagedObject(_systemConnection, x.Key, x.Value))
                   .ToArray());
        }
        async Task StartAsync()
        {
            var addedWatcher = await proxy.WatchInterfacesAddedAsync(OnAdded);

            stopActions.Push(() => Task.Run(() => addedWatcher.Dispose()));

            var devices = await proxy.GetManagedObjectsAsync();

            foreach (var device in devices)
            {
                foreach (var iface in device.Value)
                {
                    if (iface.Key == "org.bluez.Adapter1")
                    {
                        var adapter = Connection.System.CreateProxy <IAdapter1>("org.bluez", device.Key);
                        try {
                            await adapter.SetDiscoveryFilterAsync(new Dictionary <string, object> {
                                { "Transport", "le" },
                                { "UUIDs", uuids.Select(x => x.ToString()).ToArray() },
                            });

                            stopActions.Push(() => adapter.SetDiscoveryFilterAsync(new Dictionary <string, object>()));

                            await adapter.StartDiscoveryAsync();

                            stopActions.Push(() => adapter.StopDiscoveryAsync());
                        }
                        catch {
                            // we just do the best we can
                        }
                    }
                    else if (iface.Key == "org.bluez.Device1")
                    {
                        OnAdded((device.Key, device.Value));
                    }
                }
            }
        }