/// <summary> /// Called when information on a discovered BLE device has changed /// </summary> /// <param name="sender">Device watcher which discovered the device</param> /// <param name="device_info">BLE device information</param> private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate device_info) { // Retrieve device information BleDeviceInformation ble_device_info = new BleDeviceInformation(); ble_device_info.Id = device_info.Id; try { ble_device_info.MacAddress = device_info.Properties["System.Devices.Aep.DeviceAddress"].ToString(); } catch (KeyNotFoundException) { ble_device_info.MacAddress = ""; } try { ble_device_info.IsConnected = (bool)device_info.Properties["System.Devices.Aep.IsConnected"]; } catch (KeyNotFoundException) { ble_device_info.IsConnected = false; } try { ble_device_info.IsConnectable = (bool)device_info.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"]; } catch (KeyNotFoundException) { ble_device_info.IsConnectable = false; } // Notify device update DeviceUpdated?.Invoke(this, ble_device_info); }
/// <summary> /// Constructor /// </summary> /// <param name="device_info"></param> public UwpBleDevice(BleDeviceInformation ble_device_info) { // Save device information _ble_device_info = ble_device_info; // Initialize listener list _ble_notification_listeners = new Dictionary <BleGattCharacteristic, Action <BleGattCharacteristic, BleValue> >(100); _ble_characteristics = new Dictionary <GattCharacteristic, BleGattCharacteristic>(100); }
/// <summary> /// Called when a discovered BLE device is no longer reachable /// </summary> /// <param name="sender">Device watcher which discovered the device</param> /// <param name="device_info">BLE device information</param> private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate device_info) { // Retrieve device information BleDeviceInformation ble_device_info = new BleDeviceInformation(); ble_device_info.Id = device_info.Id; // Notify device removal DeviceLost?.Invoke(this, ble_device_info); }
/// <summary> /// Create a BLE device proxy object from a BLE device information /// </summary> /// <param name="device_info">BLE device information to use</param> /// <returns>BLE device proxy object on success, null otherwise</returns> public async Task <IBleDevice> CreateDeviceAsync(BleDeviceInformation device_info) { IBleDevice ble_device = null; UwpBleDevice win_ble_device = new UwpBleDevice(device_info); bool init_succeed = await win_ble_device.Initialize(); if (init_succeed) { ble_device = win_ble_device; } return(ble_device); }