public void Stop()
 {
     if (_query != null)
     {
         _query = null;
     }
     if (_onboardingProducer != null)
     {
         _onboardingProducer.Stop();
         _onboardingProducer = null;
     }
     if (_iconProducer != null)
     {
         _iconProducer.Stop();
         _iconProducer = null;
     }
     if (_busAttachment != null)
     {
         _busAttachment = null;
     }
     if (_softAccessPoint != null)
     {
         _softAccessPoint.Stop();
         _softAccessPoint = null;
     }
     if (_deviceWatcher != null)
     {
         _deviceWatcher.Stop();
         _deviceWatcher = null;
     }
     _wlanAdapterId = null;
     _state         = OnboardingState.NotConfigured;
 }
        public async void Start()
        {
            try
            {
                _iconFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IoTOnboardingService/icon72x72.png"));

                var connectionProfiles = NetworkInformation.GetConnectionProfiles();
                foreach (var profile in connectionProfiles)
                {
                    if (profile.IsWlanConnectionProfile)
                    {
                        lock (_stateLock)
                        {
                            _state = OnboardingState.ConfiguredValidated;
                        }
                        break;
                    }
                }

                if (_softAccessPoint == null)
                {
                    _softAccessPoint = new OnboardingAccessPoint(string.Format(_resourceLoader.GetString("SoftApSsidTemplate"), _onboardingInstanceId), _resourceLoader.GetString("SoftApPassword"));
                }

                if (_busAttachment == null)
                {
                    _busAttachment = new AllJoynBusAttachment();
                    _busAttachment.AboutData.DefaultDescription  = string.Format(_resourceLoader.GetString("DefaultDescriptionTemplate"), _onboardingInstanceId);
                    _busAttachment.AboutData.DefaultManufacturer = _resourceLoader.GetString("DefaultManufacturer");
                    _busAttachment.AboutData.ModelNumber         = _resourceLoader.GetString("ModelNumber");

                    _onboardingProducer         = new OnboardingProducer(_busAttachment);
                    _onboardingProducer.Service = this;

                    _iconProducer         = new IconProducer(_busAttachment);
                    _iconProducer.Service = this;
                }

                if (_deviceWatcher == null)
                {
                    var accessStatus = await WiFiAdapter.RequestAccessAsync();

                    if (accessStatus == WiFiAccessStatus.Allowed)
                    {
                        _deviceWatcher          = DeviceInformation.CreateWatcher(WiFiAdapter.GetDeviceSelector());
                        _deviceWatcher.Added   += this.HandleAdapterAdded;
                        _deviceWatcher.Removed += this.HandleAdapterRemoved;

                        _deviceWatcher.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
        public async void Start()
        {
            try
            {
                _iconFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IoTOnboardingService/icon72x72.png"));

                await _config.InitAsync();

                var connectionProfiles = NetworkInformation.GetConnectionProfiles();
                foreach (var profile in connectionProfiles)
                {
                    if (profile.IsWlanConnectionProfile)
                    {
                        lock (_stateLock)
                        {
                            _state = OnboardingState.ConfiguredValidated;
                        }
                        break;
                    }
                }

                if (_softAccessPoint == null)
                {
                    _softAccessPoint = new OnboardingAccessPoint(_config.Ssid, _config.Password);
                }

                if (_busAttachment == null)
                {
                    _busAttachment = new AllJoynBusAttachment();
                    _busAttachment.AboutData.DefaultDescription = _config.DefaultDescription;
                    _busAttachment.AboutData.DefaultManufacturer = _config.DefaultManufacturer;

                    _onboardingProducer = new OnboardingProducer(_busAttachment);
                    _onboardingProducer.Service = this;

                    _iconProducer = new IconProducer(_busAttachment);
                    _iconProducer.Service = this;
                }

                if (_deviceWatcher == null)
                {
                    var accessStatus = await WiFiAdapter.RequestAccessAsync();
                    if (accessStatus == WiFiAccessStatus.Allowed)
                    {
                        _deviceWatcher = DeviceInformation.CreateWatcher(WiFiAdapter.GetDeviceSelector());
                        _deviceWatcher.Added += this.HandleAdapterAdded;
                        _deviceWatcher.Removed += this.HandleAdapterRemoved;

                        _deviceWatcher.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
        public async void Start()
        {
            try
            {
                _iconFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IoTOnboardingService/icon72x72.png"));

                var connectionProfiles = NetworkInformation.GetConnectionProfiles();
                foreach (var profile in connectionProfiles)
                {
                    if (profile.IsWlanConnectionProfile)
                    {
                        lock (_stateLock)
                        {
                            _state = OnboardingState.ConfiguredValidated;
                        }
                        break;
                    }
                }

                // read configuration
                var configResult = await ReadConfig();

                if (!configResult.Item1)
                {
                    // for some reason config file doesn't seem to be OK
                    // => reset config and try another time
                    await ResetConfig();

                    configResult = await ReadConfig();

                    if (!configResult.Item1)
                    {
                        throw new System.IO.InvalidDataException("Invalid configuration");
                    }
                }
                _ajOnboardingConfig = configResult.Item2;
                _softAPConfig       = configResult.Item3;

                bool monitorOk = await MonitorConfigFile();

                if (!monitorOk)
                {
                    throw new System.Exception("Unable to monitor configuration file for changes.  Unexpected.");
                }


                // If everything is disabled, then there's nothing to do.
                if ((_softAPConfig.enabled == false) && (_ajOnboardingConfig.enabled == false))
                {
                    return;
                }

                // create softAP
                if (_softAccessPoint == null &&
                    (_softAPConfig.enabled ||
                     _ajOnboardingConfig.enabled))
                {
                    // The following builds up an Access Point SSID from information available to the IotOnboarding Task
                    // If AllJoyn is enabled, then we must add AJ_ to the start of the SSID
                    // If a Soft AP SSID value is specified in the configuration settings, then we will add that next
                    // Finally we add the Soft AP's MAC Address to the end.  Note that if a device does not have a WiFi adapter,
                    // this _mac value will be a forced to the onboardingDevice ID GUID created the first time this app runs
                    //
                    // Examples:  AllJoyn enabled, Wifi:        AJ_SoftAPSsid_<MACADDRESS>
                    //            AllJoyn disabled, Wifi:       SoftAPSsid_<MACADDRESS>
                    //            AllJoyn disabled, No Wifi:    SoftAPSsid_<8Chars_of_Onboarding_GUID>
                    //
                    //

                    string prefix = "";
                    string suffix = "_" + _mac;
                    string ssid   = "";

                    if (_ajOnboardingConfig.enabled)
                    {
                        prefix = SOFTAP_SSID_AJONBOARDING_PREFIX;
                    }

                    // SSID examples:  AJ_SoftAPSsid_AABBCCDDEEFF
                    ssid             = prefix + _softAPConfig.ssid + suffix;
                    _softAccessPoint = new OnboardingAccessPoint(ssid, _softAPConfig.password, _onboardingDeviceId);
                }

                // create AllJoyn related things
                if (_busAttachment == null &&
                    _ajOnboardingConfig.enabled)
                {
                    _busAttachment = new AllJoynBusAttachment();
                    _busAttachment.AboutData.DefaultDescription  = _ajOnboardingConfig.defaultDescription + " MAC: " + _mac;
                    _busAttachment.AboutData.DefaultManufacturer = _ajOnboardingConfig.defaultManufacturer;
                    _busAttachment.AboutData.ModelNumber         = _ajOnboardingConfig.modelNumber;
                    _onboardingProducer         = new OnboardingProducer(_busAttachment);
                    _onboardingProducer.Service = this;
                    _busAttachment.AuthenticationMechanisms.Clear();

                    if (_ajOnboardingConfig.presharedKey.Length == 0)
                    {
                        _busAttachment.AuthenticationMechanisms.Add(AllJoynAuthenticationMechanism.EcdheNull);
                    }
                    else
                    {
                        _busAttachment.AuthenticationMechanisms.Add(AllJoynAuthenticationMechanism.EcdhePsk);
                    }

                    _busAttachment.CredentialsRequested             += CredentialsRequested;
                    _busAttachment.CredentialsVerificationRequested += CredentialsVerificationRequested;
                    _busAttachment.AuthenticationComplete           += AuthenticationComplete;

                    _iconProducer         = new IconProducer(_busAttachment);
                    _iconProducer.Service = this;
                }

                if (_deviceWatcher == null)
                {
                    var accessStatus = await WiFiAdapter.RequestAccessAsync();

                    if (accessStatus == WiFiAccessStatus.Allowed)
                    {
                        _deviceWatcher          = DeviceInformation.CreateWatcher(WiFiAdapter.GetDeviceSelector());
                        _deviceWatcher.Added   += this.HandleAdapterAdded;
                        _deviceWatcher.Removed += this.HandleAdapterRemoved;

                        _deviceWatcher.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }