Ejemplo n.º 1
0
        public async Task <bool> WriteAsync(IGattCharacteristic characteristic, byte[] data)
        {
            lock (_lock)
            {
                if (_bluetoothGatt == null || State != BluetoothLEDeviceState.Connected)
                {
                    return(false);
                }

                var gattCharacteristic = ((GattCharacteristic)characteristic).BluetoothGattCharacteristic;
                gattCharacteristic.WriteType = GattWriteType.Default;

                if (!gattCharacteristic.SetValue(data))
                {
                    return(false);
                }

                _writeCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

                if (!_bluetoothGatt.WriteCharacteristic(gattCharacteristic))
                {
                    _writeCompletionSource = null;
                    return(false);
                }
            }

            var result = await _writeCompletionSource.Task;

            _writeCompletionSource = null;
            return(result);
        }
        public virtual void WriteTXCharacteristic(byte[] value)
        {
            BluetoothGattCharacteristic rxChar;
            var rxService = Gatt.GetService(RX_SERVICE_UUID2);

            if (rxService == null)
            {
                ShowMessage("mBluetoothGatt null" + Gatt);
                ShowMessage("Tx service not found!");
                BroadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
                return;
            }
            rxChar = rxService.GetCharacteristic(RX_CHAR_UUID2);
            if (rxChar == null)
            {
                ShowMessage("Tx charateristic not found!");
                BroadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
                return;
            }
            Debug.WriteLine(TAG, "TxChar = " + RX_CHAR_UUID2.ToString());
            rxChar.SetValue(value);
            bool status = Gatt.WriteCharacteristic(rxChar);

            Debug.WriteLine(TAG, "write TXchar - status=" + status);
        }
Ejemplo n.º 3
0
        public void ConfigureGyroscopePeriod()
        {
            var accelerometerPeriod = BluetoothGatt.GetService(BluetoothSensorAttributes.GyroscopeService).GetCharacteristic(BluetoothSensorAttributes.GyroscopePeriod);

            accelerometerPeriod.SetValue(new byte[] { 10 });
            BluetoothGatt.WriteCharacteristic(accelerometerPeriod);
        }
