Detect changes in Wifi - c#

I need to detect when wifi is turned on/off. For that purpose I'm using Connectivity by James Montemagno, but the problem is that I don't get an ConnectivityChanged event if the Phone have access to mobile network and I turn on/off the wifi.
Here is the mapping of the event:
CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
WiFiConnected = CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi);
};
So can I detect Connectivity Changed on Wifi? I would like to do it in Xamarin Forms code so I won't have to implement a solution for each platform.

Here What Your Looking For
CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
if (args.IsConnected.ToString().Equals("False"))
{
if (CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
{
// WE LOST AN CONNECTION BUT WIFI IS STILL ON
}
}
else
{
if (CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
{
// WIFI WAS TURN ON AND WE HAVE A CONNECTION
}
else
{
// WE HAVE A CONNECTION BUT NOT WIFI
}
}
};

I dont know if there is a xamarin forms solution but you can do it plateform specific. In android with BroadcastReceiver.. for other plateforms i have no idea..

Related

windows universal app hardware backButton

I am creating a Windows universal app and I would like to implement some behavior for the (hardware)back button. I am able to do so for windows Phone using
'using Windows.Phone.UI.Input;'
and
'HardwareButtons.BackPressed += HardwareButtons_BackPressed;'
But can't seem to do it with the universal app, This behavior will happen on a page that is shared. How can I do this?
You can use SystemNavigationManager
SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
And have an event handler like this one:
currentView.BackRequested += CurrentView_BackRequested;
private void CurrentView_BackRequested(object sender,BackRequestedEventArgs e) {
e.Handled = true;
if(Frame.CanGoBack)
try { Frame.GoBack(); }
catch(Exception) { }
}
And to Visible Back Button at corner of ur app (Desktop mode) :
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;

What Triggers the ValueUpdated property in BLE C# code

I am building a Xamarin.Formscross platform mobile app, which uses Monkey.Robotics for its Bluetoth Low Energy functionality. I am connecting to an mbed based implimentation of a custom GATT service.
In the Xamarin C#, What triggers the Characteristic ValueUpdated event in Monkey.Robotics?
This is a standard example my C# is based on:
if (characteristic.CanUpdate) {
characteristic.ValueUpdated += (s, e) => {
Debug.WriteLine("characteristic.ValueUpdated");
Device.BeginInvokeOnMainThread( () => {
UpdateDisplay(characteristic);
});
IsBusy = false; // only spin until the first result is received
};
IsBusy = true;
characteristic.StartUpdates();
}
This has been working, but since I changed to my own custom GATT service which I am connecting to, the ValueUpdated event is never triggered. What is this event and how is it triggered? Is this a property read from the Characteristic, as set up by the mbed device, or is it something which the mobile end works out?
Thanks
this is from the Android implementation
this._gattCallback.CharacteristicValueUpdated += (object sender, CharacteristicReadEventArgs e) => {
// it may be other characteristics, so we need to test
if(e.Characteristic.ID == this.ID) {
// update our underlying characteristic (this one will have a value)
//TODO: is this necessary? probably the underlying reference is the same.
//this._nativeCharacteristic = e.Characteristic;
this.ValueUpdated (this, e);
}
};

Working with MIDI in Windows Store App (Win 8.1)

My goal is to receive MIDI messages in Windows Store Apps.
Microsoft has delivered an API called Microsoft.WindowsPreview.MidiRT (as a nuget package).
I managed to get a midi port, but MessageReceived event is not arised, although I'm pressing keys on my MIDI keyboard, and other MIDI programs show me that PC receives these messages.
Here is my code:
public sealed partial class MainPage : Page
{
private MidiInPort port;
public MainPage()
{
this.InitializeComponent();
DeviceWatcher watcher = DeviceInformation.CreateWatcher();
watcher.Updated += watcher_Updated;
watcher.Start();
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
port.Dispose();
}
async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(MidiInPort.GetDeviceSelector());
foreach (var item in deviceCollection)
{
Debug.WriteLine(item.Name);
if (port == null)
{
port = await MidiInPort.FromIdAsync(item.Id);
port.MessageReceived += port_MessageReceived;
}
}
}
void port_MessageReceived(MidiInPort sender, MidiMessageReceivedEventArgs args)
{
Debug.WriteLine(args.Message.Type);
}
}
Any ideas?
Possibly-related: Your device watcher code is not following the normal pattern. Here is what you need to do:
DeviceWatcher midiWatcher;
void MonitorMidiChanges()
{
if (midiWatcher != null)
return;
var selector = MidiInPort.GetDeviceSelector();
midiWatcher = DeviceInformation.CreateWatcher(selector);
midiWatcher.Added += (s, a) => Debug.WriteLine("Midi Port named '{0}' with Id {1} was added", a.Name, a.Id);
midiWatcher.Updated += (s, a) => Debug.WriteLine("Midi Port with Id {1} was updated", a.Id);
midiWatcher.Removed += (s, a) => Debug.WriteLine("Midi Port with Id {1} was removed", a.Id);
midiWatcher.EnumerationCompleted += (s, a) => Debug.WriteLine("Initial enumeration complete; watching for changes...");
midiWatcher.Start();
}
I managed to make it work. I've changed the platfrom to x64, and now it works (I used to build it for x86). There is still a problem though (and it is even bigger): I want to integrate this with Unity3d, but Unity3d doesn't allow to build x64 windows apps, on the other hand x86 MIDI build doesn't work on x64 machines.
Added:
Although this API depends on your architecture, a new Windows 10 api reportedly does not, so it should be simpler, if you target Win10.

