public void WriteType_set(CharacteristicWriteType writeType, CharacteristicPropertyType currentProperties)
        {
            var characteristic = new CharacteristicMock { MockPropterties = currentProperties };
            characteristic.WriteType = writeType;

            Assert.Equal(writeType, characteristic.WriteType);
        }
예제 #2
0
        /// <summary>
        /// Write the specified data to the characteristic
        /// </summary>
        /// <param name="data">Data.</param>
        public void Write(byte[] data, CharacteristicWriteType writeType)
        {
            if (!CanWrite)
            {
                throw new InvalidOperationException("Characteristic does not support WRITE");
            }

            var nsData = NSData.FromArray(data);
            CBCharacteristicWriteType nativeWriteType = CBCharacteristicWriteType.WithResponse;

            if (writeType == CharacteristicWriteType.WithResponse)
            {
                nativeWriteType = CBCharacteristicWriteType.WithResponse;
            }
            else if (writeType == CharacteristicWriteType.WithoutResponse)
            {
                nativeWriteType = CBCharacteristicWriteType.WithoutResponse;
            }

            _peripheral.WriteValue(nsData, _nativeCharacteristic, nativeWriteType);

            if (writeType == CharacteristicWriteType.WithoutResponse)
            {
                WriteComplete?.Invoke(this, new CharacteristicWriteEventArgs(true, this));
            }
        }
예제 #3
0
        /// <summary>
        /// Write the specified data to the characteristic
        /// </summary>
        /// <param name="data">Data.</param>
        public void Write(byte[] data, CharacteristicWriteType writeType)
        {
            if (!CanWrite)
            {
                throw new InvalidOperationException("Characteristic does not support WRITE");
            }

            _nativeCharacteristic.SetValue(data);

            if (writeType == CharacteristicWriteType.WithResponse)
            {
                _nativeCharacteristic.WriteType = GattWriteType.Default;
            }
            else if (writeType == CharacteristicWriteType.WithoutResponse)
            {
                _nativeCharacteristic.WriteType = GattWriteType.NoResponse;
            }

            var success = _gatt.WriteCharacteristic(_nativeCharacteristic);

            if (!success)
            {
                WriteComplete?.Invoke(this, new CharacteristicWriteEventArgs(false, this));
                //throw new CharacteristicException("Write failed", CharacteristicException.Code.WriteFailed);
            }
        }
        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));
        }
예제 #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;
        }
예제 #6
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);
        }
 public void WriteType_set_throws_InvalidOperationException(CharacteristicWriteType writeType, CharacteristicPropertyType currentProperties)
 {
     var characteristic = new CharacteristicMock { MockPropterties = currentProperties };
     Assert.Throws<InvalidOperationException>(() =>
     {
         characteristic.WriteType = writeType;
     });
 }
        protected async Task <bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
        {
            var result = await NativeCharacteristic.WriteValueWithResultAsync(
                CryptographicBuffer.CreateFromByteArray(data),
                writeType == CharacteristicWriteType.WithResponse?GattWriteOption.WriteWithResponse : GattWriteOption.WriteWithoutResponse);

            result.ThrowIfError();
            return(true);
        }
예제 #9
0
        public void WriteType_set_throws_InvalidOperationException(CharacteristicWriteType writeType,
                                                                   CharacteristicPropertyType currentProperties)
        {
            CharacteristicMock characteristic = new CharacteristicMock {
                MockPropterties = currentProperties
            };

            Assert.Throws <InvalidOperationException>(() => { characteristic.WriteType = writeType; });
        }
        public void WriteType_set(CharacteristicWriteType writeType, CharacteristicPropertyType currentProperties)
        {
            var characteristic = new CharacteristicMock
            {
                MockPropterties = currentProperties,
                WriteType       = writeType
            };

            Assert.Equal(writeType, characteristic.WriteType);
        }
        public async Task Write_WriteType(CharacteristicWriteType expectedWriteType, CharacteristicWriteType currentWriteType, CharacteristicPropertyType currentProperties)
        {
            var characteristic = new CharacteristicMock
            {
                MockPropterties = currentProperties,
                WriteType = currentWriteType
            };

            await characteristic.WriteAsync(new byte[0]);
            var writtenType = characteristic.WriteHistory.First().WriteType;

            Assert.Equal(expectedWriteType, writtenType);
        }
        public static GattWriteType ToNative(this CharacteristicWriteType writeType)
        {
            switch (writeType)
            {
            case CharacteristicWriteType.WithResponse:
                return(GattWriteType.Default);

            case CharacteristicWriteType.WithoutResponse:
                return(GattWriteType.NoResponse);

            default:
                throw new NotImplementedException();
            }
        }
예제 #13
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);
        }
예제 #14
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
            );
        }
예제 #15
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
                       ));
        }
예제 #16
0
        protected override async Task <Boolean> WriteNativeAsync(Byte[] data, CharacteristicWriteType writeType)
        {
            //print errors if error and write with response
            if (writeType == CharacteristicWriteType.WithResponse)
            {
                GattWriteResult result =
                    await this._nativeCharacteristic.WriteValueWithResultAsync(
                        CryptographicBuffer.CreateFromByteArray(data));

                if (result.Status == GattCommunicationStatus.Success)
                {
                    Trace.Message("Write successful");
                    return(true);
                }

                if (result.Status == GattCommunicationStatus.AccessDenied)
                {
                    Trace.Message("Incorrect permissions to stop updates");
                }
                else if (result.Status == GattCommunicationStatus.ProtocolError && result.ProtocolError != null)
                {
                    Trace.Message("Write Characteristic returned with error: {0}",
                                  this.parseError(result.ProtocolError));
                }
                else if (result.Status == GattCommunicationStatus.ProtocolError)
                {
                    Trace.Message("Write Characteristic returned with unknown error");
                }
                else if (result.Status == GattCommunicationStatus.Unreachable)
                {
                    Trace.Message("Characteristic write is unreachable");
                }
                return(false);
            }

            GattCommunicationStatus status =
                await this._nativeCharacteristic.WriteValueAsync(CryptographicBuffer.CreateFromByteArray(data),
                                                                 GattWriteOption.WriteWithoutResponse);

            if (status == GattCommunicationStatus.Success)
            {
                return(true);
            }
            return(false);
        }
예제 #17
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));
        }
 protected abstract Task<bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType);
예제 #19
0
 public WriteOperation(Byte[] value, CharacteristicWriteType writeType)
 {
     this.Value     = value;
     this.WriteType = writeType;
 }
 protected Task <bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType) => throw new PlatformNotSupportedException();
 protected override Task<bool> WriteNativeAsync(byte[] data, CharacteristicWriteType writeType)
 {
     WriteHistory.Add(new WriteOperation(data, writeType));
     return Task.FromResult(true);
 }
 public WriteOperation(byte[] value, CharacteristicWriteType writeType)
 {
     Value = value;
     WriteType = writeType;
 }