/// <summary> /// Called when a new ble device is discovered. /// </summary> /// <param name="sender"></param> /// <param name="deviceInfo"></param> private void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo) { lock (this) { //Making sure that we are processing device from the current device watcher. if (sender == deviceWatcher) { //Making sure device isn't already present in the list. if (GetDiscoveredLogitowDevice(deviceInfo.Id) == null) { //Checking if the device has info or not. if (System.String.IsNullOrEmpty(deviceInfo.Name)) { unknownDevices.Add(deviceInfo); } else if (deviceInfo.Name == "LOGITOW" && (bool)deviceInfo.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"] == true) { Console.WriteLine(System.String.Format("Discovered LOGITOW device: {0}", deviceInfo.Id)); //If device has name LOGITOW, adding it to list. var logitowDevice = new LogitowDevice(deviceInfo); discoveredDevices.Add(logitowDevice); eventListener.OnDeviceDiscovered(logitowDevice.deviceInfo.Id); } } } } }
/// <summary> /// Connects or reconnects to a logitow brick. /// </summary> public void ConnectOrReconnect(LogitowDevice device) { foreach (LogitowDevice connected in LogitowDevice.connectedDevices) { if (connected.deviceInfo.Id == device.deviceInfo.Id) { Console.WriteLine("Reconnecting..."); device.Disconnect(); } } device.ConnectAsync(); }
/// <summary> /// Called when a device info is updated. /// </summary> /// <param name="sender"></param> /// <param name="deviceInfoUpdate"></param> private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { lock (this) { Console.WriteLine(System.String.Format("Updated {0}, {1}", deviceInfoUpdate.Id, "")); //Making sure that we are processing device from the current device watcher. if (sender == deviceWatcher) { LogitowDevice logitowDevice = GetDiscoveredLogitowDevice(deviceInfoUpdate.Id); if (logitowDevice != null) { //Device has already been discovered. Updating the info. logitowDevice.OnBluetoothInfoUpdate(deviceInfoUpdate); return; } else { //Checking in the unknown devices list. DeviceInformation information = GetUnknownDevice(deviceInfoUpdate.Id); //Updating the device info. if (information != null) { information.Update(deviceInfoUpdate); //Checking if the name has been fetched and if it is LOGITOW. if (!System.String.IsNullOrEmpty(information.Name) && information.Name == "LOGITOW" && (bool)information.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"] == true) { unknownDevices.Remove(information); LogitowDevice device = new LogitowDevice(information); Console.WriteLine(System.String.Format("Added {0} to the discovered LOGITOW bricks list.", information.Id)); discoveredDevices.Add(logitowDevice); eventListener.OnDeviceDiscovered(device.deviceInfo.Id); } } } } } }
/// <summary> /// Called when a BLE device is removed from the system. /// </summary> /// <param name="sender"></param> /// <param name="deviceInfoUpdate"></param> private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { lock (this) { Console.WriteLine(System.String.Format("Removed {0}, {1}", deviceInfoUpdate.Id, "")); //Making sure that we are processing device from the current device watcher. if (sender == deviceWatcher) { //Find the corresponding DeviceInformation in the collection and remove it. LogitowDevice logitowDevice = GetDiscoveredLogitowDevice(deviceInfoUpdate.Id); if (logitowDevice != null) { discoveredDevices.Remove(logitowDevice); eventListener.OnDeviceLost(logitowDevice.deviceInfo.Id); } DeviceInformation unknownDevice = GetUnknownDevice(deviceInfoUpdate.Id); if (unknownDevice != null) { unknownDevices.Remove(unknownDevice); } } } }
/// <summary> /// Called when a connection error occurs with a device. /// </summary> /// <param name="device"></param> internal void OnDeviceDisconnected(LogitowDevice device) { //Restarting the scanner. StopBleDeviceWatcher(); StartBleDeviceWatcher(); }
/// <summary> /// Disconnects the specified device. /// </summary> /// <param name="device"></param> public void Disconnect(LogitowDevice device) { device.DisconnectAsync(); }
/// <summary> /// Connects or reconnects to a logitow brick. /// </summary> public void Connect(LogitowDevice device) { device.ConnectAsync(); }