Check if there a usb/AC connection - windows-phone

I'm developing an universal windows phone application which checks if there a connection via usb or AC charging.
I used this code:
var deviceInfo = await DeviceInformation.FindAllAsync(Battery.GetDeviceSelector());
var aggBattery = Battery.AggregateBattery;
var report = aggBattery.GetReport();
report.Status.ToString();
It works well for some devices.
There are few devices (devices which got upgraded from WP8 to WP10) that takes them 30 seconds to detect the "charging mode".

I want to know if there a way to detect a usb connection?
To detect USB device, we can use a DeviceWatcher object to detect when the device is added to or removed from the system.
For Windows 10 Universal app, here is a good sample: Device enumeration sample
or get the battery level?
The Battery class does not provide method to get the battery level.
As a workaround, we can use BatteryReport.RemainingCapacityInMilliwattHours to get the remaining power capacity of the battery, in milliwatt-hours. Also we can use BatteryReport.FullChargeCapacityInMilliwattHours to get the fully-charged energy capacity of the battery. Then we can use BatteryReport.RemainingCapacityInMilliwattHours/BatteryReport.FullChargeCapacityInMilliwattHours to get the battery level.
For example:
var deviceInfo = await DeviceInformation.FindAllAsync(Battery.GetDeviceSelector());
var aggBattery = Battery.AggregateBattery;
var report = aggBattery.GetReport().RemainingCapacityInMilliwattHours;
var report3 = aggBattery.GetReport().FullChargeCapacityInMilliwattHours;
MyText.Text = (report / (double)report3).ToString();

Related

How to fix No I2c device exists for bus ID1 (GrovePi .net core)

I'm making a .net core API to control the led lights from my pi, but I'm facing the following error: System.ArgumentException:
No I2C device exists for bus ID 1.
I tried to find some info on how to find the busid of the grovepi but didn't came far. Tried also numbers 1 to 5 also didn't came far.
This is my code:
[1] https://gyazo.com/aa01ab3068201360c3ece14f125b1c45
My error:
[2] https://gyazo.com/6bf3215e4466b02643b6a9eb92d12e52
I expected to turn the light on and get a page with some text but I keep getting the same error.
I can reproduce this issue. I tracked the exception, it is thrown here. I posted an issue(#590) in GitHub.
public Windows10I2cDevice(I2cConnectionSettings settings)
{
_settings = settings;
var winSettings = new WinI2c.I2cConnectionSettings(settings.DeviceAddress);
string busFriendlyName = $"I2C{settings.BusId}";
string deviceSelector = WinI2c.I2cDevice.GetDeviceSelector(busFriendlyName);
DeviceInformationCollection deviceInformationCollection = DeviceInformation.FindAllAsync(deviceSelector).WaitForCompletion();
if (deviceInformationCollection.Count == 0)
{
throw new ArgumentException($"No I2C device exists for bus ID {settings.BusId}.", $"{nameof(settings)}.{nameof(settings.BusId)}");
}
_winI2cDevice = WinI2c.I2cDevice.FromIdAsync(deviceInformationCollection[0].Id, winSettings).WaitForCompletion();
if (_winI2cDevice == null)
{
throw new PlatformNotSupportedException($"I2C devices are not supported.");
}
}
As we know, .net core 3.0 for IoT is preview. May i know why don't you consider to use traditional way to do that? A workaround is, you can host a web server in a UWP app, and you can add the LED control in the UWP app. Please refer to IoTWeb.

How to set the input audio device for Microsoft.Speech recognizer in VB.Net or C# to any audio device

I want to use the Microsoft.Speech namespace in VB.NET to create a telephony application. I need to be able to set the recognizer input to any audio device installed on the system. Microsoft has the recognizer.SetInputToDefaultAudioDevice() method, but I need something like .SetInputToAudioDeviceID. How can I choose another wave audio input from the list of devices installed on my system? In SAPI, I would use MMSystem and SpVoice:
Set MMSysAudioIn1 = New SpMMAudioIn
MMSysAudioIn1.DeviceId = WindowsAudioDeviceID 'set audio input to audio device Id
MMSysAudioIn1.Format.Type = SAFT11kHz8BitMono 'set wave format, change to 8kHz, 16bit mono for other devices
Dim fmt As New SpeechAudioFormatInfo(1000, AudioBitsPerSample.Eight, AudioChannel.Mono)
Recognizer.SetInputToAudioStream(MMSysAudioIN1, fmt)
How do I do this with Microsoft.Speech?
MORE INFO: I want to take any wave input device in the Windows list of wave drivers and us that as input to speech recognition. Specifically, I may have a Dialogic card with wave input reported by TAPI as deviceID 1-4. In SAPI, I can use the SpMMAudioIn class to create a stream and set which device ID is associated with that stream. You can see some of that code above. Can I directly set Recognizer1.SetInputToAudioStream by the device ID of the device like I can in SAPI? Or do I have to create code that reads bytes and uses buffers, etc. Do I have to create a MemoryStream Object? I can't find any example code anywhere. What do I have to check in .NET to get access to ISpeechMMSysAudio/spMMAudioIn in case something like this would work? But hopefully, there is a way to use MemoryStream or something like it that takes a device ID and lets me pass that stream to the recognizer.
NOTE 2: I added "imports Speechlib" to the VB project and then tried to run the following code. It gives the error listed in the comments below about not being able to set the audio stream to a COM object.
Dim sre As New SpeechRecognitionEngine
Dim fmt As New SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono)
Dim audiosource As ISpeechMMSysAudio
audiosource = New SpMMAudioIn
audiosource.DeviceId = WindowsAudioDeviceID 'set audio input to audio device Id
' audiosource.Format.Type = SpeechAudioFormatType.SAFT11kHz16BitMono
sre.SetInputToAudioStream(audiosource, fmt) <----- Invalid Cast with COM here
It also appears the SpeechAudioFormatType does not support 8kHz formats. This just gets more and more complicated.
You would use SpeechRecognitionEngine.SetInputToAudioStream. Note that if you're having problems with streaming input, you may need to wrap the stream, as illustrated here.

