public async void Run(Windows.ApplicationModel.Background.IBackgroundTaskInstance taskInstance) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); try { DeviceConnectionChangeTriggerDetails details = (DeviceConnectionChangeTriggerDetails)taskInstance.TriggerDetails; BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(details.DeviceId); SmartPack device = new SmartPack(bleDevice); if (bleDevice.ConnectionStatus == BluetoothConnectionStatus.Connected) { if (device.AlertOnDevice && device.HasLinkLossService) { await device.SetAlertLevelCharacteristic(); } } else { if (device.AlertOnPhone) { XmlDocument xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); xml.SelectSingleNode("/toast/visual/binding/text").InnerText = string.Format("Device {0} is out of range.", device.Name); ToastNotification toast = new ToastNotification(xml); ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier(); notifier.Show(toast); } } } finally { deferral.Complete(); } }
// Constructor public Racer(BluetoothLEDevice device) { XValue = 128; YValue = 128; ZValue = 255; BValue = 0; this._device = device; _addressString = device.BluetoothAddress.ToString("x012"); try { linkLossService = device.GetGattService(GattServiceUuids.LinkLoss); } catch (Exception) { // e.HResult == 0x80070490 means that the device doesn't have the requested service. // We can still alert on the phone upon disconnection, but cannot ask the device to alert. // linkLossServer will remain equal to null. } if (localSettings.Values.ContainsKey(_addressString)) { string[] values = ((string)localSettings.Values[_addressString]).Split(','); alertOnPhone = bool.Parse(values[0]); alertOnDevice = bool.Parse(values[1]); alertLevel = (AlertLevel)Enum.Parse(typeof(AlertLevel), values[2]); } }
private void NativeDevice_ConnectionStatusChanged(Windows.Devices.Bluetooth.BluetoothLEDevice sender, object args) { if (sender.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Disconnected) { Device.OnGattServerDisconnected(); } }
internal async void Start() { bleDevice = await Windows.Devices.Bluetooth.BluetoothLEDevice.FromIdAsync(deviceId); services = bleDevice.GattServices; int ccc = services.Count; for (int i = 0; i < ccc; i++) { Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceService service = services[i]; string s = service.Uuid.ToString(); if (s == "4f63756c-7573-2054-6872-65656d6f7465") { myService = service; //var CharResult = myService.GetAllCharacteristics(); //Console.WriteLine("elo"); break; } } if (myService != null) { Get_Characteriisics(); } }
public async Task Init(Windows.Devices.Bluetooth.BluetoothLEDevice ble) { DeviceName = ble.Name; var services = await ble.GetGattServicesAsync(); foreach (var service in services.Services) { } }
private void Device_ConnectionStatusChanged(Windows.Devices.Bluetooth.BluetoothLEDevice sender, object args) { bool status = false; switch (sender.ConnectionStatus) { case Windows.Devices.Bluetooth.BluetoothConnectionStatus.Disconnected: status = false; break; case Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected: status = true; break; } OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(status)); }
/// <summary> /// Invoked when this page is about to be displayed in a Frame. /// </summary> /// <param name="e">Event data that describes how this page was reached. /// This parameter is typically used to configure the page.</param> protected override async void OnNavigatedTo(NavigationEventArgs e) { // Request the right to have background tasks run in the future. This need only be done once // after the app is installed, but it is harmless to do it every time the app is launched. if (await BackgroundExecutiondManager.RequestAccessAsync() == BackgroundAccessStatus.Denied) { // TODO: What? } // Acquire the set of background tasks that we already have registered. Store them into a dictionary, keyed // by task name. (For each LE device, we will use a task name that is derived from its Bluetooth address). Dictionary <string, BackgroundTaskRegistration> taskRegistrations = new Dictionary <string, BackgroundTaskRegistration>(); foreach (BackgroundTaskRegistration reg in BackgroundTaskRegistration.AllTasks.Values) { taskRegistrations[reg.Name] = reg; } // Get the list of paired Bluetooth LE devicdes, and add them to our 'devices' list. Associate each device with // its pre-existing registration if any, and remove that registration from our dictionary. Devices.Clear(); foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector())) { BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id); Racer device = new Racer(bleDevice); if (taskRegistrations.ContainsKey(device.TaskName)) { device.TaskRegistration = taskRegistrations[device.TaskName]; taskRegistrations.Remove(device.TaskName); } Devices.Add(device); } // Unregister any remaining background tasks that remain in our dictionary. These are tasks that we registered // for Bluetooth LE devices that have since been unpaired. foreach (BackgroundTaskRegistration reg in taskRegistrations.Values) { reg.Unregister(false); } }
private async Task Connect() { try { _device = await Windows.Devices.Bluetooth.BluetoothLEDevice.FromBluetoothAddressAsync(220989373023087); _device.ConnectionStatusChanged += Device_ConnectionStatusChanged; _service = _device.GattServices.Where(s => s.Uuid == GattDeviceService.ConvertShortIdToUuid(0xFFE0)).SingleOrDefault(); if (_service != null) { _characteristic = _service.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0xFFE1)).Single(); _characteristic.ProtectionLevel = GattProtectionLevel.Plain; _characteristic.ValueChanged += Characteristic_ValueChanged; bool status = false; switch (_device.ConnectionStatus) { case Windows.Devices.Bluetooth.BluetoothConnectionStatus.Disconnected: status = false; break; case Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected: status = true; break; } OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(status)); } } catch (Exception) { MessageBox.Show("Nie można nawiązać łączności z urządzeniem sterującym. Możliwe przyczyny: \n\r- Brak odbiornika Bluetooth\n\r- Urządzenie nie jest sparowane\n\r- Brak sterowników Bluetooth.\n\rBez podłączonego urządzenia sterującego korzystanie z aplikacji nie jest możliwe.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); //switch ((uint)e.HResult) //{ // case 0x80070490: // MessageBox.Show("Wykonaj parowanie urządzenia sterującego z komputerem. Informacje dotyczące parowania znajdziesz w zakładce Help", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning); // break; //} } }
internal async Task <bool> Init() { bool success = false; bleDevice = await Windows.Devices.Bluetooth.BluetoothLEDevice.FromIdAsync(deviceId); services = bleDevice.GattServices; int ccc = services.Count; for (int i = 0; i < ccc; i++) { Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceService service = services[i]; string s = service.Uuid.ToString(); if (s == "4f63756c-7573-2054-6872-65656d6f7465") { myService = service; //var CharResult = myService.GetAllCharacteristics(); //Console.WriteLine("elo"); break; } } if (myService != null) { try { allCharacteristics = myService.GetAllCharacteristics(); Guid notifyGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d281}"); Guid writeGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d282}"); foreach (Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic c in allCharacteristics) { if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Notify) && c.Uuid == notifyGuid) { notifyCharacteristic = c; } else if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Write) && c.Uuid == writeGuid) { writeCharacteristic = c; } if (notifyCharacteristic != null && writeCharacteristic != null) { break; } } success = notifyCharacteristic != null && writeCharacteristic != null; } catch { } } return(success); }
private void _device_NameChanged(Windows.Devices.Bluetooth.BluetoothLEDevice sender, object args) { _nameChanged?.Invoke(this, null); }
private void _device_ConnectionStatusChanged(Windows.Devices.Bluetooth.BluetoothLEDevice sender, object args) { _connectionStatusChanged?.Invoke(this, null); }
private BluetoothLEDevice(Windows.Devices.Bluetooth.BluetoothLEDevice device) { _device = device; }
internal GattDeviceService(Windows.Devices.Bluetooth.BluetoothLEDevice device, Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceService service) { _device = device; _service = service; }
internal BluetoothLEDevice(Windows.Devices.Bluetooth.BluetoothLEDevice device) { _device = device; }
private void Device_ConnectionStatusChanged(Windows.Devices.Bluetooth.BluetoothLEDevice sender, object args) { ScanForDevices(); }