Exemple #1
0
        private async Task RunAsync()
        {
            _bleModuleConnection.Start("COM3");

            byte[]         deviceAddress     = null;
            BleAddressType deviceAddressType = BleAddressType.Public;

            _bleDeviceDiscovery.ScanResponse += (sender, args) =>
            {
                // Filter advertisement data by the complete local name
                if (args.ParsedData.Any(x => x.Type == BleAdvertisingDataType.CompleteLocalName && x.ToAsciiString() == "DEVICE-NAME"))
                {
                    _logger.LogInformation("Device found");

                    deviceAddress     = args.Address;
                    deviceAddressType = args.AddressType;

                    _bleDeviceDiscovery.StopDeviceDiscovery();

                    _discoveryTimeout.Set();
                }
            };

            _bleDeviceDiscovery.StartDeviceDiscovery();

            try
            {
                _discoveryTimeout.WaitOne(10000);

                // Connecting to a device will log the discovered services and characteristics
                var device = await _bleDeviceManager.ConnectAsync(deviceAddress, deviceAddressType);
            }
            catch
            {
                _bleDeviceDiscovery.StopDeviceDiscovery();

                _logger.LogError("Device coulnd not be found");
            }

            _bleModuleConnection.Stop();
        }
Exemple #2
0
        private async Task RunAsync()
        {
            _bleModuleConnection.Start("COM3");

            _bleDeviceDiscovery.ScanResponse += (sender, args) =>
            {
                // Filter advertisement data by an advertised service uuid
                if (args.ParsedData.Any(x => x.Type == BleAdvertisingDataType.CompleteListof128BitServiceClassUUIDs && x.ToGuid() == new System.Guid("0a5d8ff1-67f2-4a14-b6b7-4e3baa285f12")))
                {
                    _logger?.LogInformation($"Device discovered, Address={args.Address.Reverse().ToArray().ToHexString(":")}, Data={args.Data.ToHexString()}, ParsedData={string.Join(";", args.ParsedData.Select(x => $"{x.Type}={x.ToDebugString()}"))}");
                }
            };

            _bleDeviceDiscovery.StartDeviceDiscovery();

            await Task.Delay(10000);

            _bleDeviceDiscovery.StopDeviceDiscovery();

            _bleModuleConnection.Stop();
        }