Detecting Front and Back camera in windows 8 tabs

I am developing a metro style app using c# and xaml. For a particular task i need to detect which cam(front or back) is currently capturing. Is there any way to detect front cam or back cam in winrt. Please help me.
Using indexes on the DeviceInformationCollection won't be a reliable solution:
Sometimes front camera will be index 0 and sometimes 1, after testing on the surface 2 a few times it seems to be kind of random.
User could use the USB port to connect an other webcam so you could end up with more than 2 items in your collection without any clue which camera is which index.
Having the same problem as you this is how I solved it:
// Still need to find all webcams
DeviceInformationCollection webcamList = await eviceInformation.FindAllAsync(DeviceClass.VideoCapture)
// Then I do a query to find the front webcam
DeviceInformation frontWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
// Same for the back webcam
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
In this exemple I used Linq queries but it works the same with a foreach on "webcamList".
Just look on each DeviceInformation the .EnclosureLocation.Panel property, which is a Windows.Devices.Enumeration.Panel enum. The rest is obvius, Front for the front camera, Back for the back one.
Be also carefull to check if .EnclosureLocation is null or not, using an USB webcam it seems to be null most of the time.
You can use this code.
DeviceInformationCollection videoCaptureDevices = await eviceInformation.FindAllAsync(DeviceClass.VideoCapture);
If the videoCaptureDevices count is zero ,there is no camera attached.
And if camera count is 2 , then there will be both Front & back cameras.
If you initializing camera operation with videoCaptureDevices [0], that will be using Front Camera, and will be Back Camera if it is using videoCaptureDevices [1] .

UVC Extension Unit transfert only one byte of data?