Ejemplo n.º 4
0
        private bool InternalWrite(byte[] data)
        {
            _nativeCharacteristic.SetValue(data);
            Trace.Message("Write {0}", Id);

            return(_gatt.WriteCharacteristic(_nativeCharacteristic));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Write byte data to the Bluetooth device.
        /// </summary>
        /// <param name="data">The array of bytes to write.</param>
        /// <returns>True if the write succeeded.</returns>
        public bool Write(params byte[] data)
        {
            if (State != BlueState.Connected)
            {
                return(false);
            }
            if (_TX == null)
            {
                Debug.WriteLineIf(sw.TraceError, $"--> Write no TX characteristic");
                return(false);
            }
            bool res = _TX.SetValue(data);

            if (!res)
            {
                Debug.WriteLineIf(sw.TraceVerbose, "--> TX SetValue failed");
                return(false);
            }
            else
            {
                res = _gatt.WriteCharacteristic(_TX);
                if (!res)
                {
                    Debug.WriteLineIf(sw.TraceVerbose, "--> TX WriteCharacteristic failed");
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
        bool write(byte[] data)
        {
            if (!CanWrite)
            {
                throw new InvalidOperationException("Characteristic does not support WRITE");
            }

            if (_gattCallback != null)
            {
                _gattCallback.CharacteristicValueWritten += OnCharacteristicValueWritten;
            }

            var c = _nativeCharacteristic;

            c.SetValue(data);
            Mvx.Trace(".....Write {0}", ID);
            //_gatt.WriteCharacteristic(c);
            var ret = _gatt.WriteCharacteristic(c);

            if (!ret)
            {
                _gattCallback.CharacteristicValueWritten -= OnCharacteristicValueWritten;
            }
            return(ret);
        }
Ejemplo n.º 8
0
        public void ConfigureAccelerometer(BluetoothGatt gatt)
        {
            ConfigurationStep = ConfigurationStep.AccelerometerConfigured;
            var accelerometer = gatt.GetService(BluetoothSensorAttributes.AccelerometerService).GetCharacteristic(BluetoothSensorAttributes.AccelerometerConfiguration);

            accelerometer.SetValue(new byte[] { 1 });
            gatt.WriteCharacteristic(accelerometer);
        }
Ejemplo n.º 9
0
        public void ConfigureGyroscope(BluetoothGatt gatt)
        {
            ConfigurationStep = ConfigurationStep.GyroscopeConfigured;

            var gyroscope = gatt.GetService(BluetoothSensorAttributes.GyroscopeService)?.GetCharacteristic(BluetoothSensorAttributes.GyroscopeConfiguration);

            gyroscope.SetValue(new byte[] { 7 });
            gatt.WriteCharacteristic(gyroscope);
        }
Ejemplo n.º 10
0
 public void Write(byte[] data)
 {
     if (!CanWrite)
     {
         throw new InvalidOperationException("Characteristic does not support WRITE");
     }
     _nativeCharacteristic.WriteType = GattWriteType.Default;
     _nativeCharacteristic.SetValue(data);
     if (!_gatt.WriteCharacteristic(_nativeCharacteristic))
     {
         Thread.Sleep(100);
         if (!_gatt.WriteCharacteristic(_nativeCharacteristic))
         {
             Thread.Sleep(100);
             _gatt.WriteCharacteristic(_nativeCharacteristic);
         }
     }
 }
Ejemplo n.º 11
0
        public async Task <bool> WriteAsync(IGattCharacteristic characteristic, byte[] data, CancellationToken token)
        {
            using (token.Register(() =>
            {
                lock (_lock)
                {
                    _writeCompletionSource?.TrySetResult(false);
                }
            }))
            {
                lock (_lock)
                {
                    if (_bluetoothGatt == null || State != BluetoothLEDeviceState.Connected)
                    {
                        return(false);
                    }

                    var nativeCharacteristic = ((GattCharacteristic)characteristic).BluetoothGattCharacteristic;
                    nativeCharacteristic.WriteType = GattWriteType.Default;

                    if (!nativeCharacteristic.SetValue(data))
                    {
                        return(false);
                    }

                    _writeCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

                    if (!_bluetoothGatt.WriteCharacteristic(nativeCharacteristic))
                    {
                        _writeCompletionSource = null;
                        return(false);
                    }
                }

                var result = await _writeCompletionSource.Task.ConfigureAwait(false);

                lock (_lock)
                {
                    _writeCompletionSource = null;
                    return(result);
                }
            }
        }
Ejemplo n.º 12
0
 private void PlatformWrite(byte[] data)
 {
     if (!_characteristic.SetValue(data))
     {
         RaiseWriteFailed(BLEErrorCode.InternalError);
         return;
     }
     if (!_gatt.WriteCharacteristic(_characteristic))
     {
         RaiseWriteFailed(BLEErrorCode.InternalError);
     }
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Write the specified data to the characteristic
        /// </summary>
        /// <param name="data">Data.</param>
        public void Write(byte[] data)
        {
            if (!CanWrite)
            {
                throw new InvalidOperationException("Characteristic does not support WRITE");
            }

            _nativeCharacteristic.SetValue(data);
            _nativeCharacteristic.WriteType = GattWriteType.NoResponse;

            _gatt.WriteCharacteristic(_nativeCharacteristic);
        }
Ejemplo n.º 14
0
        public void ConfigureAccelerometerPeriod(BluetoothGatt gatt)
        {
            ConfigurationStep = ConfigurationStep.AccelerometerPeriod;
            var accelerometerPeriod = gatt.GetService(BluetoothSensorAttributes.AccelerometerService).GetCharacteristic(BluetoothSensorAttributes.AccelerometerPeriod);

            if (accelerometerPeriod.GetValue() != null)
            {
                return;
            }

            accelerometerPeriod.SetValue(new byte[] { AccelerometerPeriod });
            gatt.WriteCharacteristic(accelerometerPeriod);
        }
Ejemplo n.º 15
0
        // HACK: UNTESTED - this API has only been tested on iOS
        public void Write(byte[] data)
        {
            if (!CanWrite)
            {
                throw new InvalidOperationException("Characteristic does not support WRITE");
            }

            var c = _nativeCharacteristic;

            c.SetValue(data);
            _gatt.WriteCharacteristic(c);
            Console.WriteLine(@".....Write");
        }
 private void WriteTimeSync_ex(BluetoothGatt gatt)
 {
     try {
         if (gatt == null || _customTimeCharacteristic == null)
         {
             return;
         }
         SetCustomData_MC(_customTimeCharacteristic);
         gatt.WriteCharacteristic(_customTimeCharacteristic);
     } catch (Exception e) {
         Log.Error("Error", e.Message);
     }
 }
Ejemplo n.º 17
0
        public void ConfigureGyroscopePeriod(BluetoothGatt gatt)
        {
            ConfigurationStep = ConfigurationStep.GyroscopePeriod;
            var gyroscopePeriod = gatt.GetService(BluetoothSensorAttributes.GyroscopeService).GetCharacteristic(BluetoothSensorAttributes.GyroscopePeriod);

            if (gyroscopePeriod.GetValue() != null)
            {
                return;
            }

            gyroscopePeriod.SetValue(new byte[] { GyroscopePeriod });
            gatt.WriteCharacteristic(gyroscopePeriod);
        }
 private void GetCustomTimeSync(BluetoothGatt gatt)
 {
     try {
         if (gatt == null || _customTimeCharacteristic == null)
         {
             return;
         }
         SetCustomTimeSync(_customTimeCharacteristic, new GregorianCalendar());
         gatt.WriteCharacteristic(_customTimeCharacteristic);
     } catch (Exception e) {
         Log.Error("Error", e.Message);
     }
 }
        private void InternalWrite(byte[] data)
        {
            if (!NativeCharacteristic.SetValue(data))
            {
                throw new CharacteristicReadException("Gatt characteristic set value FAILED.");
            }

            Trace.Message("Write {0}", Id);

            if (!_gatt.WriteCharacteristic(NativeCharacteristic))
            {
                throw new CharacteristicReadException("Gatt write characteristic FAILED.");
            }
        }
        private bool GetSequenceNumber(BluetoothGatt gatt)
        {
            try {
                if (gatt != null && _racpCharacteristic != null)
                {
                    SetOpCode(_racpCharacteristic, 4, 1, new int[0]);
                    return(gatt.WriteCharacteristic(_racpCharacteristic));
                }
            } catch (Exception e) {
                Log.Error("Error", e.Message);
            }

            return(false);
        }
        private bool GetAllRecords(BluetoothGatt gatt)
        {
            try {
                if (gatt != null && _racpCharacteristic != null)
                {
                    SetOpCode(_racpCharacteristic, 1, 1, new int[0]);
                    return(gatt.WriteCharacteristic(_racpCharacteristic));
                }
            } catch (Exception e) {
                Log.Error("getAllRecords", e.Message);
            }

            return(false);
        }
        private void InternalWrite(byte[] data)
        {
            if (Configuration.IsForceUI)
            {
                using (var h = new Handler(Looper.MainLooper))
                {
                    h.Post(() =>
                    {
                        if (!_nativeCharacteristic.SetValue(data))
                        {
                            throw new CharacteristicReadException("Gatt characteristic set value FAILED.");
                        }

                        Trace.Message("Write {0}", Id);

                        if (!_gatt.WriteCharacteristic(_nativeCharacteristic))
                        {
                            throw new CharacteristicReadException("Gatt write characteristic FAILED.");
                        }
                    });
                }
            }
            else
            {
                if (!_nativeCharacteristic.SetValue(data))
                {
                    throw new CharacteristicReadException("Gatt characteristic set value FAILED.");
                }

                Trace.Message("Write {0}", Id);

                if (!_gatt.WriteCharacteristic(_nativeCharacteristic))
                {
                    throw new CharacteristicReadException("Gatt write characteristic FAILED.");
                }
            }
        }
Ejemplo n.º 23
0
        private void SetValue()
        {
            String inputStr = valueInput.Text;

            String[] valStr   = inputStr.Split(':');
            byte[]   newValue = new byte[valStr.Length];

            for (int i = 0; i < valStr.Length; i++)
            {
                newValue[i] = Convert.ToByte(valStr[i], 16);
            }

            characteristic.SetValue(newValue);
            gatt.WriteCharacteristic(characteristic);
        }
Ejemplo n.º 24
0
        public void ConfigureMovement(BluetoothGatt gatt)
        {
            try
            {
                var characteristic = gatt.GetService(BluetoothSensorTagAttributes.MovementService).GetCharacteristic(BluetoothSensorTagAttributes.MovementConfiguration);
                characteristic.SetValue(new byte[] { 0x7F, 0x02 });
                gatt.WriteCharacteristic(characteristic);

                SensorTagConfigurationStep = SensorTagConfigurationStep.Movement;
            }
            catch (System.Exception ex)
            {
                string title = this.GetType().Name + " - " + System.Reflection.MethodBase.GetCurrentMethod().Name;
                BalizaFacil.App.Instance.UnhandledException(title, ex);
            }
        }
        private bool SetFlag(BluetoothGatt gatt)
        {
            if (gatt == null || _racpCharacteristic == null)
            {
                return(false);
            }

            sbyte[] bArr = { -64, 2, -31, 1, 5, 1, 1, 1, 1, 1, 0 };
            _racpCharacteristic.SetValue(new byte[bArr.Length]);
            for (var i = 0; i < bArr.Length; i++)
            {
                _racpCharacteristic.SetValue((byte[])(Array)bArr);
            }

            return(gatt.WriteCharacteristic(_racpCharacteristic));
        }
Ejemplo n.º 26
0
        public void WriteCommand(EVrcRigCommand command)
        {
            if (gatt == null)
            {
                return;
            }

            var ms = new MemoryStream(20);

            using (var writer = new BinaryWriter(ms)) {
                writer.Write((byte)command);
            }

            writeCharacteristic.SetValue(ms.ToArray());
            gatt.WriteCharacteristic(writeCharacteristic);
        }
Ejemplo n.º 27
0
        /// <summary>
        ///
        /// </summary>
        public virtual int WriteRXCharacteristic(byte[] value)
        {
            BluetoothGattService rxService = m_Gatt
                                             .GetService(m_UuidServ);

            if (rxService == null)
            {
                return(-1);
            }
            BluetoothGattCharacteristic rxChar = rxService
                                                 .GetCharacteristic(m_UuidRx);

            if (rxChar == null)
            {
                return(-2);
            }
            rxChar.SetValue(value);
            bool status = m_Gatt.WriteCharacteristic(rxChar);

            return((status)?0:-3);
        }
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            if (status != 0)
            {
                return;
            }

            if (HasCurrentTimeService(gatt))
            {
                BluetoothGattCharacteristic timeCharacteristic =
                    gatt.GetService(UUID.FromString("00001805-0000-1000-8000-00805f9b34fb"))
                    .GetCharacteristic(
                        UUID.FromString("00002A2B-0000-1000-8000-00805f9b34fb"));
                timeCharacteristic.SetValue(GetCurrentTimeLocal());
                gatt.WriteCharacteristic(timeCharacteristic);
            }
            else
            {
                ListenToMeasurements(gatt);
            }
        }
Ejemplo n.º 29
0
 public int Transfer(byte[] data)
 {
     if (_ServerCh != null && _ServerDevice != null)
     {
         var sv = _ServerCh.SetValue(data);
         if (sv)
         {
             var wc = _ServerDevice.WriteCharacteristic(_ServerCh);
             if (wc)
             {
                 return(data.Length);
             }
             else
             {
                 return(-2);
             }
         }
         return(-1);
     }
     return(0);
 }
Ejemplo n.º 30
0
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            if (status != GattStatus.Success)
            {
                _MainActivity.AppendLogText("Failed to discover services");
                gatt.Disconnect();
                return;
            }
            _MainActivity.AppendLogText("Services Discovered");

            var configurationService = gatt.GetService(_ConfigurationService);

            if (configurationService == null)
            {
                _MainActivity.AppendLogText("Unable to find configuration service");
                gatt.Disconnect();
                return;
            }

            var configurationKeyCharacteristic = configurationService.GetCharacteristic(_ConfigurationKeyCharacteristic);

            if (configurationKeyCharacteristic == null)
            {
                _MainActivity.AppendLogText("Unable to find characteristic");
                gatt.Disconnect();
                return;
            }

            var configKeyBytes = StringToByteArrayFastest(_ConfigurationValue);

            configurationKeyCharacteristic.SetValue(configKeyBytes);
            if (!gatt.WriteCharacteristic(configurationKeyCharacteristic))
            {
                _MainActivity.AppendLogText("Failed to write characteristic");
            }
            else
            {
                _MainActivity.AppendLogText("Writing characteristic...");
            }
        }