protected async Task Initialize() { await _device.WaitForPropertyValueAsync("Connected", value : true, TIMEOUT); _properties = await _device.GetAllAsync(); Log.Debug("Waiting for services to resolve..."); await _device.WaitForPropertyValueAsync("ServicesResolved", value : true, TIMEOUT); foreach (var s in await _device.GetUUIDsAsync()) { var g = Guid.Parse(s); var svc = await _device.GetServiceAsync(s); _services[Guid.Parse(s)] = svc; if (g == DEVICE_INFORMATION_SERVICE) { Log.Debug("Found DeviceInformationService."); DeviceInformationService = new DeviceInformationService(svc); } else if (g == BATTERY_SERVICE) { Log.Debug("Found BatteryService."); BatteryService = new BatteryService(svc); } } Log.Debug("Services resolved."); await BleDeviceConnected(); Log.Information($"Connected to {this}."); }
private static async Task PrintDeviceInformation(IDevice1 device) { 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)}"); }
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(); } }