private void OnCharacteristicFound(object sender, FindInformationFoundEventArgs e)
        {
            if (_deviceService == null)
            {
                return;
            }
            var characteristic = BleMappings.MapBleDeviceCharacteristic(e);

            if (characteristic == null)
            {
                return;
            }
            if (characteristic.CharacteristicId != Guid.Empty)
            {
                _lastCharacteristic = characteristic;
                if (!_deviceService.Characteristics.ContainsKey(characteristic.CharacteristicId))
                {
                    _deviceService.Characteristics.Add(characteristic.CharacteristicId, characteristic);
                }
            }
            else if (_lastCharacteristic?.Handle > 0 && _lastCharacteristic?.ClientCharacteristicConfiguratorDescriptorHandle == 0)
            {
                _lastCharacteristic.ClientCharacteristicConfiguratorDescriptorHandle = characteristic.Handle;
            }
        }
        private void ProcessBgBle_AttachCharacteristic(BgBleDeviceCharacteristic deviceCharacteristic)
        {
            if (deviceCharacteristic == null)
            {
                return;
            }
            _currentActionTokenSource = new CancellationTokenSource();
            var attachCharacteristicTokenSource =
                CancellationTokenSource.CreateLinkedTokenSource(_applicationCancellationTokenSource.Token,
                                                                _currentActionTokenSource.Token);

            _logger.LogInformation($"BgBle Service: Attaching characteristic {deviceCharacteristic.CharacteristicId}");
            _bleRepository.AttachCharacteristic(attachCharacteristicTokenSource.Token,
                                                DeviceTimeout, ProcessBgBle_Callback_Attribute, deviceCharacteristic);
        }
 public void FetchCharacteristics(CancellationToken appToken, int connectTimeoutInMilliseconds,
                                  BgBleDeviceService deviceService)
 {
     deviceService.Characteristics.Clear();
     try
     {
         var lockTaken = false;
         Monitor.TryEnter(ThreadLock, 150, ref lockTaken);
         if (lockTaken)
         {
             if (_busy)
             {
                 return;
             }
             _busy = true;
             Monitor.Exit(ThreadLock);
         }
         _deviceService = deviceService;
         _currentAction = new CancellationTokenSource();
         var populateClientInformationTokenSource =
             CancellationTokenSource.CreateLinkedTokenSource(appToken,
                                                             _currentAction.Token);
         _bglib.SendCommand(_serialPort,
                            _bglib.BLECommandATTClientFindInformation(deviceService.ConnectionHandle, deviceService.Start,
                                                                      deviceService.End));
         WaitHandle.WaitAny(new[] { populateClientInformationTokenSource.Token.WaitHandle },
                            connectTimeoutInMilliseconds);
     }
     catch (Exception exception)
     {
         _logger.LogWarning(exception,
                            $"ControlPlus Repository: Unable to populate Client Information {deviceService.ServiceId}");
     }
     _lastCharacteristic = null;
     _deviceService      = null;
     _device             = null;
     _busy = false;
 }
 public BluegigaPoweredUpBluetoothCharacteristic(BgBleService bgBleService,
     BgBleDeviceCharacteristic bgBleDeviceCharacteristic)
 {
     _bgBleService = bgBleService;
     _bgBleDeviceCharacteristic = bgBleDeviceCharacteristic;
 }
 public void WriteValueToCharacteristic(BgBleDeviceCharacteristic deviceCharacteristic, byte[] dataToWrite)
 {
     _bglib.SendCommand(_serialPort,
                        _bglib.BLECommandATTClientAttributeWrite(deviceCharacteristic.ConnectionHandle,
                                                                 deviceCharacteristic.Handle, dataToWrite));
 }
        public void AttachCharacteristic(CancellationToken appToken, int connectTimeoutInMilliseconds,
                                         Action <BgBleDeviceAttribute> attributeNotifications, BgBleDeviceCharacteristic deviceCharacteristic)
        {
            if (deviceCharacteristic.ClientCharacteristicConfiguratorDescriptorHandle == 0)
            {
                return;
            }
            try
            {
                var lockTaken = false;
                Monitor.TryEnter(ThreadLock, 150, ref lockTaken);
                if (lockTaken)
                {
                    if (_busy)
                    {
                        return;
                    }
                    _busy = true;
                    Monitor.Exit(ThreadLock);
                }
                _attributeNotifications = attributeNotifications;
                _currentAction          = new CancellationTokenSource();
                var attachCharacteristicTokenSource =
                    CancellationTokenSource.CreateLinkedTokenSource(appToken,
                                                                    _currentAction.Token);
                _bglib.SendCommand(_serialPort,
                                   _bglib.BLECommandATTClientAttributeWrite(deviceCharacteristic.ConnectionHandle,
                                                                            deviceCharacteristic.ClientCharacteristicConfiguratorDescriptorHandle, new byte[] { 0x01, 0x00 }));
                WaitHandle.WaitAny(new[] { attachCharacteristicTokenSource.Token.WaitHandle },
                                   connectTimeoutInMilliseconds);
            }
            catch (Exception exception)
            {
                _logger.LogWarning(exception,
                                   $"ControlPlus Repository: Unable to attach characteristic {deviceCharacteristic.CharacteristicId}");
            }

            _busy = false;
        }
 public void WriteValueToCharacteristic(BgBleDeviceCharacteristic deviceCharacteristic, byte[] dataToWrite)
 {
     _bleRepository.WriteValueToCharacteristic(deviceCharacteristic, dataToWrite);
 }