コード例 #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);
        }
コード例 #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);
        }
コード例 #3
0
ファイル: Bluetooth.cs プロジェクト: EtienneThompson/Sphero
 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);
     }
 }
コード例 #4
0
        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);
            };
        }
コード例 #5
0
ファイル: IOSBluetooth.cs プロジェクト: juchong/ProtoRIO
 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();
 }
コード例 #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>());
        }
コード例 #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));
        }
コード例 #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));
        }