Exemplo n.º 1
0
        protected override Task <Boolean> WriteNativeAsync(Byte[] data, CharacteristicWriteType writeType)
        {
            Exception exception =
                new Exception(
                    $"Device {this.Service.Device.Id} disconnected while writing characteristic with {this.Id}.");

            Task <Boolean> task;

            if (writeType.ToNative() == CBCharacteristicWriteType.WithResponse)
            {
                task = TaskBuilder
                       .FromEvent <Boolean, EventHandler <CBCharacteristicEventArgs>,
                                   EventHandler <CBPeripheralErrorEventArgs> >(
                    () =>
                {
                    if (this._parentDevice.State != CBPeripheralState.Connected)
                    {
                        throw exception;
                    }
                },
                    (complete, reject) => (sender, args) =>
                {
                    if (args.Characteristic.UUID != this._nativeCharacteristic.UUID)
                    {
                        return;
                    }

                    complete(args.Error == null);
                },
                    handler => this._parentDevice.WroteCharacteristicValue += handler,
                    handler => this._parentDevice.WroteCharacteristicValue -= handler,
                    reject => (sender, args) =>
                {
                    if (args.Peripheral.Identifier == this._parentDevice.Identifier)
                    {
                        reject(exception);
                    }
                },
                    handler => this._centralManager.DisconnectedPeripheral += handler,
                    handler => this._centralManager.DisconnectedPeripheral -= handler);
            }
            else
            {
                task = Task.FromResult(true);
            }

            NSData nsdata = NSData.FromArray(data);

            this._parentDevice.WriteValue(nsdata, this._nativeCharacteristic, writeType.ToNative());

            return(task);
        }
Exemplo n.º 2
0
        protected override Task <bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            var exception = new Exception($"Device {Service.Device.Id} disconnected while writing characteristic with {Id}.");

            Task <bool> task;

            if (writeType.ToNative() == CBCharacteristicWriteType.WithResponse)
            {
                task = TaskBuilder.FromEvent <bool, EventHandler <CBCharacteristicEventArgs>, EventHandler <CBPeripheralErrorEventArgs> >(
                    execute: () =>
                {
                    if (_parentDevice.State != CBPeripheralState.Connected)
                    {
                        throw exception;
                    }
                },
                    getCompleteHandler: (complete, reject) => (sender, args) =>
                {
                    if (args.Characteristic.UUID != _nativeCharacteristic.UUID)
                    {
                        return;
                    }

                    complete(args.Error == null);
                },
                    subscribeComplete: handler => _parentDevice.WroteCharacteristicValue   += handler,
                    unsubscribeComplete: handler => _parentDevice.WroteCharacteristicValue -= handler,
                    getRejectHandler: reject => ((sender, args) =>
                {
                    if (args.Peripheral.Identifier == _parentDevice.Identifier)
                    {
                        reject(exception);
                    }
                }),
                    subscribeReject: handler => _bleCentralManagerDelegate.DisconnectedPeripheral   += handler,
                    unsubscribeReject: handler => _bleCentralManagerDelegate.DisconnectedPeripheral -= handler);
            }
            else
            {
                task = Task.FromResult(true);
            }

            var nsdata = NSData.FromArray(data);

            _parentDevice.WriteValue(nsdata, _nativeCharacteristic, writeType.ToNative());

            return(task);
        }
        protected override async Task <bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            _nativeCharacteristic.WriteType = writeType.ToNative();

            if (writeType == CharacteristicWriteType.WithoutResponse)
            {
                return(InternalWrite(data));
            }

            return(await TaskBuilder.FromEvent <bool, EventHandler <CharacteristicWriteCallbackEventArgs>, EventHandler>(
                       execute : () => InternalWrite(data),
                       getCompleteHandler : (complete, reject) => ((sender, args) =>
            {
                if (args.Characteristic.Uuid == _nativeCharacteristic.Uuid)
                {
                    complete(args.Exception == null);
                }
            }),
                       subscribeComplete : handler => _gattCallback.CharacteristicValueWritten += handler,
                       unsubscribeComplete : handler => _gattCallback.CharacteristicValueWritten -= handler,
                       getRejectHandler : reject => ((sender, args) =>
            {
                reject(new Exception($"Device '{Service.Device.Id}' disconnected while writing characteristic with {Id}."));
            }),
                       subscribeReject : handler => _gattCallback.ConnectionInterrupted += handler,
                       unsubscribeReject : handler => _gattCallback.ConnectionInterrupted -= handler));
        }
