Exemplo n.º 1
0
        public override void OnConnect()
        {
            Console.WriteLine("Device Connected!");

            Windesheart.PairedDevice = this;

            //Check if bluetooth-state changes to off and then on, to enable reconnection management
            BluetoothService.StartListeningForAdapterChanges();

            Characteristics?.Clear();

            CharacteristicDisposable?.Dispose();
            //Find unique characteristics
            CharacteristicDisposable = IDevice.WhenAnyCharacteristicDiscovered().Subscribe(async characteristic =>
            {
                if (characteristic != null && !Characteristics.Contains(characteristic))
                {
                    Characteristics.Add(characteristic);

                    //Check if authCharacteristic has been found, then authenticate
                    if (characteristic.Uuid == MiBand3Resource.GuidCharacteristicAuth)
                    {
                        //Check if this is a new connection that needs authentication
                        await _authenticationService.Authenticate();
                    }
                }
            });
        }
        /// <summary>
        /// Gets all the characteristics of this service
        /// </summary>
        private async void GetAllCharacteristics()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("ObservableGattDeviceService::getAllCharacteristics: ");
            sb.Append(Name);



            try
            {
                // Request the necessary access permissions for the service and abort
                // if permissions are denied.
                GattOpenStatus status = await Service.OpenAsync(GattSharingMode.SharedReadAndWrite);

                if (status != GattOpenStatus.Success && status != GattOpenStatus.AlreadyOpened)
                {
                    string error = " - Error: " + status.ToString();
                    Name += error;
                    sb.Append(error);
                    Debug.WriteLine(sb.ToString());

                    return;
                }

                GattCharacteristicsResult result = await Service.GetCharacteristicsAsync(Services.SettingsServices.SettingsService.Instance.UseCaching?BluetoothCacheMode.Cached : BluetoothCacheMode.Uncached);

                if (result.Status == GattCommunicationStatus.Success)
                {
                    sb.Append(" - getAllCharacteristics found ");
                    sb.Append(result.Characteristics.Count());
                    sb.Append(" characteristics");
                    Debug.WriteLine(sb);
                    foreach (GattCharacteristic gattchar in result.Characteristics)
                    {
                        if (gattchar.Uuid.ToString() == "49535343-1e4d-4bd9-ba61-23c647249616")
                        {
                            Characteristics.Clear();

                            Characteristics.Add(new ObservableGattCharacteristics(gattchar, this));
                        }
                    }
                }
                else if (result.Status == GattCommunicationStatus.Unreachable)
                {
                    sb.Append(" - getAllCharacteristics failed with Unreachable");
                    Debug.WriteLine(sb.ToString());
                }
                else if (result.Status == GattCommunicationStatus.ProtocolError)
                {
                    sb.Append(" - getAllCharacteristics failed with Unreachable");
                    Debug.WriteLine(sb.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("getAllCharacteristics: Exception - {0}" + ex.Message);
                throw;
            }
        }
        public void ApplyFrom(IPresetMetadata presetMetadata)
        {
            if (!UserOverwrittenProperties.Contains(nameof(Author)))
            {
                Author = presetMetadata.Author;
            }

            if (!UserOverwrittenProperties.Contains(nameof(Comment)))
            {
                Comment = presetMetadata.Comment;
            }

            if (!UserOverwrittenProperties.Contains(nameof(PresetName)))
            {
                PresetName = presetMetadata.PresetName;
            }

            if (!UserOverwrittenProperties.Contains(nameof(BankPath)))
            {
                BankPath = presetMetadata.BankPath;
            }

            if (!UserOverwrittenProperties.Contains(nameof(Types)))
            {
                using (Types.SuspendChangeNotifications(SuspensionMode.None))
                {
                    Types.Clear();

                    foreach (var type in presetMetadata.Types)
                    {
                        if (!type.IsIgnored)
                        {
                            Types.Add(new Type {
                                TypeName = type.TypeName, SubTypeName = type.SubTypeName
                            });
                        }
                    }
                }
            }

            if (!UserOverwrittenProperties.Contains(nameof(Characteristics)))
            {
                using (Characteristics.SuspendChangeNotifications(SuspensionMode.None))
                {
                    Characteristics.Clear();

                    foreach (var characteristic in presetMetadata.Characteristics)
                    {
                        if (!characteristic.IsIgnored)
                        {
                            Characteristics.Add(new Characteristic
                            {
                                CharacteristicName = characteristic.CharacteristicName
                            });
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Discovers the characteristics for the services.
 /// </summary>
 public void DiscoverCharacteristics()
 {
     Characteristics.Clear();
     // do nothing
     foreach (var c in NativeService.Characteristics)
     {
         var characteristic = new Characteristic(c, _gatt, _callback);
         Characteristics.Add(characteristic);
     }
     CharacteristicDiscovered(this, new CharacteristicsDiscoveredEventArgs(Characteristics));
 }
Exemplo n.º 5
0
        public void OnAfterCerasDeserialize()
        {
            using (Types.SuspendChangeNotifications())
            {
                Types.Clear();
                Types.AddItems(SerializedTypes);
            }

            using (Characteristics.SuspendChangeNotifications())
            {
                Characteristics.Clear();
                Characteristics.AddItems(SerializedCharacteristics);
            }
        }