Пример #1
0
        public async Task BleManager_GetDeviceCharacteristics_Found()
        {
            var deviceId = "device-Id";
            var gsUuid   = Guid.Parse("4C088D33-76C6-4094-8C4A-65A80430678A");
            var gs       = new BleGattService {
                DeviceId = deviceId, Uuid = gsUuid
            };

            gs.Characteristics = new BleGattCharacteristic[] { };

            var bleAdapter = new DummyBleAdapter();

            var device = new BleDevice
            {
                Id   = deviceId,
                Name = "some-device-name"
            };

            var bm = new BleManager(new[] { bleAdapter }, null, null);

            bleAdapter.SetGetGattServices(device, new[] { gs });

            var res = await bm.GetDeviceCharacteristics(deviceId, gsUuid.ToString());

            res.ShouldBe(gs.Characteristics);
        }
Пример #2
0
        public void BleManager_RegisterToCharacteristicNotificationsasses()
        {
            const string deviceUuid         = "device-Id";
            const string serviceUuid        = "4C088D33-76C6-4094-8C4A-65A80430678A";
            const string characteristicUuid = "some-characteristic-id";
            const string message            = "this is notifucation content";
            var          gs = new BleGattService {
                DeviceId = deviceUuid, Uuid = Guid.Parse(serviceUuid)
            };

            gs.Characteristics = new BleGattCharacteristic[] { };

            var bleAdapter = new DummyBleAdapter();
            var device     = new BleDevice
            {
                Id   = deviceUuid,
                Name = "some-device-name"
            };

            var notifier = new Mock <INotifier>();
            var bm       = new BleManager(new[] { bleAdapter }, notifier.Object, null);

            bleAdapter.SetGetGattServices(device, new[] { gs });

            bleAdapter.RaiseDeviceValueChangedEvent(deviceUuid, serviceUuid, characteristicUuid, message);
            notifier.Verify(n => n.Push(It.Is <string>(s => s == deviceUuid), It.Is <BleDeviceValueChangedEventArgs>(
                                            b =>
                                            b.DeviceUuid == deviceUuid &&
                                            b.ServiceUuid == serviceUuid &&
                                            b.CharacteristicUuid == characteristicUuid &&
                                            b.Message == message)), Times.Once);
        }
Пример #3
0
        public void BleManager_GetDeviceCharacteristics_NotFound()
        {
            var deviceId = "device-Id";
            var gsUuid   = Guid.Parse("4C088D33-76C6-4094-8C4A-65A80430678A");
            var gs       = new BleGattService {
                DeviceId = deviceId, Uuid = gsUuid
            };

            gs.Characteristics = new BleGattCharacteristic[] { };

            var bleAdapter = new DummyBleAdapter();
            var device     = new BleDevice
            {
                Id   = deviceId,
                Name = "some-device-name"
            };

            var bm = new BleManager(new[] { bleAdapter }, null, null);

            bleAdapter.SetGetGattServices(device, new[] { gs });

            var task = bm.GetDeviceCharacteristics(deviceId, "not-exists-gatt-service-id");

            task.Exception.InnerExceptions.First().ShouldBeOfType <NullReferenceException>();
        }
Пример #4
0
        public async Task BleManager_RegisterToCharacteristicNotifications_Fails(bool readResult)
        {
            var deviceId         = "device-Id";
            var gattServiceId    = "4C088D33-76C6-4094-8C4A-65A80430678A";
            var characteristicId = "some-characteristic-id";
            var gs = new BleGattService {
                DeviceId = deviceId, Uuid = Guid.Parse(gattServiceId)
            };

            gs.Characteristics = new BleGattCharacteristic[] { };

            var bleAdapter = new DummyBleAdapter();
            var device     = new BleDevice
            {
                Id   = deviceId,
                Name = "some-device-name"
            };

            var bm = new BleManager(new[] { bleAdapter }, null, null);

            bleAdapter.SetGetGattServices(device, new[] { gs });
            bleAdapter.BleNotificationResult = readResult;
            var res = await bm.RegisterToCharacteristicNotifications(deviceId, gattServiceId, characteristicId);

            res.ShouldBe(readResult);
        }
Пример #5
0
        public async Task BleManager_WriteToCharacteristic(bool writeResult)
        {
            var deviceId         = "device-Id";
            var gattServiceId    = "4C088D33-76C6-4094-8C4A-65A80430678A";
            var characteristicId = "some-characteristic-id";
            var gs = new BleGattService {
                DeviceId = deviceId, Uuid = Guid.Parse(gattServiceId)
            };

            gs.Characteristics = new BleGattCharacteristic[] { };

            var bleAdapter = new DummyBleAdapter();
            var device     = new BleDevice
            {
                Id   = deviceId,
                Name = "some-device-name"
            };

            var bm = new BleManager(new[] { bleAdapter }, null, null);

            bleAdapter.SetGetGattServices(device, new[] { gs });
            bleAdapter.WriteToCharacteristicResult = writeResult;
            var res = await bm.WriteToCharacteristric(deviceId, gattServiceId, characteristicId, new List <byte>());

            res.ShouldBe(writeResult);
        }
