/// <summary> /// Populates the device list and initializes all the various models. /// /// Waiting for the async calls to complete takes a while, so we want to call this /// function somewhat sparingly. /// </summary> /// <returns></returns> public static async Task PopulateDeviceListAsync() { // Remove all devices and start from scratch PairedDevices.Clear(); // Asynchronously get all paired/connected bluetooth devices. var infoCollection = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()); // Re-add devices foreach (DeviceInformation info in infoCollection) { // Make sure we don't initialize duplicates if (PairedDevices.FindIndex(device => device.DeviceId == info.Id) >= 0) { continue; } BluetoothLEDevice WRTDevice = await BluetoothLEDevice.FromIdAsync(info.Id); BEDeviceModel deviceM = new BEDeviceModel(); deviceM.Initialize(WRTDevice, info); PairedDevices.Add(deviceM); } /* * FUTURE * * Consider reading one characteristic from each device uncached to trigger a connection to the * device, in case the device does not have notifiable characteristics. * * Also consider registering for DeviceConnectionChangeTrigger, in case a device does not have * notifiable characteristics. But that may be overkill - what's the likelihood that a device * won't have notifiable characteristics? * */ }
/// <summary> /// Populates the device list and initializes all the various models. /// Waiting for the async calls to complete takes a while, so we want to call this /// function somewhat sparingly. /// </summary> /// <returns></returns> public async Task PopulateDeviceListAsync() { Debug.WriteLine("PopulateDeviceListAsync Started..."); Utilities.RunFuncAsTask(async() => { try { if (IsBusy) { return; } ScanningProcess(this, true); IsBusy = true; // Remove all devices and start from scratch PairedDevices.Clear(); // Asynchronously get all paired bluetooth devices. var infoCollection = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()); // Re-add devices foreach (var info in infoCollection) { // Make sure we don't initialize duplicates if (PairedDevices.FindIndex(device => device.DeviceId == info.Id) >= 0) { continue; } var WRTDevice = await BluetoothLEDevice.FromIdAsync(info.Id); var deviceM = new BEDeviceModel(); deviceM.Initialize(WRTDevice, info); if (deviceM.Name.ToLower() == HaccpConstant.Blue2DeviceName) { if (deviceM.Connected) { PairedDevices.Add(deviceM); } } } Debug.WriteLine("PairedDevices Count...{0}", PairedDevices.Count); PairedDeviceStatusUpdated(this, new DeviceStatusUpdated(PairedDevices.Any())); if (PairedDevices.Any()) { SelectedDevice = PairedDevices[0]; SelectedDevice.DeviceConnectionStatusChanged -= SelectedDevice_DeviceConnectionStatusChanged; SelectedDevice.DeviceConnectionStatusChanged += SelectedDevice_DeviceConnectionStatusChanged; ReadCharacteristics(); } else { IsBusy = false; Debug.WriteLine("No device in the list"); } } catch (Exception ex) { Debug.WriteLine("Error PopulateDeviceListAsync : {0}", ex.Message); IsBusy = false; ScanningProcess(this, false); } }); }
public void Initialize(BEDeviceModel deviceM) { Model = deviceM; DeviceM = deviceM; DeviceM.Register(this); }