Task <GattService> DoGetPrimaryService(BluetoothUuid service)
        {
            return(Task.Run(() =>
            {
                EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.AutoReset);

                GattService matchingService = null;

                ((CBPeripheral)Device).DiscoveredService += (sender, args) =>
                {
                    handle.Set();
                };

                ((CBPeripheral)Device).DiscoverServices(new CBUUID[] { service });

                handle.WaitOne();

                foreach (CBService cbservice in ((CBPeripheral)Device).Services)
                {
                    if ((BluetoothUuid)cbservice.UUID == service)
                    {
                        matchingService = new GattService(Device, cbservice);
                    }
                }

                return matchingService;
            }));
        }
        Task <GattService> DoGetPrimaryService(BluetoothUuid service)
        {
            _servicesDiscoveredHandle.WaitOne();
            ABluetooth.BluetoothGattService nativeService = NativeGatt.GetService(service);

            return(Task.FromResult(nativeService is null ? null : new GattService(Device, nativeService)));
        }
Ejemplo n.º 3
0
        Task <GattDescriptor> PlatformGetDescriptor(BluetoothUuid descriptor)
        {
            TaskCompletionSource <GattDescriptor> tcs = new TaskCompletionSource <GattDescriptor>();
            CBPeripheral peripheral = Service.Device;

            void handler(object sender, CBCharacteristicEventArgs args)
            {
                peripheral.DiscoveredDescriptor -= handler;

                if (args.Error != null)
                {
                    tcs.SetException(new Exception(args.Error.ToString()));
                    return;
                }

                foreach (CBDescriptor cbdescriptor in _characteristic.Descriptors)
                {
                    if ((BluetoothUuid)cbdescriptor.UUID == descriptor)
                    {
                        tcs.SetResult(new GattDescriptor(this, cbdescriptor));
                        return;
                    }
                }

                tcs.SetResult(null);
            }

            peripheral.DiscoveredDescriptor += handler;
            peripheral.DiscoverDescriptors(_characteristic);

            return(tcs.Task);
        }
Ejemplo n.º 4
0
 private void Gatt_CharacteristicChanged(object sender, BluetoothUuid e)
 {
     if (e == Uuid)
     {
         characteristicValueChanged?.Invoke(this, EventArgs.Empty);
     }
 }
Ejemplo n.º 5
0
        async Task <GattService> PlatformGetPrimaryService(BluetoothUuid service)
        {
            await WaitForServiceDiscovery();

            ABluetooth.BluetoothGattService nativeService = _gatt.GetService(service);

            return(nativeService is null ? null : new GattService(Device, nativeService));
        }
Ejemplo n.º 6
0
        async Task <GattService> PlatformGetPrimaryService(BluetoothUuid service)
        {
            var result = await Device.NativeDevice.GetGattServicesForUuidAsync(service, Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);

            if (result == null || result.Services.Count == 0)
            {
                return(null);
            }

            return(new GattService(Device, result.Services[0], true));
        }
Ejemplo n.º 7
0
        Task <GattDescriptor> DoGetDescriptor(BluetoothUuid descriptor)
        {
            var gattDescriptor = _characteristic.GetDescriptor(descriptor);

            if (gattDescriptor is null)
            {
                return(Task.FromResult <GattDescriptor>(null));
            }

            return(Task.FromResult(new GattDescriptor(this, gattDescriptor)));
        }
Ejemplo n.º 8
0
        private async Task <GattService> PlatformGetIncludedServiceAsync(BluetoothUuid service)
        {
            var servicesResult = await _service.GetIncludedServicesForUuidAsync(service, BluetoothCacheMode.Cached);

            if (servicesResult.Status == WBluetooth.GattCommunicationStatus.Success)
            {
                return(new GattService(Device, servicesResult.Services[0], false));
            }

            return(null);
        }
