DeviceConnectionChangeTrigger - Value does not fall within the expected range - c#

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

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.

Related

How to handle Mediadevices FriendlyName when null

I was working with MediaDevice library , it went okay when the device name match the FriendlyName,
My problem is when i dont plug in my device into the PC or i change a simple letter in FriendlyName
var devicess = MediaDevice.GetDevices();
using (var device = devicess.First(d => d.FriendlyName == "MyCellPhone"))
{
device.Connect();
// get list of available storages (SD-Card, Internal Flash, ...)
var objects = device.FunctionalObjects(FunctionalCategory.Storage);
MediaStorageInfo infoss = GetStorageInfo(objects.First());
ulong size = infoss.FreeSpaceInBytes;
device.Disconnect();
}
for Example if my smartphone is named "samsung" an i put "samsam" in here
using (var device = devicess.First(d => d.FriendlyName == "samsam")) (instead of samsung)
the app crashes , the same thing if leave it empty or i run the code with no phone attached.
i tried some try {} catch{} but no result.
Can any one help me please .
Thank you.
Use FirstOrDefault instead of First:
var device = devicess.FirstOrDefault(d => d.FriendlyName == "MyCellPhone")
This will return null if it can't find a match, instead of throwing an exception.

Playing in-built webcam feed in a UWP app stopped working after?

I'm trying to play the built-in webcam feed in a MediaElement within a UWP app. It works fine for a few users but there is no feed played for most and I'm lost on what could be the issue.
Some observations when the webcam feed doesn't play:
The code executes without any exceptions
The dialog that requests user permission to access the camera is shown
The LED indicating the webcam is in use turns on soon as it is executed, but there is no feed.
Skype and Camera apps work fine.
The app was working as expected until a week back. A few things that changed in the mean time that could have had an impact are
Installed Kaspersky
A bunch of windows updates
Uninstalled VS2017 professional edition & VS2019 Community edition and installed VS2019 Professional Edition
Some additional information that might be needed to narrow down the reason.
Webcam is enabled in the Package manifest of the app
App Target version: 18362
App Min version: 18362
Windows OS Version : 18362
Any help on this would be highly appreciated. Thanks much in advance!
Here is the piece of code used to play the webcam feed where VideoStreamer is a MediaElement.
private async Task PlayLiveVideo()
{
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
var eligibleGroups = allGroups.Select(g => new
{
Group = g,
// For each source kind, find the source which offers that kind of media frame,
// or null if there is no such source.
SourceInfos = new MediaFrameSourceInfo[]
{
g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
&& info.SourceKind == MediaFrameSourceKind.Color),
g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
&& info.SourceKind == MediaFrameSourceKind.Color)
}
}).Where(g => g.SourceInfos.Any(info => info != null)).ToList();
if (eligibleGroups.Count == 0)
{
System.Diagnostics.Debug.WriteLine("No source group with front and back-facing camera found.");
return;
}
var selectedGroupIndex = 0; // Select the first eligible group
MediaFrameSourceGroup selectedGroup = eligibleGroups[selectedGroupIndex].Group;
MediaFrameSourceInfo frontSourceInfo = selectedGroup.SourceInfos[0];
MediaCapture mediaCapture = new MediaCapture();
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings()
{
SourceGroup = selectedGroup,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video,
};
try
{
await mediaCapture.InitializeAsync(settings);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
return;
}
var frameMediaSource1 = MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources[frontSourceInfo.Id]);
VideoStreamer.SetPlaybackSource(frameMediaSource1);
VideoStreamer.Play();
}
As mentioned by Faywang-MSFT here , it worked after marking the application as trusted in Kaspersky.

Bluetooth BLE Battery Level - C#

