Windows Phone emulator 8.1 await InvokeScriptAsync silent crash? - windows-phone-8

Why this piece of code crash silently my windows pone 8.1 emulator? If not the first time click it till it will do it (second or third time).
private async void MainPage_BackPressed(object sender, BackPressedEventArgs e)
{
var result = await WebViewControl.InvokeScriptAsync("eval", new string[] { document.location.href" });
e.Handled = true;
}

Related

Reading data from Arduino Bluetooth on Windows Phone 8.1

I am developing a code that will allow me to send small amounts of data between my arduino mega and my windows 8.1 phone using a HC-05 bluetooth module.
Sending data from the phone to the arduino was pretty simple, but I am having a dire time attempting to read data that comes back from the arduino that I send on the serial port.
For testing purposes my code is simple, sending ascii charaters 'a' & 'b' to turn an LED on & off, this could not work any better, but I am having trouble trying to figure out how to correctly read the data that I send back to my phone.
I send a single arbitrary ascii character back to the phone but I cannot for the life of me figure out the correct way of reading this data from the bluetooth stream I have setup.
I have been trying for nearly two days, but everything I try ends up freezing my phone with no exceptions thrown? A lot of posts online send me to the Nokia dev site which is now inactive.
I have tried using the 'datareader' and the 'streamreader' classes to do this but it always freezes, does anyone know how to make this work? And why my streamreader keeps freezing my phone?
I have tried to annotate my code appropriatley (seen below). The problem occurs in the 'Tick' event handler at the bottom of the code.
(FYI: All capabilities have been added to the manifest files so this shouldn't be the problem).
Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Bluetooth_SL.Resources;
using Windows.Networking;
using Windows.Networking.Proximity;
using Windows.Networking.Sockets;
using Windows.Devices.Bluetooth;
using System.IO;
using Microsoft.Xna.Framework;
using System.Windows.Threading;
using Windows.Storage.Streams; // <-- for the datareader class
namespace Bluetooth_SL // silverlight, does this matter?
{
public partial class MainPage : PhoneApplicationPage
{
DispatcherTimer Timer = new DispatcherTimer();
StreamSocket socket = new StreamSocket();
StreamWriter writer;
StreamReader reader;
public MainPage()
{
InitializeComponent();
Timer.Interval = TimeSpan.FromMilliseconds(1000); // dispatcher timer used to check for incoming data from arduino
Timer.Tick += Timer_Tick; // event handler for dispatcher timer
}
protected override void OnNavigatedFrom(NavigationEventArgs e) // frees up memory
{
socket.Dispose();
}
private void Connect_But_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ConnectToBluetooth();
}
private async void ConnectToBluetooth() // sets up the connection // this works fine
{
// Configure PeerFinder to search for all paired devices.
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
if (pairedDevices.Count == 0)
{
Debug_Block.Text = "No paired devices were found.";
}
else
{
Debug_Block.Text = "Found";
PeerInformation selectedDevice = pairedDevices[0]; // pick the first paired device
try // 'try' used in the case the socket has already been connected
{
await socket.ConnectAsync(selectedDevice.HostName, "1");
writer = new StreamWriter(socket.OutputStream.AsStreamForWrite());
writer.AutoFlush = true;
reader = new StreamReader(socket.InputStream.AsStreamForRead());
Debug_Block.Text = "Connected";
}
catch (Exception x)
{
Debug_Block.Text = x.ToString();
}
}
}
private void SendButton_Tap(object sender, System.Windows.Input.GestureEventArgs e) // this works perfectly
{
try { writer.WriteLine("a"); } // attempts to write the ascii 'a' to the arduino which turns on the on-board LED
catch { Debug_Block.Text = "Failed to write"; }
}
private void SendButton_Off_Tap(object sender, System.Windows.Input.GestureEventArgs e) // this works perfectly
{
try { writer.WriteLine("b"); } // attempts to write the ascii 'b' to the arduino which turns off the on-board LED
catch { Debug_Block.Text = "Failed to write"; }
}
private void ReadButtonToggle_Tap(object sender, System.Windows.Input.GestureEventArgs e) // toggles the timer on and off
{
if(Timer.IsEnabled == true)
{
Timer.Stop();
Debug_Block.Text = "Timer Stopped";
}
else
{
Timer.Start();
Debug_Block.Text = "Timer Started";
}
}
void Timer_Tick(object sender, EventArgs e) // THIS IS THE PROBLEM
{
Debug_Block.Text = "Tick";
Debug_Block.Text = reader.ReadLine(); // <-- ALWAYS FREEZES HERE
Timer.Stop(); // This line is temporary for debugging
}
}
}

recieving notifications from parse server on windows phone 8

I'm building a windows phone application connected with parse server I want know could I recieve push notifications from the server (I send the notification from the server but I don't recieve it and the connection is fine with the server because I can send objects and the server recieve them perfectly). I made many reseaches I found a piece of code but I didn't know where to put it so I put it on a button click like this:
private async void Button_Click_3(object sender, RoutedEventArgs e)
{
try
{
var installation = ParseInstallation.CurrentInstallation;
installation.AddUniqueToList("channels", "Giants");
await installation.SaveAsync();
MessageBox.Show("Done ! ");
ParsePush.ToastNotificationReceived += ParsePushOnToastNotificationReceived;
await ParseAnalytics.TrackAppOpenedAsync();
MessageBox.Show("Done ! ");
}
catch (Exception ex)
{
}
}
void ParsePushOnToastNotificationReceived(object sender, NotificationEventArgs notificationEventArgs)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// do anything
MessageBox.Show("got notification");
});
}

