public int Compare(object x, object y) { ObservableBluetoothLEDevice a = x as ObservableBluetoothLEDevice; ObservableBluetoothLEDevice b = y as ObservableBluetoothLEDevice; if (a == null || b == null) { throw new InvalidOperationException("Compared objects are not ObservableBluetoothLEDevice"); } // If they're equal if (a.RSSI == b.RSSI) { return(0); } // RSSI == 0 means we don't know it. Always make that the end. if (b.RSSI == 0) { return(-1); } if (a.RSSI < b.RSSI || a.rssi == 0) { return(1); } else { return(-1); } }
/// <summary> /// Update the battery level of a ObservableBluetoothLEDevice by searching through known dev nodes /// </summary> /// <param name="dev">device to update</param> /// <returns></returns> private async Task UpdateBatteryLevel(ObservableBluetoothLEDevice dev) { foreach (DeviceInformation devNode in devNodes) { string addr = dev.BluetoothAddressAsString.Replace(":", String.Empty); if (devNode.Properties.Keys.Contains(BatteryLevelGUID) && devNode.Properties[BatteryLevelGUID] != null && devNode.Properties.Keys.Contains(BluetoothDeviceAddress) && devNode.Properties[BluetoothDeviceAddress] != null) { if ((string)devNode.Properties[BluetoothDeviceAddress] == addr) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { dev.BatteryLevel = Convert.ToInt32((byte)devNode.Properties[BatteryLevelGUID]); }); break; } } } }
/// <summary> /// Adds the new or updated device to the displayed or unused list /// </summary> /// <param name="deviceInfo"></param> /// <returns></returns> private async Task AddDeviceToList(DeviceInformation deviceInfo) { ObservableBluetoothLEDevice dev = new ObservableBluetoothLEDevice(deviceInfo); // Let's make it connectable by default, we have error handles in case it doesn't work bool shouldDisplay = ((dev.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.Bluetooth.Le.IsConnectable") && (bool)dev.DeviceInfo.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"])) || ((dev.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.IsConnected") && (bool)dev.DeviceInfo.Properties["System.Devices.Aep.IsConnected"])); if (shouldDisplay) { // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices try { await BluetoothLEDevicesLock.WaitAsync(); await UpdateBatteryLevel(dev); if ((dev.Name.Length != 0) && (!BluetoothLEDevices.Contains(dev))) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Log.WriteLine("~~~Finding device: " + dev.Name); BluetoothLEDevices.Add(dev); OnPropertyChanged(new PropertyChangedEventArgs("DeviceList")); }); } } finally { BluetoothLEDevicesLock.Release(); } } else { try { await BluetoothLEDevicesLock.WaitAsync(); unusedDevices.Add(deviceInfo); } finally { BluetoothLEDevicesLock.Release(); } } }
private async Task <bool> doStartConnection(string device_id) { bool successfull = false; lastError = ""; //step 1: connect to device try { SelectedDevice = Context.BluetoothLEDevices.FirstOrDefault(item => item.BluetoothAddressAsString == device_id); if (SelectedDevice != null) { IsDeviceConnected = await SelectedDevice.Connect(); successfull = IsDeviceConnected; Log.WriteLine("~~~End of Connection " + successfull); SelectedDevice.PropertyChanged += SelectedDevice_PropertyChanged; } else { IsDeviceConnected = false; successfull = false; } } catch (Exception e) { lastError = "Exception in connection " + e.Message; Log.WriteLine("!!!" + lastError); connectionState = ConnectionState._error; return(false); } if (successfull == false) { lastError = "Failed in connection " + device_id; Log.WriteLine("!!!" + lastError); connectionState = ConnectionState._error; return(false); } lastError = "Successfull connection"; connectionState = ConnectionState._connected; Log.WriteLine("<---" + lastError); return(true); }
private async Task <bool> doStartDisConnection(string device_id) { bool successfull = false; lastError = ""; //step 1: connect to device try { if (SelectedDevice != null) { SelectedDevice.PropertyChanged -= SelectedDevice_PropertyChanged; _disconnectionCallback = null; successfull = await SelectedDevice.Disconnect(); IsDeviceConnected = !successfull; } } catch (Exception e) { lastError = "Exception in disconnection " + e.Message; Log.WriteLine("!!!" + lastError); connectionState = ConnectionState._error; return(false); } if (successfull == false) { lastError = "Failed in disconnection " + device_id; Log.WriteLine("!!!" + lastError); connectionState = ConnectionState._error; return(false); } lastError = "Successfull disconnection"; SelectedDevice = null; connectionState = ConnectionState._connected; Log.WriteLine("<---" + lastError); return(true); }