Exemplo n.º 4
0
        protected override Task <bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            Task <bool> task;

            var nativeWriteType = writeType.ToNative();

            if (nativeWriteType == CBCharacteristicWriteType.WithResponse)
            {
                task = TaskBuilder.FromEvent <bool, EventHandler <CBCharacteristicEventArgs> >(
                    execute: () => { },
                    getCompleteHandler: (complete, reject) => (sender, args) =>
                {
                    if (args.Characteristic.UUID != _nativeCharacteristic.UUID)
                    {
                        return;
                    }

                    complete(args.Error == null);
                },
                    subscribeComplete: handler => _parentDevice.WroteCharacteristicValue   += handler,
                    unsubscribeComplete: handler => _parentDevice.WroteCharacteristicValue -= handler);
            }
            else
            {
                task = Task.FromResult(true);
            }

            var nsdata = NSData.FromArray(data);

            _parentDevice.WriteValue(nsdata, _nativeCharacteristic, nativeWriteType);

            return(task);
        }
Exemplo n.º 5
0
        protected override Task<bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            Task<bool> task;

            var nativeWriteType = writeType.ToNative();
            if (nativeWriteType == CBCharacteristicWriteType.WithResponse)
            {
                task = TaskBuilder.FromEvent<bool, EventHandler<CBCharacteristicEventArgs>>(
                    execute: () => { },
                    getCompleteHandler: (complete, reject) => (sender, args) =>
                    {
                        if (args.Characteristic.UUID != _nativeCharacteristic.UUID)
                            return;

                        complete(args.Error == null);
                    },
                    subscribeComplete: handler => _parentDevice.WroteCharacteristicValue += handler,
                    unsubscribeComplete: handler => _parentDevice.WroteCharacteristicValue -= handler);
            }
            else
            {
                task = Task.FromResult(true);
            }

            var nsdata = NSData.FromArray(data);
            _parentDevice.WriteValue(nsdata, _nativeCharacteristic, nativeWriteType);

            return task;
        }
Exemplo n.º 6
0
        protected override async Task<bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            _nativeCharacteristic.WriteType = writeType.ToNative();

            return await TaskBuilder.FromEvent<bool, EventHandler<CharacteristicWriteCallbackEventArgs>>(
                execute: () => InternalWrite(data),
                getCompleteHandler: (complete, reject) => ((sender, args) =>
                   {
                       if (args.Characteristic.Uuid == _nativeCharacteristic.Uuid)
                       {
                           complete(args.Exception == null);
                       }
                   }),
               subscribeComplete: handler => _gattCallback.CharacteristicValueWritten += handler,
               unsubscribeComplete: handler => _gattCallback.CharacteristicValueWritten -= handler
            );
        }
Exemplo n.º 7
0
        protected override async Task <bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            _nativeCharacteristic.WriteType = writeType.ToNative();

            return(await TaskBuilder.FromEvent <bool, EventHandler <CharacteristicWriteCallbackEventArgs> >(
                       execute : () => InternalWrite(data),
                       getCompleteHandler : (complete, reject) => ((sender, args) =>
            {
                if (args.Characteristic.Uuid == _nativeCharacteristic.Uuid)
                {
                    complete(args.Exception == null);
                }
            }),
                       subscribeComplete : handler => _gattCallback.CharacteristicValueWritten += handler,
                       unsubscribeComplete : handler => _gattCallback.CharacteristicValueWritten -= handler
                       ));
        }
Exemplo n.º 8
0
        protected override async Task <Boolean> WriteNativeAsync(Byte[] data, CharacteristicWriteType writeType)
        {
            this._nativeCharacteristic.WriteType = writeType.ToNative();

            return(await TaskBuilder
                   .FromEvent <Boolean, EventHandler <CharacteristicWriteCallbackEventArgs>, EventHandler>(
                       () => this.InternalWrite(data),
                       (complete, reject) => (sender, args) =>
            {
                if (args.Characteristic.Uuid == this._nativeCharacteristic.Uuid)
                {
                    complete(args.Exception == null);
                }
            },
                       handler => this._gattCallback.CharacteristicValueWritten += handler,
                       handler => this._gattCallback.CharacteristicValueWritten -= handler,
                       reject => (sender, args) =>
            {
                reject(new Exception(
                           $"Device '{this.Service.Device.Id}' disconnected while writing characteristic with {this.Id}."));
            },
                       handler => this._gattCallback.ConnectionInterrupted += handler,
                       handler => this._gattCallback.ConnectionInterrupted -= handler));
        }