Exemple #1
0
        Task <IReadOnlyList <GattDescriptor> > PlatformGetDescriptors()
        {
            TaskCompletionSource <IReadOnlyList <GattDescriptor> > tcs = new TaskCompletionSource <IReadOnlyList <GattDescriptor> >();
            CBPeripheral peripheral = Service.Device;

            void handler(object sender, CBCharacteristicEventArgs args)
            {
                peripheral.DiscoveredDescriptor -= handler;

                if (args.Error != null)
                {
                    tcs.SetException(new Exception(args.Error.ToString()));
                    return;
                }

                List <GattDescriptor> descriptors = new List <GattDescriptor>();

                foreach (CBDescriptor cbdescriptor in _characteristic.Descriptors)
                {
                    descriptors.Add(new GattDescriptor(this, cbdescriptor));
                }

                tcs.SetResult(descriptors.AsReadOnly());
            }

            peripheral.DiscoveredDescriptor += handler;
            peripheral.DiscoverDescriptors(_characteristic);

            return(tcs.Task);
        }
Exemple #2
0
        Task <GattDescriptor> PlatformGetDescriptor(BluetoothUuid descriptor)
        {
            TaskCompletionSource <GattDescriptor> tcs = new TaskCompletionSource <GattDescriptor>();
            CBPeripheral peripheral = Service.Device;

            void handler(object sender, CBCharacteristicEventArgs args)
            {
                peripheral.DiscoveredDescriptor -= handler;

                if (args.Error != null)
                {
                    tcs.SetException(new Exception(args.Error.ToString()));
                    return;
                }

                foreach (CBDescriptor cbdescriptor in _characteristic.Descriptors)
                {
                    if ((BluetoothUuid)cbdescriptor.UUID == descriptor)
                    {
                        tcs.SetResult(new GattDescriptor(this, cbdescriptor));
                        return;
                    }
                }

                tcs.SetResult(null);
            }

            peripheral.DiscoveredDescriptor += handler;
            peripheral.DiscoverDescriptors(_characteristic);

            return(tcs.Task);
        }
Exemple #3
0
 public override void DiscoveredCharacteristic(CBPeripheral peripheral, CBService service, NSError error)
 {
     // Discover the Descriptors of the characteristics contained in service.Characteristics
     foreach (var characteristic in service.Characteristics)
     {
         peripheral.DiscoverDescriptors(characteristic);
     }
 }
        protected void Initialize()
        {
            tableSource.Characteristics = characteristics;

            // when the characteristic is selected in the table, make a request to disover the descriptors for it.
            tableSource.CharacteristicSelected += (sender, e) => {
                connectedPeripheral.DiscoverDescriptors(e.Characteristic);
            };
        }
Exemple #5
0
 public override void DiscoveredCharacteristic(CBPeripheral peripheral, CBService service, NSError error)
 {
     client.completedChar += 1;
     peripheral.Delegate   = this;
     if (service.Characteristics != null)
     {
         client.descCallCount += service.Characteristics.Count();
         foreach (var characteristic in service.Characteristics)
         {
             client.characteristicObjects.Add(characteristic);
             peripheral.DiscoverDescriptors(characteristic);
         }
     }
     client.allDiscovered();
 }
Exemple #6
0
        public async partial Task <IEnumerable <GattDescriptor> > GetDescriptorsAsync()
        {
            var errorAwaiter = @delegate.DiscoveredDescriptorObservable
                               .FirstAsync(x => x.characteristic == characteristic).GetAwaiter();

            peripheral.DiscoverDescriptors(characteristic);
            var(_, error) = await errorAwaiter;

            if (error is not null)
            {
                throw new NSErrorException(error);
            }

            return(characteristic.Descriptors?.Select(
                       descriptor => new GattDescriptor(peripheral, @delegate, this, descriptor)
                       ) ?? Enumerable.Empty <GattDescriptor>());
        }
Exemple #7
0
        protected override Task <IList <IDescriptor> > GetDescriptorsNativeAsync()
        {
            var exception = new Exception($"Device '{Service.Device.Id}' disconnected while fetching descriptors for characteristic with {Id}.");

            return(TaskBuilder.FromEvent <IList <IDescriptor>, EventHandler <CBCharacteristicEventArgs>, EventHandler <CBPeripheralErrorEventArgs> >(
                       execute: () =>
            {
                if (_parentDevice.State != CBPeripheralState.Connected)
                {
                    throw exception;
                }

                _parentDevice.DiscoverDescriptors(_nativeCharacteristic);
            },
                       getCompleteHandler: (complete, reject) => (sender, args) =>
            {
                if (args.Characteristic.UUID != _nativeCharacteristic.UUID)
                {
                    return;
                }

                if (args.Error != null)
                {
                    reject(new Exception($"Discover descriptors error: {args.Error.Description}"));
                }
                else
                {
                    complete(args.Characteristic.Descriptors.Select(descriptor => new Descriptor(descriptor, _parentDevice, this, _bleCentralManagerDelegate)).Cast <IDescriptor>().ToList());
                }
            },
                       subscribeComplete: handler => _parentDevice.DiscoveredDescriptor += handler,
                       unsubscribeComplete: handler => _parentDevice.DiscoveredDescriptor -= handler,
                       getRejectHandler: reject => ((sender, args) =>
            {
                if (args.Peripheral.Identifier == _parentDevice.Identifier)
                {
                    reject(exception);
                }
            }),
                       subscribeReject: handler => _bleCentralManagerDelegate.DisconnectedPeripheral += handler,
                       unsubscribeReject: handler => _bleCentralManagerDelegate.DisconnectedPeripheral -= handler));
        }
Exemple #8
0
        protected override Task <IList <IDescriptor> > GetDescriptorsNativeAsync()
        {
            return(TaskBuilder.FromEvent <IList <IDescriptor>, EventHandler <CBCharacteristicEventArgs> >(
                       execute: () => _parentDevice.DiscoverDescriptors(_nativeCharacteristic),
                       getCompleteHandler: (complete, reject) => (sender, args) =>
            {
                if (args.Characteristic.UUID != _nativeCharacteristic.UUID)
                {
                    return;
                }

                if (args.Error != null)
                {
                    reject(new Exception($"Discover descriptors error: {args.Error.Description}"));
                }
                else
                {
                    complete(args.Characteristic.Descriptors.Select(descriptor => new Descriptor(descriptor, _parentDevice, this)).Cast <IDescriptor>().ToList());
                }
            },
                       subscribeComplete: handler => _parentDevice.DiscoveredDescriptor += handler,
                       unsubscribeComplete: handler => _parentDevice.DiscoveredDescriptor -= handler));
        }