ReadingChanged never called for Accelerometer on WP8 - windows-phone-8

I'm trying to use the C++ Accelerometer interface in the Windows::Devices::Sensors namespace on the Windows Phone 8. The code is very similar to a C# project I have that works, but I can't get the C++ event to fire like I can with my C# code.
My C++ code is a C# project with a C++ component, the C++ component just opens up the Accelerometer device for reading, and then tries to setup an event to fire whenever data is ready:
AccelerometerWrapper::AccelerometerWrapper() {
Accelerometer^ acc = Accelerometer::GetDefault();
accReading = acc->ReadingChanged::add( ref new TypedEventHandler<Accelerometer^, AccelerometerReadingChangedEventArgs^>(this, &AccelerometerWrapper::ReadingChanged));
}
void AccelerometerWrapper::ReadingChanged(Accelerometer^ sender, AccelerometerReadingChangedEventArgs^ e) {
...
}
Unfortunately, my ReadingChanged() function is never being called. I've looked around for a Start() method or somesuch but I can't find anything. I'm basing most of my knowledge off of the AccelerometerCPP example, but I can't actually test that as it is a generic WinRT (e.g. Windows 8, not Windows Phone 8) example, and my computer does not have an accelerometer. Everything compiles and runs, the event is just never triggered.
EDIT: I have successfully run a test to verify that I can manually call acc->GetCurrentReading(), so the accelerometer is working, it just seems to be the event that is not getting triggered.
Thank you in advance!

I'm not a C++ expert, but the new Accelerometer sensor works on my machine using C#.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var accelerometer = Accelerometer.GetDefault();
if (accelerometer != null)
{
accelerometer.ReadingChanged += accelerometer_ReadingChanged;
}
}
void accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
Debug.WriteLine(args.Reading.AccelerationX + ", " + args.Reading.AccelerationY + "," + args.Reading.AccelerationZ);
}
When we run that code snippet we can see the following expected output:
Just a guess for C++, do you need to cache the Accelerometer instance somewhere instead of letting it go out of scope?

Related

GPS running in background when Here Drive+ is running

i have a problem running my wp (8.0) app properly in the background when HERE Drive+ is running in the foreground. my app is a location based app.
i've setup a little demo project to reproduce and isolate and simplify the problem.
I have a GeoLocator, which is checking and displaying the current location in the PositionChanged event to a label, when in foreground. when running in background, it displays a toast every 5 seconds (to show me, that the PositionChanged event is still triggered).
pretty forward stuff, that works.
public MainPage()
{
InitializeComponent();
DataContext = this;
App.LocationWatcher.ReportInterval = 5000;
App.LocationWatcher.DesiredAccuracy = PositionAccuracy.High;
App.LocationWatcher.PositionChanged += LocationWatcherOnPositionChanged;
App.LocationWatcher.StatusChanged += LocationWatcherOnStatusChanged;
}
private void LocationWatcherOnStatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
DisplayToast("Status:", args.Status.ToString());
}
private void LocationWatcherOnPositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
if (!App.IsRunningInBackground)
{
Dispatcher.BeginInvoke(() => {
this.tbkLastUpdatedValue.Text = DateTime.Now.Ticks.ToString();
this.tbkLatitudeValue.Text = args.Position.Coordinate.Latitude.ToString();
this.tbkLongitudeValue.Text = args.Position.Coordinate.Longitude.ToString();
});
}
else
{
DisplayToast("Location:", args.Position.Coordinate.Latitude.ToString());
}
}
So, now the problem: When my app runs in background, it displays it's toasts, when i run any other app (including the normal map, which actually uses gps), but when i run HERE Drive+, my PositionChanged and my StatusChanged events dont get triggered anymore.
there is an app on the marketplace that is capable to run in background when here drive+ is running in foreground, as stated in the marketplace comments (in german only)
any ideas how to solve this or what may cause that problem?
The Windows SDK is no longer. An alternative could be to use the HERE Maps API for Javascript, which supports vector map data rendering.

AdMob interstitial crashes with NullReferenceException in GoogleAds.DLL

I am trying to integrate an interstitial AdMob ad into a Windows Phone 8 MonoGame. It is the newest version of MonoGame (3.2) and the newest version of the AdMob SDK for Windows Phone 8 (6.5.13). Ideally I would like to be able to preload the ad and show it when the player dies.
This first example works, but it calls ShowAd directly in the event handler when the ad was received.
public void LoadAndShowAd()
{
interstitial = new InterstitialAd("xxx");
interstitial.ReceivedAd += OnAdReceived;
AdRequest adRequest = new AdRequest();
adRequest.ForceTesting = true;
interstitial.LoadAd(adRequest);
}
private void OnAdReceived(object sender, AdEventArgs e)
{
interstitial.ShowAd();
}
This slightly modified example does not work. The ad is received and is ready, but the call to ShowAd crashes within the GoogleAds.DLL with a NullReferenceException. As you can see the only difference is that ShowLoadedAd is called at a later point in time when the ad is ready.
public void LoadAd()
{
interstitial = new InterstitialAd("xxx");
interstitial.ReceivedAd += OnAdReceived;
AdRequest adRequest = new AdRequest();
adRequest.ForceTesting = true;
interstitial.LoadAd(adRequest);
}
public void ShowLoadedAd()
{
if (isReady)
interstitial.ShowAd();
}
private void OnAdReceived(object sender, AdEventArgs e)
{
isReady = true;
}
The "interstitial" object itself is not null, the exception happens within the GoogleAds dll as shown in the stack trace below. The exception itself sadly does not give away much:
System.NullReferenceException was unhandled by user code HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=GoogleAds
StackTrace:
at A.c81d6e02e9732689b8a214cc526c72649.c8b6f578dd247fa29b178e585ca8da582()
at A.c81d6e02e9732689b8a214cc526c72649.ce00282f6ceb6d2b777527815c84e2081()
at GoogleAds.InterstitialAd.ShowAd()
at GameName10.GamePage.<Test2>b__1()
InnerException:
The Google Ads example for Windows Phone 8 and interstitial ads actually uses the second approach which causes the exception in my monogame project. So I assumed it could be an issue with these calls being made from the game loop of monogame being in a different thread. I wrapped all the calls to these functions in Dispatcher.BeginInvoke calls to make sure they are called on the UI thread. However this does not change anything, the first example works regardless if it is wrapped in BeginInvoke or not and the second one does not.
Any input in how to solve this issue or integrate an interstitial AdMob ad into MonoGame successfully is appreciated.
Thanks

