/// <summary> /// Start device discovery /// </summary> public void StartDeviceDiscovery() { _logger?.LogDebug("Start device discovery"); _bgLib.BLEEventGAPScanResponse += OnScanResponse; _bgLib.SendCommand(_bleModuleConnection.SerialPort, _bgLib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1)); _bgLib.SendCommand(_bleModuleConnection.SerialPort, _bgLib.BLECommandGAPDiscover(1)); }
/// <summary> /// Send command to serialPort. /// </summary> private void SendCommand(byte[] cmd) { if (!CheckPortOpened()) { return; } try { bglib.SendCommand(serialPort, cmd); } catch (Exception ex) { WriteTxtLog($"Serial port write failed: {ex.Message}"); } }
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; }