Getting GPS Coordinates Windows Phone 7 - c#

I've been looking quite some time today to gather GPS coordinates from a Windows Phone 7 device - however since I do not have an actual test device here I tried to set some dummy data which I want to have returned instead of real GPS Data ... that, however ist not working out too well:
This code ist partially an example from so which I found here. However I tried to put it into a class which I can access later.
public class GetGPS : GeoCoordinateWatcher
{
GeoCoordinateWatcher watcher;
public GetGPS()
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.PositionChanged += this.watcher_PositionChanged;
watcher.StatusChanged += this.watcher_StatusChanged;
watcher.Start();
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Ready:
//plingpling
break;
case GeoPositionStatus.Disabled:
// location is unsupported on this device
break;
case GeoPositionStatus.NoData:
watcher.Position.Location.Latitude = 54.086369f;
watcher.Position.Location.Longitude = 12.124754f;
break;
}
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var epl = e.Position.Location;
// Access the position information thusly:
epl.Latitude.ToString("0.000");
epl.Longitude.ToString("0.000");
epl.Altitude.ToString();
epl.HorizontalAccuracy.ToString();
epl.VerticalAccuracy.ToString();
epl.Course.ToString();
epl.Speed.ToString();
e.Position.Timestamp.LocalDateTime.ToString();
}
}
This is my other class in which I try to access the data - however I always get NaN as lat1Rad and long1Rad ... can you please help me?
I want that example to be functional on the emulator ( with a fixed GPS Coordinate ) and on a phone 7 device - where it actually grabs the value.
GetGPS location1= new GetGPS();
//GeoCoordinate myPosition = location1.getPosition();
//Radianten berechnen
double lat1Rad = GradZuRad(location1.Position.Location.Latitude);
double long1Rad = GradZuRad(location1.Position.Location.Longitude);
I basically just want to program a class which returns me the CURRENT GPS Position.

Where is your example code from?
Have you tried using the sample on MSDN?
Alternatively, there's a greate simulator available from http://phone7.wordpress.com/2010/08/02/no-device-no-gps-no-matter-with-code/

Why are you deriving frmo GeoCoordinateWatcher? That's a mistake, IMO. It makes it unclear when you're using the members of your own class and when you're using the delegated instance. At the moment you're setting the coordinates on the delegated watcher, but then asking for the coordinates from the GetGPS instance directly.
I suggest you implement IGeoPositionWatcher<GeoCoordinate> with your own "fixed" position watcher - and then decide at execution time whether to use that or the real GeoCoordinateWatcher. Obviously this means your client code should only depend on IGeoPositionWatcher<GeoCoordinate> instead of GeoCoordinateWatcher directly. This should also help for unit testing purposes.

Of course Jon's answer is very apropos - I think you've not done reasonable interface abstraction, but even when you get that, for simulated data have you taken a look at Kevin Wolf's GPS Simulator for Windpws Phone?

Related

Bluetooth Fit Watch Connection Xamarin Android

i recently posted about connection to bluetooth devices using Xamarin. I managed to get the device and the mac address on a list. Im having a problem connecting to the device. It doesnt do anything when clicked... What are the next steps to pairing with device and is it even possible to pair with a fit watch, im trying to use the heart rate monitor on the fit watch as well.
I'm guessing the next step would be to setup a Click event on the list...
Something like
myListView = FindViewById<ListView>(Resource.Id.list);
myListView.ItemClick += List_Click;
private void List_Click(object sender, AdapterView.ItemClickEventArgs e)
{
//throw new NotImplementedException();
}
and is it possible to get the data from the fit watch and sync it with a chart.
I have managed to get a pie chart . now i just need to pair the fit watch and get the data from bluetooth. Any advice would be grateful thanks
You could get a BluetoothDevice object from the BluetoothAdapter
and something like :
myListView.ItemClick += List_Click;
private void List_Click(object sender, AdapterView.ItemClickEventArgs e)
{
var address = xxxxx; //the address you select
BluetoothDevice btDevice = mBluetoothAdapter.GetRemoteDevice(address);
var _socket = btDevice .CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
_socket.Connect();
}
Much of the specifics for handling paired devices is in the documentation

C# trackbar not updating