Enumerating IPP Printers

I'm looking into DNS based service discovery in Windows 10 and found this video from Build. The only change I made to discovery was changing the service name to "_ipp._tcp". However I don't get any hits even though I know I have > 15 IPP enabled printers on the network (I can successfully identify these using the ipptool, IOS and Android code).
I've checked and double checked for typos. I've included all networking capabilities in the appxmanifest file.
Here's my code, pretty straight forward:
public sealed partial class MainPage : Page
{
static Guid DnsSdProtocol = new Guid("{4526e8c1-8aac-4153-9b16-55e86ada0e54}");
string queryString = "System.Devices.AepService.ProtocolId:={" + DnsSdProtocol + "} AND " +
"System.Devices.Dnssd.Domain:=\"local\" AND System.Devices.Dnssd.ServiceName:=\"_ipp._tcp\"";
DeviceWatcher watcher;
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
watcher = DeviceInformation.CreateWatcher(queryString,
new String[]
{
"System.Devices.Dnssd.HostName",
"System.Devices.Dnssd.ServiceName",
"System.Devices.Dnssd.TextAttributes",
"System.Devices.IpAddress" },
DeviceInformationKind.AssociationEndpointService
);
watcher.Added += Watcher_Added;
watcher.Start();
}
private void Watcher_Added(DeviceWatcher sender, DeviceInformation args)
{
System.Diagnostics.Debug.WriteLine("found device");
}
}
Does anybody else have experience with this and can help figure out why no devices are found? Is my query correct? Do I need to do anything else when setting up the DeviceWatcher?
update
I've verified the requests are being created since they are showing up in Wireshark. They look to be identical to other mdns requests being created. I've also verified I can create SSDP requests that return discovered devices so I doubt it's an issue with networking permissions via app capabilities.
Try using the watcher.Updated handler. Even if it is empty, the presence of an Updated handler can cause the Added handler to be triggered.

Event for Geolocation Changed in Windows Phone 8

Is there any event for the geolocation changed in Windows Phone 8 (C#)?
I want to trigger some event when the geolocation is changed like city from reversegeocoding changed.
If not, then is it possible to call some event manually whenever the Phone's location like City is Changed.
(suppose I have fetched the city using reverse geocoding). (for Windows Phone 8)
I can't understand your question properly, But this answer may solve your problem.
There is a event called "PositionChanged" in geolocator. This event gets triggered when geolocator position is changed.
geolocator = new Geolocator();
geolocator.PositionChanged -= geolocator_PositionChanged;
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00");
LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00");
});
}
for more info :::http://msdn.microsoft.com/en-us/library/windows/apps/jj247548(v=vs.105).aspx

Categories

Resources