예제 #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 async Task <bool> init()
        {
            if (!string.IsNullOrEmpty(adapterId))
            {
                Instance = await BlueZManager.GetAdapterAsync(adapterId);

                return(true);
            }
            else
            {
                var adapters = await BlueZManager.GetAdaptersAsync();

                if (adapters.Count == 0)
                {
                    throw new Exception("No Bluetooth adapters found.");
                }

                Instance = adapters.First();
            }

            var adapterPath = Instance.ObjectPath.ToString();
            var adapterName = adapterPath.Substring(adapterPath.LastIndexOf("/") + 1);

            Console.WriteLine($"Using Bluetooth adapter {adapterName}");
            return(true);
        }
예제 #3
0
 internal BluetoothAdapter(IAdapter1 adapter1, Adapter1Properties properties)
 {
     _adapter1    = adapter1;
     Address      = properties.Address;
     Name         = properties.Name;
     Alias        = properties.Alias;
     Discoverable = properties.Discoverable;
     Discovering  = properties.Discovering;
     UUIDs        = properties.UUIDs;
 }
예제 #4
0
 public async void LoadFirstAdapter()
 {
     try
     {
         adapter = (await BlueZManager.GetAdaptersAsync()).FirstOrDefault();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
예제 #5
0
        internal static async Task <Adapter> CreateAsync(IAdapter1 proxy)
        {
            var adapter = new Adapter
            {
                m_proxy = proxy,
            };

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

            adapter.m_interfacesWatcher = await objectManager.WatchInterfacesAddedAsync(adapter.OnDeviceAdded);

            adapter.m_propertyWatcher = await proxy.WatchPropertiesAsync(adapter.OnPropertyChanges);

            return(adapter);
        }
예제 #6
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));
        }
예제 #7
0
        public async Task <object> ScanForRobots(string filter = null)
        {
            string    adapterObjectPath = $"/org/bluez/{ADAPTER_NAME}";
            IAdapter1 adapter           = Connection.System.CreateProxy <IAdapter1>(BluezConstants.DbusService, adapterObjectPath);

            if (adapter == null)
            {
                Console.WriteLine($"Bluetooth adapter '{ADAPTER_NAME}' not found.");
            }

            IReadOnlyList <IDevice1> devices = await adapter.GetDevicesAsync();

            Console.WriteLine($"{devices.Count} devices found");
            foreach (IDevice1 device in devices)
            {
                Console.WriteLine(device);
                string name = await device.GetNameAsync();

                Console.WriteLine(name);
            }

            return(devices);
        }
예제 #8
0
 public static Task <string> GetAddressTypeAsync(this IAdapter1 o) => o.GetAsync <string>("AddressType");
예제 #9
0
 public static Task <string> GetNameAsync(this IAdapter1 o) => o.GetAsync <string>("Name");
예제 #10
0
 public DotNetBlueZAdapter(IAdapter1 adapter)
 {
     _adapter = adapter;
 }
예제 #11
0
 public static Task SetPairableTimeoutAsync(this IAdapter1 o, uint val) => o.SetAsync("PairableTimeout", val);
예제 #12
0
 public static Task SetPairableAsync(this IAdapter1 o, bool val) => o.SetAsync("Pairable", val);
예제 #13
0
 public static Task SetDiscoverableTimeoutAsync(this IAdapter1 o, uint val) => o.SetAsync("DiscoverableTimeout", val);
예제 #14
0
 public static Task <uint> GetDiscoverableTimeoutAsync(this IAdapter1 o) => o.GetAsync <uint>("DiscoverableTimeout");
예제 #15
0
 public static Task <uint> GetClassAsync(this IAdapter1 o) => o.GetAsync <uint>("Class");
예제 #16
0
 public static Task <string[]> GetUUIDsAsync(this IAdapter1 o) => o.GetAsync <string[]>("UUIDs");
예제 #17
0
 public Repository1(IAdapter1 _adapter1)
 {
     adapter1 = _adapter1;
 }
예제 #18
0
 public static Task <bool> GetDiscoveringAsync(this IAdapter1 o) => o.GetAsync <bool>("Discovering");
예제 #19
0
 public static Task <uint> GetPairableTimeoutAsync(this IAdapter1 o) => o.GetAsync <uint>("PairableTimeout");
예제 #20
0
 public static Task <bool> GetPairableAsync(this IAdapter1 o) => o.GetAsync <bool>("Pairable");
예제 #21
0
 public static Task SetPairableTimeoutAsync(this IAdapter1 o, uint val) => o.SetAsync(nameof(Adapter1Properties.PairableTimeout), val);
예제 #22
0
 public static Task SetPairableAsync(this IAdapter1 o, bool val) => o.SetAsync(nameof(Adapter1Properties.Pairable), val);
예제 #23
0
 public static Task <string> GetModaliasAsync(this IAdapter1 o) => o.GetAsync <string>("Modalias");
예제 #24
0
 public static Task <bool> GetPoweredAsync(this IAdapter1 o) => o.GetAsync <bool>("Powered");
예제 #25
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)));
        }
예제 #26
0
 public static Task SetAliasAsync(this IAdapter1 o, string val) => o.SetAsync("Alias", val);
예제 #27
0
 public static Task SetPoweredAsync(this IAdapter1 o, bool val) => o.SetAsync("Powered", val);
예제 #28
0
 public static Task SetDiscoverableAsync(this IAdapter1 o, bool val) => o.SetAsync("Discoverable", val);
예제 #29
0
 public static Task <IReadOnlyList <IDevice1> > GetDevicesAsync(this IAdapter1 adapter)
 {
     return(GetProxiesAsync <IDevice1>(adapter, BluezConstants.DeviceInterface));
 }
예제 #30
0
 public static Task SetPoweredAsync(this IAdapter1 o, bool val) => o.SetAsync(nameof(Adapter1Properties.Powered), val);