async Task <Device> InitAsync() { propertyWatcher = await proxy.WatchPropertiesAsync(x => UpdateProperties(x.Changed)); UpdateProperties(await proxy.GetAllAsync()); await proxy.ConnectAsync(); return(this); }
private static async Task <bool> CheckAndConnect(ScanFilter filter, IDevice1 device) { if (!await ShouldConnect(filter, device)) { return(false); } await device.ConnectAsync(); await device.WaitForPropertyValueAsync("Connected", value : true, TIMEOUT); return(true); }
public Task Connect() { return(_device1.ConnectAsync()); }
static async Task OnDeviceFoundAsync(IDevice1 device) { string deviceDescription = await GetDeviceDescriptionAsync(device); while (true) { Console.WriteLine($"Connect to {deviceDescription}? yes/[no]?"); string response = Console.ReadLine(); if (response.Length == 0 || response.ToLowerInvariant().StartsWith("n")) { return; } if (response.ToLowerInvariant().StartsWith("y")) { break; } } try { Console.WriteLine("Connecting..."); await device.ConnectAsync(); await device.WaitForPropertyValueAsync("Connected", value : true, timeout); Console.WriteLine("Connected."); Console.WriteLine("Waiting for services to resolve..."); await device.WaitForPropertyValueAsync("ServicesResolved", value : true, timeout); var servicesUUIDs = await device.GetUUIDsAsync(); Console.WriteLine($"Device offers {servicesUUIDs.Length} service(s)."); var deviceInfoServiceFound = servicesUUIDs.Any(uuid => uuid == GattConstants.DeviceInformationServiceUUID); if (!deviceInfoServiceFound) { Console.WriteLine("Device doesn't have the Device Information Service. Try pairing first?"); return; } // Console.WriteLine("Retrieving Device Information service..."); var service = await device.GetServiceAsync(GattConstants.DeviceInformationServiceUUID); var modelNameCharacteristic = await service.GetCharacteristicAsync(GattConstants.ModelNameCharacteristicUUID); var manufacturerCharacteristic = await service.GetCharacteristicAsync(GattConstants.ManufacturerNameCharacteristicUUID); Console.WriteLine("Reading Device Info characteristic values..."); var modelNameBytes = await modelNameCharacteristic.ReadValueAsync(timeout); var manufacturerBytes = await manufacturerCharacteristic.ReadValueAsync(timeout); Console.WriteLine($"Model name: {Encoding.UTF8.GetString(modelNameBytes)}"); Console.WriteLine($"Manufacturer: {Encoding.UTF8.GetString(manufacturerBytes)}"); // Test walking back up to the adapter... var adapterName = await(await(await(await modelNameCharacteristic.GetServiceAsync()).GetDeviceAsync()).GetAdapterAsync()).GetAliasAsync(); Console.WriteLine($"Adapter name: {adapterName}"); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); } finally { Console.WriteLine(); } }