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); }
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); }
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; }
public async void LoadFirstAdapter() { try { adapter = (await BlueZManager.GetAdaptersAsync()).FirstOrDefault(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
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); }
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)); }
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); }
public static Task <string> GetAddressTypeAsync(this IAdapter1 o) => o.GetAsync <string>("AddressType");
public static Task <string> GetNameAsync(this IAdapter1 o) => o.GetAsync <string>("Name");
public DotNetBlueZAdapter(IAdapter1 adapter) { _adapter = adapter; }
public static Task SetPairableTimeoutAsync(this IAdapter1 o, uint val) => o.SetAsync("PairableTimeout", val);
public static Task SetPairableAsync(this IAdapter1 o, bool val) => o.SetAsync("Pairable", val);
public static Task SetDiscoverableTimeoutAsync(this IAdapter1 o, uint val) => o.SetAsync("DiscoverableTimeout", val);
public static Task <uint> GetDiscoverableTimeoutAsync(this IAdapter1 o) => o.GetAsync <uint>("DiscoverableTimeout");
public static Task <uint> GetClassAsync(this IAdapter1 o) => o.GetAsync <uint>("Class");
public static Task <string[]> GetUUIDsAsync(this IAdapter1 o) => o.GetAsync <string[]>("UUIDs");
public Repository1(IAdapter1 _adapter1) { adapter1 = _adapter1; }
public static Task <bool> GetDiscoveringAsync(this IAdapter1 o) => o.GetAsync <bool>("Discovering");
public static Task <uint> GetPairableTimeoutAsync(this IAdapter1 o) => o.GetAsync <uint>("PairableTimeout");
public static Task <bool> GetPairableAsync(this IAdapter1 o) => o.GetAsync <bool>("Pairable");
public static Task SetPairableTimeoutAsync(this IAdapter1 o, uint val) => o.SetAsync(nameof(Adapter1Properties.PairableTimeout), val);
public static Task SetPairableAsync(this IAdapter1 o, bool val) => o.SetAsync(nameof(Adapter1Properties.Pairable), val);
public static Task <string> GetModaliasAsync(this IAdapter1 o) => o.GetAsync <string>("Modalias");
public static Task <bool> GetPoweredAsync(this IAdapter1 o) => o.GetAsync <bool>("Powered");
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))); }
public static Task SetAliasAsync(this IAdapter1 o, string val) => o.SetAsync("Alias", val);
public static Task SetPoweredAsync(this IAdapter1 o, bool val) => o.SetAsync("Powered", val);
public static Task SetDiscoverableAsync(this IAdapter1 o, bool val) => o.SetAsync("Discoverable", val);
public static Task <IReadOnlyList <IDevice1> > GetDevicesAsync(this IAdapter1 adapter) { return(GetProxiesAsync <IDevice1>(adapter, BluezConstants.DeviceInterface)); }
public static Task SetPoweredAsync(this IAdapter1 o, bool val) => o.SetAsync(nameof(Adapter1Properties.Powered), val);