Пример #6
0
        /// <summary>
        /// Get the list of the GATT characteristics included in a specific GATT service
        /// </summary>
        /// <returns>List of the included GATT characteristics</returns>
        public async Task <IList <BleGattCharacteristic> > GetCharacteristicsAsync(BleGattService service)
        {
            List <BleGattCharacteristic> characteristics = new List <BleGattCharacteristic>();

            GattDeviceService         gatt_service = service.Context as GattDeviceService;
            GattCharacteristicsResult result       = await gatt_service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached);

            foreach (GattCharacteristic characteristic in result.Characteristics)
            {
                BleGattCharacteristic ble_characteristic = new BleGattCharacteristic();
                ble_characteristic.Name     = "";
                ble_characteristic.Guid     = characteristic.Uuid;
                ble_characteristic.Context  = characteristic;
                ble_characteristic.CanRead  = characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read);
                ble_characteristic.CanWrite = ((characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write)) ||
                                               (characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.WriteWithoutResponse)));
                ble_characteristic.CanNotify = ((characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify)) ||
                                                (characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Indicate)));

                characteristics.Add(ble_characteristic);
                _ble_characteristics.Add(characteristic, ble_characteristic);
            }

            return(characteristics);
        }
Пример #7
0
        /// <summary>
        /// Get the list of the primary GATT services
        /// </summary>
        /// <returns>List of the primary GATT services</returns>
        public async Task <IList <BleGattService> > GetServicesAsync()
        {
            List <BleGattService> services = new List <BleGattService>();

            GattDeviceServicesResult result = await _ble_device.GetGattServicesAsync(BluetoothCacheMode.Uncached);

            foreach (GattDeviceService gatt_service in result.Services)
            {
                BleGattService service = new BleGattService();
                service.Name    = "";
                service.Guid    = gatt_service.Uuid;
                service.Context = gatt_service;

                services.Add(service);
            }

            return(services);
        }
Пример #8
0
        /// <summary>
        /// Get the list of the GATT services included in a specific GATT service
        /// </summary>
        /// <returns>List of the included GATT services</returns>
        public async Task <IList <BleGattService> > GetServicesAsync(BleGattService service)
        {
            List <BleGattService> services = new List <BleGattService>();

            GattDeviceService        gatt_service = service.Context as GattDeviceService;
            GattDeviceServicesResult result       = await gatt_service.GetIncludedServicesAsync(BluetoothCacheMode.Uncached);

            foreach (GattDeviceService included_service in result.Services)
            {
                BleGattService ble_service = new BleGattService();
                ble_service.Name    = "";
                ble_service.Guid    = gatt_service.Uuid;
                ble_service.Context = included_service;

                services.Add(ble_service);
            }

            return(services);
        }
Пример #9
0
        public async Task GetGattService_ReturnService()
        {
            var gattServiceId = "DE9522AE-D0B0-49DD-A2E7-82C1D76DE7C0";
            var deviceId      = "deviceId";

            var expGattService = new BleGattService
            {
                DeviceId = deviceId,
                Uuid     = new Guid(gattServiceId)
            };
            var bleManager = new Mock <IBleManager>();

            bleManager.Setup(b => b.GetDeviceGattServices(It.IsAny <string>()))
            .ReturnsAsync(new[] { expGattService });


            var gs = await BleManagerExtensions.GetGattServiceById(bleManager.Object, deviceId, gattServiceId);

            gs.ShouldBe(expGattService);
        }
Пример #10
0
        private void OnClientDatabaseDiscoveryResult(CSR_BLE_DATABASE_DISCOVERY_RESULT database)
        {
            var gattDevice = GattConnectDevices.Find(d => d.Handle == database.connectHandle);

            if (gattDevice == null || database.result != 0)
            {
                Error("[CSR]:Database Discover Error " + gattDevice?.Address);
                return;
            }

            gattDevice.NService = database.nServices;

            //update GattDevice services and characteristics
            for (var i = 0; i < database.nServices; i++)
            {
                var service =
                    (CSR_BLE_SERVICE)
                    Marshal.PtrToStructure(database.services + i * Marshal.SizeOf(typeof(CSR_BLE_SERVICE)),
                                           typeof(CSR_BLE_SERVICE));
                Debug($"[CSR]:Service -Uuid:{service.uuid} -nCharacters:{service.nCharacteristics}");

                var gattService = new BleGattService
                {
                    GattDevice  = gattDevice,
                    Uuid        = service.uuid.uuid16,
                    StartHandle = service.startHandle,
                    EndHandle   = service.endHandle,
                    NCharacters = service.nCharacteristics
                };

                gattDevice.AddGattService(gattService);

                // add gattCharacteristic to gattService
                for (var j = 0; j < service.nCharacteristics; j++)
                {
                    var character = (CSR_BLE_CHARACTERISTIC)
                                    Marshal.PtrToStructure(
                        service.characteristics + j * Marshal.SizeOf(typeof(CSR_BLE_CHARACTERISTIC)),
                        typeof(CSR_BLE_CHARACTERISTIC));

                    var gattCharacter = new BleGattCharacteristic
                    {
                        Service           = gattService,
                        Uuid              = character.uuid.uuid16,
                        Handle            = character.handle,
                        DeclarationHandle = character.declHandle,
                        NDescriptors      = character.nDescriptors,
                        Properties        = character.properties
                    };

                    if (character.nDescriptors > 0)
                    {
                        var descriptor =
                            (CSR_BLE_CHARACTERISTIC_DSC)Marshal.PtrToStructure(character.descriptors,
                                                                               typeof(CSR_BLE_CHARACTERISTIC_DSC));
                        gattCharacter.DescriptorHandle = descriptor.handle;
                    }

                    gattService.AddCharacter(gattCharacter);
                }
            }

            _databaseEvent.Set();
        }