Exemplo n.º 1
0
        public void Parse_TestVector2_AdvertisingDataParsedCorrectly()
        {
            // Arrange
            var bytes = "020106110707b9f9d750a420897740cbfd2cc18048090842474d3131312053".HexStringToByteArray();

            // Act
            var advertisingData = BleAdvertisingDataParser.Parse(bytes);

            // Assert
            Assert.AreEqual(3, advertisingData.Count);
            Assert.AreEqual(BleAdvertisingDataType.Flags, advertisingData[0].Type);
            Assert.AreEqual(0x06, advertisingData[0].ToUint8());
            Assert.AreEqual(BleAdvertisingDataType.CompleteListof128BitServiceClassUUIDs, advertisingData[1].Type);
            Assert.AreEqual(new Guid("4880c12c-fdcb-4077-8920-a450d7f9b907"), advertisingData[1].ToGuid());
            Assert.AreEqual(BleAdvertisingDataType.ShortenedLocalName, advertisingData[2].Type);
            Assert.AreEqual("BGM111 S", advertisingData[2].ToAsciiString());
        }
Exemplo n.º 2
0
        public void Parse_TestVector1_AdvertisingDataParsedCorrectly()
        {
            // Arrange
            var bytes = "020106030309181409546865726d6f6d65746572204578616d706c65".HexStringToByteArray();

            // Act
            var advertisingData = BleAdvertisingDataParser.Parse(bytes);

            // Assert
            Assert.AreEqual(3, advertisingData.Count);
            Assert.AreEqual(BleAdvertisingDataType.Flags, advertisingData[0].Type);
            Assert.AreEqual(0x06, advertisingData[0].ToUint8());
            Assert.AreEqual(BleAdvertisingDataType.CompleteListof16BitServiceClassUUIDs, advertisingData[1].Type);
            Assert.AreEqual((ushort)0x1809, advertisingData[1].ToUint16());
            Assert.AreEqual(BleAdvertisingDataType.CompleteLocalName, advertisingData[2].Type);
            Assert.AreEqual("Thermometer Example", advertisingData[2].ToAsciiString());
        }
        public async Task <ScanResponseEventArgs> ExecuteAsync(ushort manufacturerId, CancellationToken cancellationToken, int timeout = DefaultTimeout)
        {
            Logger?.LogDebug($"Discover device by Manufacturer ID {manufacturerId}");

            var taskCompletionSource = new TaskCompletionSource <ScanResponseEventArgs>();

            using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
            {
                cancellationTokenSource.CancelAfter(timeout);

                void OnScanResponse(object sender, ScanResponseEventArgs e)
                {
                    var advertisementData             = BleAdvertisingDataParser.Parse(e.data);
                    var manufacturerAdvertisementData = advertisementData.SingleOrDefault(x => x.Type == BleAdvertisingDataType.ManufacturerSpecificData);

                    if (manufacturerAdvertisementData?.GetManufacturerId() == manufacturerId)
                    {
                        taskCompletionSource.SetResult(e);
                    }
                }

                try
                {
                    BgLib.BLEEventGAPScanResponse += OnScanResponse;

                    using (cancellationTokenSource.Token.Register(() => taskCompletionSource.SetCanceled(), false))
                    {
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1));
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPDiscover(1));

                        return(await taskCompletionSource.Task.ConfigureAwait(false));
                    }
                }
                finally
                {
                    BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPEndProcedure());

                    BgLib.BLEEventGAPScanResponse -= OnScanResponse;
                }
            }
        }
Exemplo n.º 4
0
        public async Task <ScanResponseEventArgs> ExecuteAsync(byte[] serviceUuid, CancellationToken cancellationToken, int timeout = DefaultTimeout)
        {
            Logger?.LogDebug($"Discover device by service Uuid {serviceUuid.ToHexString()}");

            var taskCompletionSource = new TaskCompletionSource <ScanResponseEventArgs>();

            using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
            {
                cancellationTokenSource.CancelAfter(timeout);

                void OnScanResponse(object sender, ScanResponseEventArgs e)
                {
                    var advertisementData = BleAdvertisingDataParser.Parse(e.data);

                    if (advertisementData.Where(x => ServiceUuidAdvertisements.Contains(x.Type)).Any(x => x.Data.SequenceEqual(serviceUuid)))
                    {
                        taskCompletionSource.SetResult(e);
                    }
                }

                try
                {
                    BgLib.BLEEventGAPScanResponse += OnScanResponse;

                    using (cancellationTokenSource.Token.Register(() => taskCompletionSource.SetCanceled(), false))
                    {
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1));
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPDiscover(1));

                        return(await taskCompletionSource.Task.ConfigureAwait(false));
                    }
                }
                finally
                {
                    BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPEndProcedure());

                    BgLib.BLEEventGAPScanResponse -= OnScanResponse;
                }
            }
        }