Beispiel #1
0
        private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            // We must update the collection on the UI thread because the collection is databound to a UI element.
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                lock (this)
                {
                    Debug.WriteLine(String.Format("Removed {0}{1}", deviceInfoUpdate.Id, ""));

                    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                    if (sender == deviceWatcher)
                    {
                        // Find the corresponding DeviceInformation in the collection and remove it.
                        BluetoothLEDeviceDisplay bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
                        if (bleDeviceDisplay != null)
                        {
                            KnownDevices.Remove(bleDeviceDisplay);
                        }

                        DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id);
                        if (deviceInfo != null)
                        {
                            UnknownDevices.Remove(deviceInfo);
                        }
                    }
                }
            });
        }
Beispiel #2
0
        private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            // We must update the collection on the UI thread because the collection is databound to a UI element.
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                lock (this)
                {
                    Debug.WriteLine(String.Format("Updated {0}{1}", deviceInfoUpdate.Id, ""));

                    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                    if (sender == deviceWatcher)
                    {
                        BluetoothLEDeviceDisplay bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
                        if (bleDeviceDisplay != null)
                        {
                            // Device is already being displayed - update UX.
                            bleDeviceDisplay.Update(deviceInfoUpdate);
                            return;
                        }

                        DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id);
                        if (deviceInfo != null)
                        {
                            deviceInfo.Update(deviceInfoUpdate);
                            // If device has been updated with a friendly name it's no longer unknown.
                            if (deviceInfo.Name != String.Empty)
                            {
                                KnownDevices.Add(new BluetoothLEDeviceDisplay(deviceInfo));
                                UnknownDevices.Remove(deviceInfo);
                            }
                        }
                    }
                }
            });
        }
Beispiel #3
0
        private async void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            ConnectButton.IsEnabled = false;

            if (!await ClearBluetoothLEDeviceAsync())
            {
                NotifyUser("Error: Unable to reset state, try again.", NotifyType.ErrorMessage);
                ConnectButton.IsEnabled = false;
                return;
            }

            /*if (CurrentBTDeviceInfo != null) // already connected -> disconnect
             * {
             *  return;
             * }*/

            bleDeviceDisplay = ResultsListView.SelectedItem as BluetoothLEDeviceDisplay;

            if (ResultsListView.SelectedItem != null)
            {
                Debug.WriteLine("Test1");
            }
            else
            {
                Debug.WriteLine("Test2");
            }

            if (ResultsListView.SelectedItem is BluetoothLEDeviceDisplay)//DeviceInformation)
            {
                Debug.WriteLine("Test3");
            }
            else
            {
                Debug.WriteLine("Test4");
            }


            if (bleDeviceDisplay != null)
            {
                var SelectedBleDeviceId   = bleDeviceDisplay.Id;
                var SelectedBleDeviceName = bleDeviceDisplay.Name;
                Connect(/*SelectedBleDeviceId*/);
            }

            /*if (ResultsListView.SelectedItem != null &&
             *  ResultsListView.SelectedItem is BluetoothLEDeviceDisplay)//DeviceInformation)
             * {
             *  var dev = ResultsListView.SelectedItem as BluetoothLEDeviceDisplay;//DeviceInformation;
             *  Connect(dev);
             * }*/

            ConnectButton.IsEnabled = true;
        }
Beispiel #4
0
 private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
 {
     // We must update the collection on the UI thread because the collection is databound to a UI element.
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
         // Protect against race condition if the task runs after the app stopped the deviceWatcher.
         if (sender == deviceWatcher)
         {
             BluetoothLEDeviceDisplay bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
             if (bleDeviceDisplay != null)
             {
                 bleDeviceDisplay.Update(deviceInfoUpdate);
             }
         }
     });
 }
Beispiel #5
0
        //This function searches for the actual laser everytime bluetooth device enumeration (device watcher) is updated.
        //Code adapted from microsoft UWP BLE app
        private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            await Task.Run(async() =>
            {
                lock (this)
                {
                    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                    if (sender == deviceWatcher)
                    {
                        BluetoothLEDeviceDisplay bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
                        if (bleDeviceDisplay != null)
                        {
                            // Device is already being displayed - update UX.
                            bleDeviceDisplay.Update(deviceInfoUpdate);
                            DeviceInformation updatedDevice = bleDeviceDisplay.DeviceInformation;
                            //IsConnectable will be established once updated accordingly here. So function needs to be added that handles all devices.
                            if (bleDeviceDisplay.IsConnected && bleDeviceDisplay.Name.Contains("DISTO"))
                            {
                                updateConnectionStatus("CONNECTED");
                            }
                            if (bleDeviceDisplay.IsConnected == false && bleDeviceDisplay.Name.Contains("DISTO"))
                            {
                                //This cna have a bit of a delay on the UI, maybe find out why.
                                updateConnectionStatus("Disconnected");
                            }

                            return;
                        }

                        //Just extra handling, not sure if completely neccessary.
                        DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id);
                        if (deviceInfo != null)
                        {
                            deviceInfo.Update(deviceInfoUpdate);
                            // If device has been updated with a friendly name it's no longer unknown.
                            if (deviceInfo.Name != String.Empty)
                            {
                                KnownDevices.Add(new BluetoothLEDeviceDisplay(deviceInfo));
                                UnknownDevices.Remove(deviceInfo);
                            }
                        }
                    }
                }
            });
        }
        //Any new BT device updated in the observable list this is fired. Taken from microsoft BLE client open source and modified.
        private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            //if contains PIE and PIE is connectable stop all other handlers and connect
            await Task.Run(async() =>
            {
                lock (this)
                {
                    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                    if (sender == deviceWatcher)
                    {
                        BluetoothLEDeviceDisplay bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
                        if (bleDeviceDisplay != null)
                        {
                            // Device is already being displayed, Update infor with most recent data
                            bleDeviceDisplay.Update(deviceInfoUpdate);
                            DeviceInformation updatedDevice = bleDeviceDisplay.DeviceInformation;
                            //IsConnectable will be established once updated accordingly here. So function needs to be added that handles all devices.
                            if (bleDeviceDisplay.IsConnected && bleDeviceDisplay.Name.Contains("PIE"))
                            {
                                updateConnectionStatus("CONNECTED");
                            }
                            if (bleDeviceDisplay.IsConnected == false && bleDeviceDisplay.Name.Contains("PIE"))
                            {
                                updateConnectionStatus("Disconnected");
                            }

                            return;
                        }

                        //left here as residual from orginal microsoft UWP app. Needs this code to properly function.
                        DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id);
                        if (deviceInfo != null)
                        {
                            deviceInfo.Update(deviceInfoUpdate);
                            // If device has been updated with a friendly name it's no longer unknown.
                            if (deviceInfo.Name != String.Empty)
                            {
                                KnownDevices.Add(new BluetoothLEDeviceDisplay(deviceInfo));
                                UnknownDevices.Remove(deviceInfo);
                            }
                        }
                    }
                }
            });
        }
        private async void PairDevice(BluetoothLEDeviceDisplay device)
        {
            // Do not allow a new Pair operation to start if an existing one is in progress.
            if (isBusy)
            {
                return;
            }

            isBusy = true;

            Debug.WriteLine("Pairing started. Please wait...");

            // For more information about device pairing, including examples of
            // customizing the pairing process, see the DeviceEnumerationAndPairing sample.

            // BT_Code: Pair the currently selected device.
            DevicePairingResult result = await device.DeviceInformation.Pairing.PairAsync();

            Debug.WriteLine($"Pairing result = {result.Status}");

            isBusy = false;
        }
