0
votes

I'm developing an UWP app for Desktop and Mobile. I'd like using DeviceConnectionChangeTrigger to identify when my BT device is in range, but when i try using it, I cannot register background task cause it keeps return System.ArgumentException "Value does not fall within the expected range." when executing "Register" method of BackgroundTaskBuilder.

code is very simple

    var current = BackgroundExecutionManager.GetAccessStatus();
    if (current == BackgroundAccessStatus.Unspecified || current == BackgroundAccessStatus.Denied)
    {
      var result = await BackgroundExecutionManager.RequestAccessAsync();
      if (result == BackgroundAccessStatus.Denied || result == BackgroundAccessStatus.Unspecified)
        throw new Exception("You cannot register Access");
    }

    //I tested it with BT and BTLE device but no luck
    //var devices = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector());
    var devices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector());
    var device = devices.FirstOrDefault();
    if (device == null) throw new Exception("Device not found");

    var trigger = await DeviceConnectionChangeTrigger.FromIdAsync(device.Id);
    //trigger.MaintainConnection = true;

    var builder = new BackgroundTaskBuilder
    {
      Name = "BluetoothConnectionWatcher",
      TaskEntryPoint = typeof(BluetoothConnectionWatcherTask).FullName
    };
    builder.SetTrigger(trigger);

    //error on next line!
    var r = builder.Register();

My project has Bluetooth capabilities and one background task registered with Bluetooth property flagged.

All Bluetooth functionalities work well and I can communicate with devices without problem.

I tried several BT and BTLE device and for all of them seems I have the same problem.

I tried on Desktop and Mobile version of Windows 10 (both last fast insider build). Same problem.

The device.Id returned by code is similar to "Bluetooth#Bluetooth00:1a:7d:da:71:0a-fc:58:fa:4c:17:0a" for every BT device i tried (of course 2nd MAC address change based on device...)

Any advice? Thanks in advance

1

1 Answers

0
votes

As far as I know, with BLE you should use the Triggers designed for it. Thus if it is characteristics change you would want to get knowledge about, then you should use GattCharacteristicNotificationTrigger, and example on using it can be found from my blog.

Then if it just BLE device availability, and you can detect yours via scanrecord data, then do use the BluetoothLEAdvertisementWatcherTrigger instead. My Friend Juhana has example fro this in his blog.