I have an specific issue about the Bluetooth BLE API on Windows 10.
At the moment I'm programming a tool on C# (Visual Studio) which connects itself to a given BLE - Device. Currently the connection works perfect and I can read out the ServiceUUIDs and the CharacterUUIDs.
The main Problem is after I try to read the value of the Character its always returns me 00. I heard that implementing a notification will change that and I followed these instructions but they didn't help me.
(Here are the Specifications from bluetooth.com:)
My Code:
//connect to BluetoothDevice
var device = await BluetoothLEDevice.FromIdAsync(address);
//get UUID of Services
var services = await device.GetGattServicesAsync();
if (services != null)
{
foreach (var servicesID in services.Services)
{
//if there is a service thats same like the Battery Service
if (servicesID.Uuid.ToString() == BluetoothBLE.Constants.BATTERY_SERVICE)
{
//updateServiceList is like a console logging in my tool
updateServiceList($"Service: {servicesID.Uuid}");
var characteristics = await servicesID.GetCharacteristicsAsync();
foreach (var character in characteristics.Characteristics)
{
if (Constants.BATTERY_LEVEL == character.Uuid.ToString())
{
updateServiceList("C - UUID: "+ character.Uuid.ToString());
GattReadResult result = await character.ReadValueAsync();
if (result.Status == GattCommunicationStatus.Success)
{
var reader = DataReader.FromBuffer(result.Value);
byte[] input = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(input);
System.Diagnostics.Debug.WriteLine(BitConverter.ToString(input));
}
}
}
}
After Running my Code, the system logs 00. The characterUUID for battery level (0x2A19, from https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml) was read successfully but the value is strange..

How do you know if a UWP device has Chat/SMS Available?

Is there a C# method to call to see if Chat/SMS is available on a Windows10 device?
You can have the chat capabilities even on devices without a SIM card. Even Skype can also play the role of the default SMS app...
This link is giving you a sample
private async void ComposeSms(Windows.ApplicationModel.Contacts.Contact recipient, string messageBody, StorageFile attachmentFile, string mimeType)
{
var chatMessage = new Windows.ApplicationModel.Chat.ChatMessage();
chatMessage.Body = messageBody;
if (attachmentFile != null)
{
var stream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(attachmentFile);
var attachment = new Windows.ApplicationModel.Chat.ChatMessageAttachment(mimeType, stream);
chatMessage.Attachments.Add(attachment);
}
var phone = recipient.Phones.FirstOrDefault<Windows.ApplicationModel.Contacts.ContactPhone>();
if (phone != null)
{
chatMessage.Recipients.Add(phone.Number);
}
await Windows.ApplicationModel.Chat.ChatMessageManager.ShowComposeSmsMessageAsync(chatMessage);
}
To check if the message is a SIM message, you should take a look at the property ChatMessage.IsSimMessage
var isSimMessage = chatMessage.isSimMessage;
You may try:
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.ApplicationModel.Chat "))
{
}
Only if the method returns “true” the code inside it will be implemented, which indicates that SMS/Chat is available in this device.
Otherwise, your project will skip this part of code since the capability is unavailable in the device in case that your app may crash on these devices.
For more details, may check this document.

Control Widows Media Device Flash Light

I am trying to create a simple application which will have the functionality to switch on and off the flash light in a Windows Media Device.
I have initialized the camera as following:
var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var rearCamera = devices.FirstOrDefault(item => item.EnclosureLocation != null &&
item.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
if (rearCamera != null)
{
DeviceName.Content = rearCamera.Name;
FlashButton.Visibility = System.Windows.Visibility.Visible;
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
VideoDeviceId = rearCamera.Id
});
LowLagPhotoCapture lowLagCaptureMgr = null;
// Image properties
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
// Create LowLagPhotoCapture object
lowLagCaptureMgr = await mediaCapture.PrepareLowLagPhotoCaptureAsync(imgFormat);
}
And to switch on the flash I have written the following code:
var MyVideoDeviceController = mediaCapture.VideoDeviceController;
var MyTorch = MyVideoDeviceController.TorchControl;
var MyFlash = MyVideoDeviceController.FlashControl;
if (MyTorch.Supported)
{
MyTorch.PowerPercent = 100;
MyTorch.Enabled = true;
}
else
{
if (MyFlash.Supported)
{
MyFlash.PowerPercent = 100;
MyFlash.Enabled = true;
}
else
{
MessageBox.Show("No Flash and Torch Support", "Flash and Torch");
}
}
But seems both TorchControl and FlashControl are not supported in the code. I am not sure if am using the right APIs too. I am trying to run this on a Motion F5m - Tablet PC
Thanks in advance
The TorchControl is used for constant video light, so if you're taking a photograph, it's not the most appropriate control to use. One reason is that on many devices, video light will be dimmer than a photo flash, but especially because on some devices, the torch will only turn on while a video recording is in progress. Depending on the capabilities of the device, this may interfere with the ability to take photos.
You have the right idea setting MyFlash.Enabled = true, but just to be safe, I would also set MyFlash.Auto = false, so that the flash will fire each time, and not only when it's dark.
The CameraManualControls sample on the Microsoft GitHub repository shows you how to use the Flash and Torch controls, and many more. It targets Windows 10, though, so if you're on 8.1 you'll have to adapt the code or upgrade your tablet.
Now, all of the above is assuming that the device you're running your app on has flash support in the first place. When you say that the controls are not supported, that means that the camera driver on the device is not advertising the capability to Windows. I assume that the built-in Microsoft Camera app doesn't allow you to use the flash either?
I see the manufacturer of your tablet lists an "Illuminator Light" on their camera specs list, but there is a chance that the only way to control it is through their proprietary application. In that case you'd have to reach out to them for support.

Categories

Resources