コード例 #1
0
 public GattCharacteristicValueEventArgs(Peripheral peripheral, GattService service, GattCharacteristic characteristic, byte[] value)
 {
     Peripheral     = peripheral;
     Service        = service;
     Characteristic = characteristic;
     Value          = value;
 }
コード例 #2
0
ファイル: Central.cs プロジェクト: yanshouwang/BGLib.NET
        public async Task <Peripheral> ConnectAsync(MAC mac, MacType macType)
        {
            var connectionTCS = new TaskCompletionSource <byte>();
            var onStatus      = new EventHandler <StatusEventArgs>((s, e) =>
            {
                var eMAC     = new MAC(e.Address);
                var eMacType = (MacType)e.AddressType;
                if (eMAC != mac || eMacType != macType)
                {
                    return;
                }
                connectionTCS.TrySetResult(e.Connection);
            });

            _messageHub.Connection.Status += onStatus;
            try
            {
                var value = mac.ToArray();
                var type  = (AddressType)macType;
                await _messageHub.GAP.ConnectDirectAsync(value, type, 0x20, 0x30, 0x100, 0);

                var connection = await connectionTCS.Task;
                var peripheral = new Peripheral(connection, mac, macType);
                _peripherals[connection] = peripheral;
                return(peripheral);
            }
            finally
            {
                _messageHub.Connection.Status -= onStatus;
            }
        }
コード例 #3
0
ファイル: Central.cs プロジェクト: yanshouwang/BGLib.NET
        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);
        }
コード例 #4
0
 public PeripheralEventArgs(Peripheral peripheral)
 {
     Peripheral = peripheral;
 }
コード例 #5
0
ファイル: Central.cs プロジェクト: yanshouwang/BGLib.NET
 public async Task DisconnectAsync(Peripheral peripheral)
 {
     var connection = peripheral.Connection;
     await _messageHub.Connection.DisconnectAsync(connection);
 }