private void ProcessBgBle_FetchCharacteristics(BgBleDeviceService deviceClientGroup)
        {
            if (deviceClientGroup == null)
            {
                return;
            }
            _currentActionTokenSource = new CancellationTokenSource();
            var fetchCharacteristicsTokenSource =
                CancellationTokenSource.CreateLinkedTokenSource(_applicationCancellationTokenSource.Token,
                                                                _currentActionTokenSource.Token);

            _logger.LogInformation($"BgBle Service: Fetching characteristics for {deviceClientGroup.ServiceId}");
            _bleRepository.FetchCharacteristics(fetchCharacteristicsTokenSource.Token,
                                                DeviceTimeout, deviceClientGroup);
        }
 public void Connect(CancellationToken connectToken, CancellationToken appToken,
                     int connectTimeoutInMilliseconds, BgBleDevice deviceToConnect)
 {
     try
     {
         var lockTaken = false;
         Monitor.TryEnter(ThreadLock, 150, ref lockTaken);
         if (lockTaken)
         {
             if (_busy)
             {
                 return;
             }
             _busy = true;
             Monitor.Exit(ThreadLock);
         }
         _device        = deviceToConnect;
         _currentAction = new CancellationTokenSource();
         var connectToDevicesTokenSource =
             CancellationTokenSource.CreateLinkedTokenSource(connectToken,
                                                             _currentAction.Token);
         _bglib.SendCommand(_serialPort,
                            _bglib.BLECommandGAPConnectDirect(deviceToConnect.Identifier,
                                                              deviceToConnect.Advertisement.AddressType, 0x20, 0x30, 0x100, 0));
         WaitHandle.WaitAny(new[] { connectToDevicesTokenSource.Token.WaitHandle }, connectTimeoutInMilliseconds);
         // cleanup takes 125ms
         _bglib.SendCommand(_serialPort, _bglib.BLECommandGAPEndProcedure());
         WaitHandle.WaitAny(new[] { appToken.WaitHandle }, 130);
         _logger.LogInformation(
             $"ControlPlus Repository: Connected to device {deviceToConnect.Address} with {_device.Services.Count} services");
     }
     catch (Exception exception)
     {
         _logger.LogWarning(exception,
                            $"ControlPlus Repository: Unable to connect to device {deviceToConnect.Address}");
     }
     _deviceService = null;
     _device        = null;
     _busy          = false;
 }
 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;
 }
示例#4
0
 protected virtual void Dispose(bool disposing)
 {
     _bgBleService       = null;
     _bgBleDeviceService = null;
 }
示例#5
0
 public BluegigaPoweredUpBluetoothService(BgBleService bgBleService, BgBleDeviceService bgBleDeviceService)
 {
     _bgBleService       = bgBleService;
     _bgBleDeviceService = bgBleDeviceService;
 }