i've run into this pretty tricky problem recently and i hoped somebody could help me.
i have a program that uses trackbars as to display sound volume and it's controlled with an Arduino via serial.
When i try to modify the value (programmaticaly) of the trackar (moving the slider) in any method, it works perfectly with the following code :
trackbar1.Value = ...;
However, when i put this in my serial data handler, it doesn't works :/
I declare the serial data handler this way :
//declaring arduinoCom
public SerialPort arduinoCOM;
//In form1
arduinoCOM.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
my handler looks like this :
public void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
trackBar2.Value = 50;
}
The serial communication works flawlessly, and the handler does it's job no problem.
I've tried for 2 days now and i was able to identify that the only difference between the working trackbar and the not-working one is where the "trackbar1.value" is located. So i've remove the rest of the (i hope) unessecary code for clarity reasons.
So my Question is why does the Trackbar slider doesn't move when i try to modify it's value outside of the "standards method"
additional informations : I've tried runnning the program and then pausing it with visual stuio and the trackbar.Value has been changed successfully, the only thing that isn't working is the graphics side.
I've tested with multiple trackbars, and tried using
trackbar1.Refresh();
it didn't work
Picture of the value of trackbar 1 and 2 as well as picture of all 5 :
Values of trackbars
trackbars not moving
The DataReceived event for SerialPort is raised on a secundary thread (not the UI thread) from which you cannot change UI elements.
Using 'Invoke', you can make the change in the UI thread
Instead of
public void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
trackBar2.Value = 50;
}
use:
public void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
if (trackbBar2.IsHandlecreated) trackBar2.Invoke(new Action(() => trackbar.Value = 50));
}
I found the problem, when i was declaring my serial communication i was using `
Form1 form1 = new Mixer.Form1();
initialiseSerialEventHandler(arduinoCOM);
and instead i should only use
initialiseSerialEventHandler(arduinoCOM);

How to notify UI about action done in API?

I'm writing a simple game, like tic tac toe (only mine is bigger). The game play is simple: my turn, check if won, bot's turn, check if won. I have simple UI and API that uses Domain Entites (that's not important now). So when user's moves, API will update the board, will know that next move is bot's move, so will do it and... has to notify UI. Here is my problem.
My question is:
How to notify UI about bot's move? I mean, to keep it simple but stick to the best programming practices.
My first thought was to create an event in GameAPI class. Is that good idea? Today will all new stuff, C# 6, etc.. I'm not sure:/ Right now UI is WinForms, but I would like to use this API in other UIs, like WPF or even mobile. Here is my simplified code of UI:
EDIT: Right now I'm talking about single player game. Both UI and API is a Client. There will be multiplayer through central server in next step, but right now, single player.
public partial class Form1 : Form
{
private GameAPI api;
public Form1()
{
InitializeComponent();
api = new GameAPI();
}
private void boardClick(object sender, EventArgs e)
{
Field field = GetClickedField(e);
MoveResult result = api.MakeMove(clickedColumn);
if (result != null && result.Row >= 0)
{
MessageBox.Show(result.Row + "," + clickedColumn);
if (result.IsConnected)
{
MessageBox.Show("Success!");
}
}
}
}
and API:
public class GameAPI
{
public IGame CurrentGame { get; set; }
public void CreateGame(GameType type)
{
CurrentGame = new SinglePlayerGame();
}
public Result Move(int column)
{
if (CurrentGame == null) return null;
Player player = CurrentGame.GetNextPlayer();
if (player.Type == PlayerType.Human) return CurrentGame.Move(column, player.Id);
}
public Result MoveBot()
{
// Simulate Bot's move...
}
}
My first thought was to create an event in GameAPI class. Is that good idea?
Yes, why not? Let take for example the modern UI frameworks data binding. The key point of making data binging work is providing a property change notification (read - event) when some property value of the object is modified. Usually that's implemented via IPropertyNotifyChanged interface, which is simply a polymorphic way of declaring support for PropertyChanged event. This way, if you set the object property via code, the UI updates automatically. Hope you see the similarity with your case - the API does something and raises an event, UI (being attached handler to that event as some earlier point) receives the event and updates accordingly.

Windows 7 Desktop without location sensor

I'm writing a windows 7 desktop C# app and I need to write code when there is a location sensor. I have this:
public string check(string stop)
{
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
watcher.Start();
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
return null;
}
static void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Initializing:
Console.WriteLine("Working on location fix");
break;
case GeoPositionStatus.Ready:
Console.WriteLine("Have location");
break;
case GeoPositionStatus.NoData:
Console.WriteLine("No data");
break;
case GeoPositionStatus.Disabled:
Console.WriteLine("Disabled");
break;
}
MessageBox.Show("GO");
}
This is really just to see if it grabs the latitude and longitude, but I don't have a sensor and I just want to know, will this work if a laptop does have a sensor? I can't afford to get a sensor yet so I would like to have the code and move on until I can aquire one. Also, I need to stop the geocoordinatewatcher when the user logs out, so since I cant stop this in a different function, if I start another and stop that one will that stop all?These functions are called from a webapge and there are no buttons on my form. The function called when a user logs out will be stop();. And just in case someone wanted to suggest it, I can't use GeoSense.
Old question but here goes anyway:
I think your answer is not a problem with the sensor but with the scope of the watcher variable. You start the watcher then register for the event and then your watcher losses scope.
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
public string check(string stop)
{
watcher.Start();...

Simple C# Screen sharing application

I am looking to create a very basic screen sharing application in C#. No remote control necessary. I just want a user to be able to broadcast their screen to a webserver.
How should I implement this? (Any pointer in the right direction will be greatly appreciated).
It does NOT need to be high FPS. Would be sufficient to even update ever 5s or so. Do you think it would be sufficient to just upload a screenshot ever 5 seconds to my web server?
I previously blogged about how remote screen sharing software works here, it is not specific to C# but it gives a good fundamental understanding on the topic. Also linked in that article is the remote frame buffer spec which you'll also probably want to read up on.
Basically you will want to take screenshots and you can transmit those screenshots and display them on the other side. You can keep the last screenshot and compare the screenshot in blocks to see which blocks of the screenshot you need to send. You would typically do some sort of compression before sending the data.
To have remote control you can track mouse movement and transmit it and set the pointer position on the other end. Also ditto about keystrokes.
As far as compression goes in C#, you can simply use JpegBitmapEncoder to create your screenshots with Jpeg compression with the quality that you want.
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 40;
To compare file blocks you are probably best to create a hash on the old block and the new one, and then check to see if they are the same. You can use any hashing algorithm you want for this.
Here's code to take a screenshot, uncompressed as a bitmap:
public static Bitmap TakeScreenshot() {
Rectangle totalSize = Rectangle.Empty;
foreach (Screen s in Screen.AllScreens)
totalSize = Rectangle.Union(totalSize, s.Bounds);
Bitmap screenShotBMP = new Bitmap(totalSize.Width, totalSize.Height, PixelFormat.
Format32bppArgb);
Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP);
screenShotGraphics.CopyFromScreen(totalSize.X, totalSize.Y, 0, 0, totalSize.Size,
CopyPixelOperation.SourceCopy);
screenShotGraphics.Dispose();
return screenShotBMP;
}
Now just compress it and send it over the wire, and you're done.
This code combines all screens in a multiscreen setup into one image. Tweak as needed.
Well, it can be as simple as taking screenshots, compressing them, and then sending them over the wire. However, there is existing software that already does this. Is this for practice?
I'm looking to do something similar, and I just found this up on CodeProject. I think this will help you.
http://www.codeproject.com/Articles/371955/Motion-JPEG-Streaming-Server
The key player on sharing/replicating a screen is a COM Component called: RPDViewer
Add that com component to your window form and in References as well..
and thin add this code to your form load and you will get the screen replicated in your form:
using RDPCOMAPILib;
using System;
using System.Windows.Forms;
namespace screenSharingAttempt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
RDPSession x = new RDPSession();
private void Incoming(object Guest)
{
IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;
MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
}
//access to COM/firewall will prompt
private void button1_Click(object sender, EventArgs e)
{
x.OnAttendeeConnected += Incoming;
x.Open();
}
//connect
private void button2_Click(object sender, EventArgs e)
{
IRDPSRAPIInvitation Invitation = x.Invitations.CreateInvitation("Trial", "MyGroup", "", 10);
textBox1.Text = Invitation.ConnectionString;
}
//Share screen
private void button4_Click(object sender, EventArgs e)
{
string Invitation = textBox1.Text;// "";// Interaction.InputBox("Insert Invitation ConnectionString", "Attention");
axRDPViewer1.Connect(Invitation, "User1", "");
}
//stop sharing
private void button5_Click(object sender, EventArgs e)
{
axRDPViewer1.Disconnect();
}
}
}

Categories

Resources