Store update of 8.1 app to uwp app

I have a Windows Phone 8.1 app in the store. Now I have created an uwp update. My question is:
If I load an update of the app into the store and an user does this update. Is the app just overwritten or deinstalled and then new installed? And are the saved settings in ApplicationData.Current.LocalSettings deleted?
thx newone
TL;DR; - It preserves data in LocalFolder and LocalSettings when updating from WP8.1 Runtime to UWP (tested with mobile on device with Insider preview - Fast ring).
I've run similar test, like the last time:
I've published a Beta version of the App - WP8.1 Runtime.
After successful installation on the Phone, I've created a file in LocalFolder and set value in LocalSettings (see code below),
I've submitted an update - went to Store, selected the App, clicked Update then Packages, after a while, browse your files and chosen the new generated appxbundle (I have not deleted the old WP8.1 package), save and submit.
After some time my Phone is notified that there is an update for the App - I click update
After successful installation, I see that it's a new App, I click my special button to check LocalFolder and value in LocalSettings - I see that there are old values from WP8.1 version.
Code for buttons used to test:
private async void Generate_Click(object sender, RoutedEventArgs e)
{
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.txt");
await FileIO.WriteTextAsync(file, "Something inside");
}
private async void CheckFile_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("test.txt");
string text = await FileIO.ReadTextAsync(file);
await new MessageDialog($"File exists = {text}").ShowAsync();
}
catch (Exception) { await new MessageDialog("File desnt exists").ShowAsync(); }
}
private void GenerateSetting_Click(object sender, RoutedEventArgs e) => ApplicationData.Current.LocalSettings.Values["CHECK"] = "Test value";
private async void CheckSetting_Click(object sender, RoutedEventArgs e)
{
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("CHECK"))
await new MessageDialog($"Setting exists = {ApplicationData.Current.LocalSettings.Values["CHECK"]}").ShowAsync();
else await new MessageDialog("Setting doesn't exist").ShowAsync();
}

WinRT Toolkit Windows Phone 8.1 save video wth CameraCaptureControl

