예제 #1
0
        Task PlatformWriteValue(byte[] value, bool requireResponse)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            void handler(object s, CharacteristicEventArgs e)
            {
                if (e.Characteristic == _characteristic)
                {
                    if (!tcs.Task.IsCompleted)
                    {
                        tcs.SetResult(e.Status == ABluetooth.GattStatus.Success);
                    }

                    Service.Device.Gatt.CharacteristicWrite -= handler;
                }
            };

            Service.Device.Gatt.CharacteristicWrite += handler;
            bool written = _characteristic.SetValue(value);

            _characteristic.WriteType = requireResponse ? ABluetooth.GattWriteType.Default : ABluetooth.GattWriteType.NoResponse;
            written = ((ABluetooth.BluetoothGatt)Service.Device.Gatt).WriteCharacteristic(_characteristic);
            return(tcs.Task);
        }