Beispiel #8
0
        private async void ProcessManufacturerData(BluetoothLEAdvertisementReceivedEventArgs e, MData m)
        {
            int idx = m.FindManufacturerId(BMRecordBase.MANUFACTURER_ID);

            if (idx < 0)
            {
                return;
            }

            if (!_blueMaestroDevices.ContainsKey(e.BluetoothAddress))
            {
                BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(e.BluetoothAddress);

                lock (_blueMaestroDevices)
                {
                    if (device != null)
                    {
                        _blueMaestroDevices[e.BluetoothAddress] = device;
                        _stopperBM = 0;
                    }
                }
            }

            if (_blueMaestroDevices.ContainsKey(e.BluetoothAddress))
            {
                BluetoothLEDeviceDisplay display = new BluetoothLEDeviceDisplay(_blueMaestroDevices[e.BluetoothAddress].DeviceInformation);
                Log.d("*BT* " + display.ToString());
            }

            if (m.ManufacturerData.Count > 0)
            {
                lock (_blueMaestroDevices)
                {
                    foreach (MData.Section section in m.ManufacturerData)
                    {
                        if (BMRecordCurrent.IsManufacturerID(section.CompanyId))
                        {
                            DateTime        date = DateTime.Now; // eventArgs.Timestamp.DateTime;
                            BluetoothDevice dev  = new BluetoothDevice(
                                e.Advertisement.LocalName,
                                e.BluetoothAddress,
                                e.AdvertisementType.ToString());

                            if (_averages == null)
                            {
                                _averages = new BMRecordAverages(dev, e.RawSignalStrengthInDBm, date, null);
                            }

                            if (_current == null)
                            {
                                _current = new BMRecordCurrent(dev, e.RawSignalStrengthInDBm, date, null);
                            }

                            if (section.Buffer.Length == 14)
                            {
                                if (BMDatabaseMap.INSTANCE.Contains(dev.Address))
                                {
                                    int count = BMDatabaseMap.INSTANCE[dev.Address].Records.Count;
                                    if (count > 0)
                                    {
                                        //time between readings
                                        TimeSpan tsElapsed = DateTime.Now - BMDatabaseMap.INSTANCE[dev.Address].Records.Last().Date;
                                        _lastCurrent = ", Curr Updated: " + tsElapsed.TotalSeconds.ToString("0s");
                                    }
                                }

                                _current = BMDatabaseMap.INSTANCE.AddRecord(dev, e.RawSignalStrengthInDBm, date, section.Buffer);
                            }
                            else if (section.Buffer.Length == 25)
                            {
                                //just update time
                                TimeSpan tsElapsed = DateTime.Now - _averages.Date;
                                _lastAverage = ", Avg Updated: " + tsElapsed.TotalSeconds.ToString("0s");

                                _averages.Set_sData(section.Buffer);
                            }
                            else
                            {
                                Log.e(" --- Unknown Length: " + section.Buffer.Length);
                            }

                            string recordsCount = "";
                            if (BMDatabaseMap.INSTANCE.Contains(dev.Address))
                            {
                                int count = BMDatabaseMap.INSTANCE[dev.Address].Records.Count;
                                recordsCount = "Records: " + count + " \n";
                            }

                            string message = recordsCount;
                            message += "Total: " + CommonTools.TimeSpanToString(TimeSpan.FromSeconds(_stopperBM)) + _lastAverage + _lastCurrent + " \n";
                            message += "Timestamp: " + date.ToString("MMM dd, HH:mm:ss") + " \n";
                            message += _current.ToString() + _averages.ToString();

                            OnBMDeviceMsgReceivedAction(message);
                        }
                    }
                }
            }
        }
 public async Task <DevicePairingResult> Pair(BluetoothLEDeviceDisplay bleDeviceDisplay)
 {
     return(await bleDeviceDisplay.DeviceInformation.Pairing.PairAsync());
 }