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 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); }
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 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); }
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))); }
static async Task Main(string[] args) { if (args.Length < 1 || args.Length > 3 || args[0].ToLowerInvariant() == "-h" || !int.TryParse(args[1], out int scanSeconds)) { Console.WriteLine("Usage: scan <SecondsToScan> [adapterName]"); Console.WriteLine("Example: scan 15 hci0"); return; } IAdapter1 adapter; if (args.Length > 2) { adapter = await BlueZManager.GetAdapterAsync(args[2]); } else { var adapters = await BlueZManager.GetAdaptersAsync(); if (adapters.Count == 0) { throw new Exception("No Bluetooth adapters found."); } adapter = adapters.First(); } var adapterPath = adapter.ObjectPath.ToString(); var adapterName = adapterPath.Substring(adapterPath.LastIndexOf("/") + 1); Console.WriteLine($"Using Bluetooth adapter {adapterName}"); // Print out the devices we already know about. var devices = await adapter.GetDevicesAsync(); foreach (var device in devices) { string deviceDescription = await GetDeviceDescriptionAsync(device); Console.WriteLine(deviceDescription); } Console.WriteLine($"{devices.Count} device(s) found ahead of scan."); Console.WriteLine(); // Scan for more devices. Console.WriteLine($"Scanning for {scanSeconds} seconds..."); int newDevices = 0; using (await adapter.WatchDevicesAddedAsync(async device => { newDevices++; // Write a message when we detect new devices during the scan. string deviceDescription = await GetDeviceDescriptionAsync(device); Console.WriteLine($"[NEW] {deviceDescription}"); })) { await adapter.StartDiscoveryAsync(); await Task.Delay(TimeSpan.FromSeconds(scanSeconds)); await adapter.StopDiscoveryAsync(); } Console.WriteLine($"Scan complete. {newDevices} new device(s) found."); }