Esempio n. 1
0
        private async void OnDeviceAdded(DeviceWatcher watcher, DeviceInformation deviceInfoAdded)
        {
            // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
            await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                DeviceInformationDisplay deviceInfoDisplay = new DeviceInformationDisplay(deviceInfoAdded);

                if (!ResultCollection.Any(p => p.Name == deviceInfoAdded.Name))
                {
                    ResultCollection.Add(deviceInfoDisplay);
                    Debug.WriteLine("{0} devices found.", ResultCollection.Count);
                }

                CheckPairing(deviceInfoDisplay);
            });
        }
Esempio n. 2
0
        private async void PostBeaconData(DeviceInformationDisplay deviceInfoDisplay)
        {
            if (deviceInfoDisplay.Name.StartsWith("XY"))
            {
                if (null != _devClient)
                {
                    try
                    {
                        string  jsonText = deviceInfoDisplay.ToJson();
                        Message msg      = new Message(Encoding.UTF8.GetBytes(jsonText));
                        await _devClient.SendEventAsync(msg);

                        Debug.WriteLine("Message Sent: {0}", jsonText);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Exception when sending message:" + ex.Message);
                    }
                }
            }
        }
Esempio n. 3
0
        private async void CheckPairing(DeviceInformationDisplay deviceInfoDisplay)
        {
            Debug.WriteLine("{0}- '{1}' Signal Strength - {2}", DateTime.Now.Ticks, deviceInfoDisplay.Name, deviceInfoDisplay.SignalStrength);

            if (!deviceInfoDisplay.IsPairing)
            {
                // Pair device only if it is close
                if (deviceInfoDisplay.Name.StartsWith("XY") &&
                    !deviceInfoDisplay.IsPaired &&
                    (deviceInfoDisplay.SignalStrength > -50))
                {
                    // Set a flag on the device so that once it begins to pair, it doesn't constantly try to pair
                    deviceInfoDisplay.IsPairing = true;

                    // This is just basic pairing (No PIN or confirmation)
                    DevicePairingKinds             ceremonies      = DevicePairingKinds.ConfirmOnly;
                    DevicePairingProtectionLevel   protectionLevel = DevicePairingProtectionLevel.Default;
                    DeviceInformationCustomPairing customPairing   = deviceInfoDisplay.DeviceInformation.Pairing.Custom;

                    // In the cases where more complex pairing is required, user interaction happens in the Pairing Requested event
                    customPairing.PairingRequested += OnPairingRequested;
                    DevicePairingResult result = await customPairing.PairAsync(ceremonies, protectionLevel);

                    customPairing.PairingRequested -= OnPairingRequested;

                    Debug.WriteLine("{0} pair result - {1}", deviceInfoDisplay.Name, result.Status);

                    // If the pair was successful, act based on the beacon ID
                    if (DevicePairingResultStatus.Paired == result.Status)
                    {
                        var stream = await _speechSynth.SynthesizeTextToStreamAsync("Paired beacon ID " + deviceInfoDisplay.Name);

                        // Use a dispatcher to play the audio
                        var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            //Set the souce of the MediaElement to the SpeechSynthesisStream
                            _audio.SetSource(stream, stream.ContentType);

                            //Play the stream
                            _audio.Play();
                        });
                    }

                    deviceInfoDisplay.IsPairing = false;
                }
                else if (deviceInfoDisplay.Name.StartsWith("XY") &&
                         deviceInfoDisplay.IsPaired &&
                         (deviceInfoDisplay.SignalStrength < -50))
                {
                    // Set a flag on the device so that once it begins to pair, it doesn't constantly try to unpair
                    deviceInfoDisplay.IsPairing = true;

                    // Unpair the beacon
                    DeviceUnpairingResult result = await deviceInfoDisplay.DeviceInformation.Pairing.UnpairAsync();

                    Debug.WriteLine("{0} unpair result - {1}", deviceInfoDisplay.Name, result.Status);

                    deviceInfoDisplay.IsPairing = false;
                }
            }
        }