Windows phone 8.1 BackPressed not working properly

Windows phone 8.1 new to world. Basic function is back button click. Is that function not working properly is this windows phone 8.1. Is that behavior or i'm made mistake.
Below code using in Homepage but this code calling from all other class too while clicking back. I need to access below method only on Home page .
Please check below code and refer me good solution.
Please look my code:
public HomePage()
{
this.InitializeComponent();
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
}
Thanks
It is working properly. The BackPressed event is working app-wide. Two options that come to my mind:
write eventhandler that would recognize the Page in which you currently invoke it - simple example can look like this:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null) return;
if (frame.Content is HomePage)
{
e.Handled = true;
Debug.WriteLine("I'm in HomePage");
}
else if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
second option - subscribe to Windows.Phone.UI.Input.HardwareButtons.BackPressed when you enter the Page and unsubscribe when you leave the Page. Note that in this way there are some pitfalls - you have to handle properly OnNavigatedTo, OnNavigatedFrom, Suspending and Resuming (more about Lifecycle here). Note also that the subscription should be done before others - for example NavigationHelper.
Some remarks - the above code should work, but it also depends on other circumstances:
if there is something other subscribed to BackPressed before (in App.xaml.cs) - remember that usually events are fired in order they were subscribed
check if you are using NavigationHelper - it also subscribes to BackPressed
remember not to subscribe multiple times
remember to allow the User to leave your HomePage

Trigger a phone call in Windows Phone 8 application

I need to develop an app to make a call from the Windows Phone 8 app using Visual Studio.
But I couldn't find any resources to do it.
When a button is clicked I need to call to a mobile number which is already given.
By clicking that button I must call only to that mobile number.
This is what I coded. When a given button is clicked, I this method is calling...
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
PhoneCallTask phoneCallTask = new PhoneCallTask();
phoneCallTask.PhoneNumber = "0719957868";
phoneCallTask.DisplayName = "Gage";
phoneCallTask.Show();
}
But I get an unhandled exception.
Unhandled exception.
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
}
When you use PhoneCallTask, you have to specify a new Capability of your app in WMAppManifest.xaml: ID_CAP_PHONEDIALER
source
This is how you should do it:
PhoneCallTask phoneCallTask = new PhoneCallTask();
phoneCallTask.PhoneNumber = "2065550123";
phoneCallTask.DisplayName = "Gage";
phoneCallTask.Show();
Remember that the call is not automatically started but it prompts the user to confirm that action.
Here is how to initiate a call on windows phone
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394025(v=vs.105).aspx
Ok for future reference i must add this.
If you get an Unauthorized Access Exception then you need to enable ID_CAP_PHONEDAILER from the Capabilities section in the WMAppManifest.xml file.
See here

Windows Phone 8 Connection Handler / Internet Availability

My team is working on a team project aplication. At the moment, we need an event handler to check the connection status (if it's on/off).
I had big hopes in the System.Net.NetworkInformation Namespace, but unfortunately most important things aren't supported in wp8.
Can anyone help me with it a bit?
Edit 1#
It seems, I didn't specifed my problem well.
I'm using Mvvm light expresion, and it does not support that namespace or at least I can't add it.
I'm a newbie in using VS and c# atm, mayby I'm doing someting wrong, but simply when im trying to add the refernce to my project it does not list.
I haven't tried the System.Net.NetworkInformation namespace on WP8. But the new WP8 Windows.Networking.Connectivity Windows Phone Runtime namespace works just fine.
Use Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged to know when network conditions change and use Microsoft.Phone.Net.NetworkInformation.NetworkInterface properties or Windows.Networking.Connectivity.NetworkInformation properties to see what's up.
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
PrintNetworkStatus();
NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
}
void NetworkInformation_NetworkStatusChanged(object sender)
{
PrintNetworkStatus();
}
private void PrintNetworkStatus()
{
Dispatcher.BeginInvoke(() =>
MessageBox.Show(NetworkInterface.NetworkInterfaceType +
Environment.NewLine +
NetworkInterface.GetIsNetworkAvailable()));
}
When I test this code snippet on my WP8 Lumia 920 it works as expected. On startup when my phone is on WiFi only I see the following MessageBox:
And once I shutdown my WiFI router and the WiFi connection on the phone is lost I see the following MessageBox:
Try this:
bool isNetwork=NetworkInterface.GetIsNetworkAvailable();
if(!isNetwork)
{
//proceed with your code
}
In App.xaml.cs, create a property like below
/// <summary>
/// check if network is available
/// </summary>
public bool IsNetworkAvailable
{
get
{
return NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None;
}
}
And you can use this property anywhere in your project as in below code
if (((App) Application.Current).IsNetworkAvailable)
{
//Lines of Code
}
else
{
MessageBox.Show("Not Connected to Network!", "Checking Connection!",
MessageBoxButton.OK);
}