I am using UVC extension Unit between Windows7 / Windows xp and Custom device.
To access the custom device I am using the following COM interface :
KSP_NODE s;
s.Property.Set = Guid_KSPROPSETID;
s.Property.Id = PropID;
s.Property.Flags = KSPROPERTY_TYPE_GET | KSPROPERTY_TYPE_TOPOLOGY;
s.NodeId = dwExtensionNode;
hr = pIKsControl->KsProperty( (PKSPROPERTY) &s, sizeof(s), pbPropertyValue, ulSize, &ulBytesReturned);
It works fine, but at the Windows UVC side I can't transfert more than one byte instead of the complete buffer pbPropertyValue of ulSize size. Someone know why? And how to fix it?
One more question, I am trying to find how to access the UVC_GET_MIN, UVC_GET_MAX, UVC_GET_INFO, UVC_GET_DEF and UVC_GET_RES with Extension unit? With the standard property I am using pVideoProcAmp->GetRange Method. But I didn't find the equivalent for the extension unit.
Finally, the problem was coming from the UVC_GEN_LEN return value.
The length need to be = 0x02;
And the data back needed to be equal to the len of the ulSize..

How to get notifications when the headphones are plugged in/out? Mac

I'd like to get notified when headphones are plugged in or out in the headphone jack.
I've searched around for this on stackoverflow but I can't seem to find what I'm looking for for the Mac, I can only find for iOS.
So, do you have any ideas on how to perform this? What I want to do with this is: when headphones are plugged out I want to programmatically pause iTunes (iOS-like feature).
Thank you!
You can observe changes using the CoreAudio framework.
Both headphones and the speakers are data sources on the same audio output device (of type built-in). One of both will be on the audio device based on headphones being plugged in or not.
To get notifications you listen to changes of the active datasource on the built-in output device.
1. Get the built-in output device
To keep this short we'll use the default output device. In most cases this is the built-in output device. In real-life applications you'll want to loop all available devices to find it, because the default device could be set to a different audio device (soundflower or airplay for example).
AudioDeviceID defaultDevice = 0;
UInt32 defaultSize = sizeof(AudioDeviceID);
const AudioObjectPropertyAddress defaultAddr = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultAddr, 0, NULL, &defaultSize, &defaultDevice);
2. Read its current data source
The current datasource on a device is identified by an ID of type UInt32.
AudioObjectPropertyAddress sourceAddr;
sourceAddr.mSelector = kAudioDevicePropertyDataSource;
sourceAddr.mScope = kAudioDevicePropertyScopeOutput;
sourceAddr.mElement = kAudioObjectPropertyElementMaster;
UInt32 dataSourceId = 0;
UInt32 dataSourceIdSize = sizeof(UInt32);
AudioObjectGetPropertyData(defaultDevice, &sourceAddr, 0, NULL, &dataSourceIdSize, &dataSourceId);
3. Observe for changes to the data source
AudioObjectAddPropertyListenerBlock(_defaultDevice, &sourceAddr, dispatch_get_current_queue(), ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses) {
// move to step 2. to read the updated value
});
Determine the data source type
When you have the data source id as UInt32 you can query the audio object for properties using a value transformer. For example to get the source name as string use kAudioDevicePropertyDataSourceNameForIDCFString. This will result in the string "Internal Speaker" or "Headphones". However this might differ based on user locale.
An easier way is to compare the data source id directly:
if (dataSourceId == 'ispk') {
// Recognized as internal speakers
} else if (dataSourceId == 'hdpn') {
// Recognized as headphones
}
However I couldn't find any constants defined for these values, so this is kind of undocumented.
I was looking for a similar solution and found AutoMute in the app store. It works well.
I'm also working on some scripts of my own, and wrote this script to test if headphones are plugged in:
#!/bin/bash
if system_profiler SPAudioDataType | grep --quiet Headphones; then
echo plugged in
else
echo not plugged in
fi