Exemple #1
0
        //Watcher for Bluetooth LE Devices based on the Protocol ID
        private async void StartWatcher()
        {
            string aqsFilter;

            numDevicesFound = 0;

            var bTSupported = await BT.GetBluetoothIsSupportedAsync();

            if (!bTSupported)
            {
                Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    UserOut.Text = "No Bluetooth radios available";
                    System.Diagnostics.Debug.WriteLine("No Bluetooth radios available!");
                });
                return;
            }


            var bTIsEnabled = await BT.GetBluetoothIsEnabledAsync();

            if (!bTIsEnabled)
            {
                bool bAccess = true;
                Dispatcher.RunAsync(CoreDispatcherPriority.Low, async() =>
                {
                    var access = await BT.RequestAccessAsync();
                    bAccess    = access;
                });



                if (bAccess)
                {
                    bTIsEnabled = await BT.SetBluetoothStateOnAsync();

                    if (!bTIsEnabled)
                    {
                        Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            UserOut.Text = "Bluetooth isn't enabled! Turn on Failed";
                            System.Diagnostics.Debug.WriteLine("Bluetooth isn't enabled! Turn on Failed.");
                        });
                        return;
                    }
                }
            }

            //If sensors are paired we can skip straight to getting GATT services
            //Assumes only one sensor paired
            //Seeingthat we are already paired cut straight to the chase
            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                UserOut.Text = "Setting up SensorTag";
            });
            iSetUpProgress = 0;
            SetUpProgress();
            CC2650SensorTag.SetUpProgress   = SetUpProgress;
            CC2650SensorTag.SetBatteryLevel = SetBatteryLevel;
            CC2650SensorTag_BLEWatcher.StartBLEWatcher(this, SetDeviceInfo, SetUpProgress2);
            return;


            // Request the IsPaired property so we can display the paired status in the UI
            string[] requestedProperties = { "System.Devices.Aep.IsPaired" };

            //for bluetooth LE Devices
            aqsFilter = "System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\"";

            deviceWatcher = DeviceInformation.CreateWatcher(
                aqsFilter,
                requestedProperties,
                DeviceInformationKind.AssociationEndpoint
                );

            // Hook up handlers for the watcher events before starting the watcher

            handlerAdded = async(watcher, deviceInfo) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    if (numDevicesFound < numDevices)
                    {
                        //Depending upon firware there are two names.
                        if (CC2650SensorTag.DeviceAltSensorNames.Contains(deviceInfo.Name))
                        {
                            Debug.WriteLine("Watcher Add: " + deviceInfo.Id);
                            ResultCollection.Add(new DeviceInformationDisplay(deviceInfo));


                            UpdatePairingButtons();
                            //resultsListView.IsEnabled = true;
                            UserOut.Text = "Found at least one SensorTag. " + " Select for pairing. Still searching for others though.";
                            var st       = watcher.Status;
                            numDevicesFound++;
                            if (numDevicesFound == numDevices)
                            {
                                if (watcher != null)
                                {
                                    if (st != DeviceWatcherStatus.Stopped)
                                    {
                                        watcher.Stop();
                                    }
                                }
                            }
                        }
                    }
                });
            };
            deviceWatcher.Added += handlerAdded;

            handlerUpdated = async(watcher, deviceInfoUpdate) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    //Debug.WriteLine("Watcher Update: " + deviceInfoUpdate.Id);
                    // Find the corresponding updated DeviceInformation in the collection and pass the update object
                    // to the Update method of the existing DeviceInformation. This automatically updates the object
                    // for us.
                    foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection)
                    {
                        if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                        {
                            deviceInfoDisp.Update(deviceInfoUpdate);
                            break;
                        }
                    }
                });
            };
            deviceWatcher.Updated += handlerUpdated;



            handlerRemoved = async(watcher, deviceInfoUpdate) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    Debug.WriteLine("Watcher Remove: " + deviceInfoUpdate.Id);
                    // Find the corresponding DeviceInformation in the collection and remove it
                    foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection)
                    {
                        if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                        {
                            ResultCollection.Remove(deviceInfoDisp);
                            UpdatePairingButtons();
                            if (ResultCollection.Count == 0)
                            {
                                UserOut.Text = "Searching for Bluetooth LE Devices...";
                            }
                            break;
                        }
                    }
                });
            };
            deviceWatcher.Removed += handlerRemoved;

            handlerStopped = async(watcher, obj) =>
            {
                Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    Debug.WriteLine($"Watcher Stopped: Found {ResultCollection.Count} Bluetooth LE Devices");

                    if (ResultCollection.Count > 0)
                    {
                        UserOut.Text = "Select a device for pairing";
                    }
                    else
                    {
                        UserOut.Text = "No Bluetooth LE Devices found.";
                    }
                    UpdatePairingButtons();
                });
            };

            handlerEnumCompleted = async(watcher, obj) =>
            {
                Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    Debug.WriteLine($"Found {ResultCollection.Count} Bluetooth LE Devices");

                    if (ResultCollection.Count > 0)
                    {
                        UserOut.Text = "Select a " + CC2650SensorTag.DeviceAltSensorNames + " for pairing. Search done.";
                    }
                    else
                    {
                        UserOut.Text = "Search done: No Bluetooth LE Devices found.";
                    }
                    UpdatePairingButtons();
                });
            };

            deviceWatcher.EnumerationCompleted += handlerEnumCompleted;

            deviceWatcher.Start();
        }