I'm pretty new on Windows Phone 8.1 development and what I'm currently trying is recording a video and store it on the Windows Phone. However, I don't have any idea how that can be done. I have some code excerpt below which is the code executed when the start/stop record button is pressed. The code is taken from an example.
My questions:
How _videoFile can be saved to the VideoLibrary?
Preferably I would like the program to execute a method when recording is stopped. How I get the video filename inside this method?
private async void OnCaptureVideoButtonClick(object sender, RoutedEventArgs e)
{
if (!_capturingVideo)
{
//BtnStartStop.Content = "Stop";
StartAppBarButton.Icon = new SymbolIcon(Symbol.Stop);
_capturingVideo = true;
_videoFile = await TestedControl.StartVideoCaptureAsync(KnownFolders.VideosLibrary, "capture.mp4");
CapturedVideoElement.Visibility = Visibility.Visible;
IRandomAccessStreamWithContentType stream;
try
{
stream = await TryCatchRetry.RunWithDelayAsync<Exception, IRandomAccessStreamWithContentType>(
_videoFile.OpenReadAsync(),
TimeSpan.FromSeconds(0.5),
10);
}
catch (Exception ex)
{
#pragma warning disable 4014
new MessageDialog(ex.Message, "Error").ShowAsync();
#pragma warning restore 4014
return;
}
CapturedVideoElement.SetSource(stream, _videoFile.ContentType);
}
else
{
StartAppBarButton.Icon = new SymbolIcon(Symbol.Camera);
_capturingVideo = false;
#pragma warning disable 4014
await TestedControl.StopCapture();
#pragma warning restore 4014
}
}
Using the await keyword, StartVideoCaptureAsync is called asynchronously.
So the next line of code will be executed only once this asynchronous task is finished.
It means that the line of code below (and all the next ones):
CapturedVideoElement.Visibility = Visibility.Visible
will be executing at the end of the recording.
So if you need to execute a method after the recording is done, you can just put it after the call of StartVideoCaptureAsync.

CameraPreviewImageSource empty preview frame

I made cut and paste of the code below about how to use CameraPreviewImageSource and access to preview buffer frames, but do not work and it seems the frame buffer size is 0x0 reading the value of IImageSize parameter of OnPreviewFrameAvailable event.
How to get preview buffer of MediaCapture - Universal app
protected override void OnNavigatedTo(NavigationEventArgs e)
{
InitializeAsync();
}
public async void InitializeAsync()
{
_cameraPreviewImageSource = new CameraPreviewImageSource();
await _cameraPreviewImageSource.InitializeAsync(string.Empty);
var properties = await _cameraPreviewImageSource.StartPreviewAsync();
var width = 640.0;
var height = 480;
_writeableBitmap = new WriteableBitmap((int)width, (int)height);
_writeableBitmapRenderer = new WriteableBitmapRenderer(_cameraPreviewImageSource, _writeableBitmap);
Initialized = true;
_cameraPreviewImageSource.PreviewFrameAvailable += OnPreviewFrameAvailable;
}
private async void OnPreviewFrameAvailable(IImageSize args)
{
System.Diagnostics.Debug.WriteLine("ww:"+args.Size.Width+" hh:"+args.Size.Height);
// Prevent multiple rendering attempts at once
if (Initialized && !_isRendering)
{
_isRendering = true;
try
{
await _writeableBitmapRenderer.RenderAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("\n\n"+ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
_isRendering = false;
}
}
Capabilities (webcam & microphone) on Package.appxmanifest has been selected
Implementing CameraPreviewImageSource on a Silverlight app works great!
I am afraid you are (were) seeing a bug in Lumia Imaging SDK 2.0.184. The problem only occured on some camera models and only on 8.1/universal applications. Silverlight applications were not affected by the problem.
The bug has been fixed in the newly released Lumia Imaging SDK 2.0.208. From release notes:
Fixed ArgumentOutOfRangeException being thrown by CameraPreviewImageSource when used with certain camera models.