Ejemplo n.º 9
0
        async Task <GattCharacteristic> PlatformGetCharacteristic(BluetoothUuid characteristic)
        {
            var result = await _service.GetCharacteristicsForUuidAsync(characteristic, Windows.Devices.Bluetooth.BluetoothCacheMode.Cached);

            if (result.Status == WBluetooth.GattCommunicationStatus.Success && result.Characteristics.Count > 0)
            {
                return(new GattCharacteristic(this, result.Characteristics[0]));
            }

            return(null);
        }
Ejemplo n.º 10
0
        async Task <GattCharacteristic> DoGetCharacteristic(BluetoothUuid characteristic)
        {
            var result = await _service.GetCharacteristicsForUuidAsync(characteristic);

            if (result.Status == WBluetooth.GattCommunicationStatus.Success && result.Characteristics.Count > 0)
            {
                return(new GattCharacteristic(this, result.Characteristics[0]));
            }

            return(null);
        }
Ejemplo n.º 11
0
        private Task <GattCharacteristic> DoGetCharacteristic(BluetoothUuid characteristic)
        {
            var nativeCharacteristic = NativeService.GetCharacteristic(characteristic);

            if (nativeCharacteristic is null)
            {
                return(Task.FromResult((GattCharacteristic)null));
            }

            return(Task.FromResult(new GattCharacteristic(this, nativeCharacteristic)));
        }
Ejemplo n.º 12
0
        private async Task <GattService> DoGetIncludedServiceAsync(BluetoothUuid service)
        {
            foreach (var includedService in NativeService.IncludedServices)
            {
                if (includedService.Uuid == service)
                {
                    return(new GattService(Device, includedService));
                }
            }

            return(null);
        }
Ejemplo n.º 13
0
        async Task <GattService> PlatformGetPrimaryService(BluetoothUuid service)
        {
            Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceServicesResult result = null;

            result = await Device.NativeDevice.GetGattServicesForUuidAsync(service, Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);

            if (result == null || result.Services.Count == 0)
            {
                return(null);
            }

            return(new GattService(Device, result.Services[0]));
        }
Ejemplo n.º 14
0
        private async Task <GattService> PlatformGetIncludedServiceAsync(BluetoothUuid service)
        {
            ((CBPeripheral)Device).DiscoverIncludedServices(new CBUUID[] { }, _service);

            foreach (var includedService in _service.IncludedServices)
            {
                if ((BluetoothUuid)includedService.UUID == service)
                {
                    return(new GattService(Device, includedService));
                }
            }

            return(null);
        }
Ejemplo n.º 15
0
        Task <GattDescriptor> PlatformGetDescriptor(BluetoothUuid descriptor)
        {
            GattDescriptor matchingDescriptor = null;

            ((CBPeripheral)Service.Device).DiscoverDescriptors(_characteristic);
            foreach (CBDescriptor cbdescriptor in _characteristic.Descriptors)
            {
                if ((BluetoothUuid)cbdescriptor.UUID == descriptor)
                {
                    matchingDescriptor = new GattDescriptor(this, cbdescriptor);
                    break;
                }
            }

            return(Task.FromResult(matchingDescriptor));
        }
Ejemplo n.º 16
0
        Task <GattCharacteristic> PlatformGetCharacteristic(BluetoothUuid characteristic)
        {
            ((CBPeripheral)Device).DiscoverCharacteristics(new CBUUID[] { characteristic }, _service);
            GattCharacteristic matchingCharacteristic = null;

            foreach (CBCharacteristic cbcharacteristic in _service.Characteristics)
            {
                if ((BluetoothUuid)cbcharacteristic.UUID == characteristic)
                {
                    matchingCharacteristic = new GattCharacteristic(this, cbcharacteristic);
                    break;
                }
            }

            return(Task.FromResult(matchingCharacteristic));
        }
