public BluetoothCharacteristic(BluetoothGattCharacteristic gattCharacteristic, BluetoothService service) { this.Service = service; this.GattCharacteristic = gattCharacteristic; this.Id = new Guid(gattCharacteristic.Uuid.ToString()); }
public async Task <IBluetoothService[]> GetServicesAsync(CancellationToken cancellationToken) { if (_servicesDiscovered) { return(ServicesById.Values.Select(x => (IBluetoothService)x).ToArray()); } else { TaskCompletionSource <IBluetoothService[]> tcs = new TaskCompletionSource <IBluetoothService[]>(); CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); cts.CancelAfter(TimeSpan.FromSeconds(5)); EventHandler <ServicesDiscoveredEventArgs> eventHandler = null; eventHandler = (o, e) => { BluetoothService service; List <IBluetoothService> servicesList = new List <IBluetoothService>(); foreach (var platformService in e.Gatt.Services) { service = new BluetoothService(platformService, this); this.ServicesById[platformService.Uuid] = service; servicesList.Add(service); } tcs.TrySetResult(servicesList.ToArray()); }; this.ServicesDiscovered += eventHandler; try { using (cts.Token.Register(() => tcs.TrySetResult(new IBluetoothService[] { }), false)) { Connect(); return(await tcs.Task.ConfigureAwait(continueOnCapturedContext : false)); } } finally { this.ServicesDiscovered -= eventHandler; } } }