Exemplo n.º 1
0
 public GattCharacteristicValueEventArgs(Peripheral peripheral, GattService service, GattCharacteristic characteristic, byte[] value)
 {
     Peripheral     = peripheral;
     Service        = service;
     Characteristic = characteristic;
     Value          = value;
 }
Exemplo n.º 2
0
        public async Task <IList <GattService> > GetServicesAsync(Peripheral peripheral)
        {
            var connection   = peripheral.Connection;
            var itemsTCS     = new TaskCompletionSource <IList <GroupFoundEventArgs> >();
            var items        = new List <GroupFoundEventArgs>();
            var onGroupFound = new EventHandler <GroupFoundEventArgs>((s, e) =>
            {
                if (e.Connection != connection)
                {
                    return;
                }
                items.Add(e);
            });
            var onProcedureCompleted = new EventHandler <ProcedureCompletedEventArgs>((s, e) =>
            {
                if (e.Connection != connection)
                {
                    return;
                }
                //if (e.ErrorCode == 0)
                //{
                //    itemsTCS.TrySetResult(items);
                //}
                //else
                //{
                //    var error = new ErrorException(e.ErrorCode);
                //    itemsTCS.TrySetException(error);
                //}
                itemsTCS.TrySetResult(items);
            });

            _messageHub.AttributeClient.GroupFound         += onGroupFound;
            _messageHub.AttributeClient.ProcedureCompleted += onProcedureCompleted;
            try
            {
                var start     = (ushort)0x0001;
                var end       = (ushort)0xFFFF;
                var uuid      = (ushort)0x2800;
                var uuidValue = BitConverter.GetBytes(uuid);
                await _messageHub.AttributeClient.ReadByGroupTypeAsync(connection, start, end, uuidValue);

                await itemsTCS.Task;
            }
            finally
            {
                _messageHub.AttributeClient.GroupFound         -= onGroupFound;
                _messageHub.AttributeClient.ProcedureCompleted -= onProcedureCompleted;
            }
            var services = new List <GattService>();

            foreach (var item in items)
            {
                var uuid    = item.UUID.ToUUID();
                var service = new GattService(connection, item.Start, item.End, uuid);
                services.Add(service);
            }
            return(services);
        }
Exemplo n.º 3
0
        public async Task <IList <GattCharacteristic> > GetCharacteristicsAsync(GattService service)
        {
            var connection       = service.Connection;
            var itemsTCS         = new TaskCompletionSource <IList <AttributeValueEventArgs> >();
            var items            = new List <AttributeValueEventArgs>();
            var onAttributeValue = new EventHandler <AttributeValueEventArgs>((s, e) =>
            {
                if (e.Connection != connection ||
                    e.Type != AttributeValueType.ReadByType)
                {
                    return;
                }
                items.Add(e);
            });
            var onProcedureCompleted = new EventHandler <ProcedureCompletedEventArgs>((s, e) =>
            {
                if (e.Connection != connection)
                {
                    return;
                }
                //if (e.ErrorCode == 0)
                //{
                //    itemsTCS.TrySetResult(items);
                //}
                //else
                //{
                //    var error = new ErrorException(e.ErrorCode);
                //    itemsTCS.TrySetException(error);
                //}
                itemsTCS.TrySetResult(items);
            });

            _messageHub.AttributeClient.AttributeValue     += onAttributeValue;
            _messageHub.AttributeClient.ProcedureCompleted += onProcedureCompleted;
            try
            {
                var start     = service.Start;
                var end       = service.End;
                var uuid      = (ushort)0x2803;
                var uuidValue = BitConverter.GetBytes(uuid);
                await _messageHub.AttributeClient.ReadByTypeAsync(connection, start, end, uuidValue);

                await itemsTCS.Task;
            }
            finally
            {
                _messageHub.AttributeClient.AttributeValue     -= onAttributeValue;
                _messageHub.AttributeClient.ProcedureCompleted -= onProcedureCompleted;
            }
            var peripheral      = _peripherals[connection];
            var characteristics = new List <GattCharacteristic>();

            for (var i = 0; i < items.Count; i++)
            {
                var item  = items[i];
                var start = item.AttHandle;
                var end   = i < items.Count - 1
                    ? (ushort)(items[i + 1].AttHandle - 1)
                    : service.End;
                // BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part G, Page 1551
                var properties = (GattCharacteristicProperty)item.Value[0];
                var value      = BitConverter.ToUInt16(item.Value, 1);
                var uuidValue  = new byte[item.Value.Length - 3];
                Array.Copy(item.Value, 3, uuidValue, 0, uuidValue.Length);
                var uuid           = uuidValue.ToUUID();
                var characteristic = new GattCharacteristic(connection, start, end, value, uuid, properties);
                peripheral.Services[value]        = service;
                peripheral.Characteristics[value] = characteristic;
                characteristics.Add(characteristic);
            }
            return(characteristics);
        }