Ejemplo n.º 17
0
        IReadOnlyDictionary <BluetoothUuid, byte[]> GetServiceData()
        {
            Dictionary <BluetoothUuid, byte[]> serviceData = new Dictionary <BluetoothUuid, byte[]>();

            foreach (BluetoothLEAdvertisementDataSection data in _advertisement.DataSections)
            {
                byte[] uuidBytes = new byte[16];

                if (data.DataType == BluetoothLEAdvertisementDataTypes.ServiceData128BitUuids)
                {
                    // read uuid
                    data.Data.CopyTo(0, uuidBytes, 0, 16);
                    // read data
                    byte[] dataBytes = new byte[data.Data.Length - 16];
                    data.Data.CopyTo(16, dataBytes, 0, dataBytes.Length);
                    serviceData.Add(new Guid(uuidBytes), dataBytes);
                }
                else if (data.DataType == BluetoothLEAdvertisementDataTypes.ServiceData32BitUuids)
                {
                    // read uuid
                    data.Data.CopyTo(0, uuidBytes, 0, 4);
                    // read data
                    byte[] dataBytes = new byte[data.Data.Length - 4];
                    data.Data.CopyTo(4, dataBytes, 0, dataBytes.Length);
                    serviceData.Add(BluetoothUuid.FromShortId(BitConverter.ToUInt16(uuidBytes, 0)), dataBytes);
                }
                else if (data.DataType == BluetoothLEAdvertisementDataTypes.ServiceData16BitUuids)
                {
                    // read uuid
                    data.Data.CopyTo(0, uuidBytes, 0, 2);
                    // read data
                    byte[] dataBytes = new byte[data.Data.Length - 2];
                    data.Data.CopyTo(2, dataBytes, 0, dataBytes.Length);
                    serviceData.Add(BluetoothUuid.FromShortId(BitConverter.ToUInt16(uuidBytes, 0)), dataBytes);
                }
                else
                {
                    if (data.DataType != BluetoothLEAdvertisementDataTypes.Flags)
                    {
                        System.Diagnostics.Debug.WriteLine(data.DataType);
                    }
                }
            }

            return(new ReadOnlyDictionary <BluetoothUuid, byte[]>(serviceData));
        }
Ejemplo n.º 18
0
        private Task <GattService> PlatformGetIncludedServiceAsync(BluetoothUuid service)
        {
            TaskCompletionSource <GattService> tcs = new TaskCompletionSource <GattService>();
            CBPeripheral peripheral = Device;

            void handler(object sender, CBServiceEventArgs args)
            {
                peripheral.DiscoveredIncludedService -= handler;

                if (args.Error != null)
                {
                    tcs.SetException(new Exception(args.Error.ToString()));
                }
                else
                {
                    tcs.SetResult(new GattService(Device, args.Service));
                }
            }

            peripheral.DiscoveredIncludedService += handler;
            peripheral.DiscoverIncludedServices(new CBUUID[] { service }, _service);

            return(tcs.Task);
        }
Ejemplo n.º 19
0
 public Task <GattService> GetPrimaryServiceAsync(BluetoothUuid service)
 {
     return(PlatformGetPrimaryService(service));
 }
Ejemplo n.º 20
0
 public Task <GattDescriptor> GetDescriptorAsync(BluetoothUuid descriptor)
 {
     return(PlatformGetDescriptor(descriptor));
 }
Ejemplo n.º 21
0
 public Task <GattService> GetIncludedServiceAsync(BluetoothUuid service)
 {
     return(PlatformGetIncludedServiceAsync(service));
 }
Ejemplo n.º 22
0
 public Task <GattCharacteristic> GetCharacteristicAsync(BluetoothUuid characteristic)
 {
     return(PlatformGetCharacteristic(characteristic));
 }
Ejemplo n.º 23
0
 Task <GattService> PlatformGetPrimaryService(BluetoothUuid service)
 {
     return(Task.FromResult((GattService)null));
 }
Ejemplo n.º 24
0
 public Task <GattService> GetPrimaryService(BluetoothUuid service)
 {
     return(DoGetPrimaryService(service));
 }