I am doing a Bluetooth LE app on wp8.1 runtime app.
I have registered a value change event to listen when new data are coming.
heartRateMeasurementCharacteristic.ValueChanged += heartRateMeasurement_DataChanged;
private void heartRateMeasurement_DataChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
//do things
}
after a while the event is not triggered anymore, and rpgram throw NO exception(I checked runtime, memory etc). How can I debug this? Is that possible the event thread is down leaves without any exception?
under condition that BLE device is keep sending signal.
It's an old post, but the answer is to set heartRateMeasurementCharacteristic as a global variable.
Related
I'm working on a Windows forms application that needs to perform some logic before the PC goes to sleep. I've looked through many threads and found this which should work perfectly: Link. I can detect when the power is plugged/unplugged just fine, but I've run into serious problems when trying to detect a sleep/suspend event.
Using the logic mentioned, I have this section of code in my program:
public void powerModeChanged(object sender, PowerModeChangedEventArgs args)
{
if (args.Mode == PowerModes.Suspend)
{
Trace.WriteLine("Sleeping.....");
}
else if (args.Mode == PowerModes.StatusChange)
{
Trace.WriteLine("Other Status Change:");
}
}
public MainPage()
{
InitializeComponent();
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(powerModeChanged);
Per this documentation page - Link, there are 3 types of power modes. The statusChange is detected as expected when I unplug and replug the power adapter into my laptop, and prints to the debug Window just fine. However, it will not detect when I put the computer to sleep. After going over this for hours, my conclusion is that what the version of Windows 10 I'm running defines as "sleep" doesn't match up with the event that I'm checking for.
There is a comment on that initial thread in the first link that says the solution I tried doesn't seem to work with the "new Connected/Modern Standby modes" and provides a link to this thread: Link where it describes using the session switch event handler instead. This works on my laptop as my laptop locks upon sleep, but when testing on a Surface tablet (which is our target device for operation), it doesn't work due to the surface not locking upon sleep.
Of course, I could just set the device to lock on sleep, and that may end up being the only solution, but I wanted to see if there was something I was overlooking or any other way to check for sleep in modern versions of Windows. As it stands, I would hate for this important feature of the application depend on the system having to be setup to lock when sleeping. Thanks!
You could use the SystemEvents.PowerModeChanged event to detect when the computer is about to go to sleep. Within the event handler, you can perform any logic that needs to be done before the computer goes to sleep. For example, you could write commands to the windows registry or perform some other related tasks.
Below is an example of how this could be done:
// Register the SystemEvents.PowerModeChanged event handler
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
// Event handler for SystemEvents.PowerModeChanged
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
// Check if the computer is about to go to sleep
if (e.Mode == PowerModes.Suspend)
{
// Perform the logic that needs to be done before the computer goes to sleep
// ...
}
}
I ended up finding this thread, the answer on that thread solves this issue.
Link to Solution
I initialazed DeviceWatcher ... works fine, I add Honeywell Ring Scanner it raises event deviceWatcher. When I remove Honeywell USB Ring Scanner it raises event DeviceWatcher_Removed where I null ClaimedBarcodeScanner and BarcodeScanner object and DeviceWatcher_Updated where return status was STOP
After I connect Ring Scanner nothing happened in App. If I restart the app it's work until I disconnect and connect Ring Scanner.
I need to release BrcodeScanner from app.
I try on Honeywell D75e Win 10 iot and Honeywell Ring Scanner 8620903
I also try free memory ...
GC.Collect();
GC.WaitForPendingFinalizers();
I try to do Dispose of ClaimedBarcodeScanner
When you disconnect the device, it will raise the device removed event, but all pending operations need to be canceled properly, and all of the resources need to clean up. Please refer to following code in EventHandlerForDevice. The callback in the code is used to close the device explicitly is to clean up resources, to properly handle errors,and stop talking to the disconnected device.
private async void CloseCurrentlyConnectedDevice()
{
if (device != null)
{
// Notify callback that we're about to close the device
if (deviceCloseCallback != null)
{
deviceCloseCallback(this, deviceInformation);
}
// This closes the handle to the device
device.Dispose();
device = null;
}
}
My application needs to resume operations after the system wakes up after sleep. To do that, the app registers a PowerModeChanged event listener, waiting for PowerMode.Resume, where some bookkeeping needs to be done.
This code worked quite well with Windows 7, but with Windows 10 the PowerModeChanged does not fire until a key is pressed or mouse is moved.
public Shell()
{
Logging.LogMethodEntry();
try
{
InitializeComponent();
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}
catch (Exception exc)
{
Logging.LogException(exc);
}
Logging.LogMethodExit();
}
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Logging.LogMethodEntry(sender, e);
try
{
switch (e.Mode)
{
case PowerModes.StatusChange
case PowerModes.Resume:
Logging.LogInfo("Resuming...");
// some code here
break;
}
}
catch (Exception exc)
{
Logging.LogException(exc);
}
Logging.LogMethodExit();
}
I could not find any hint about any changes that were introduced in this mechanics for Windows 10, but maybe there is some settings I'm just not aware of...
The PowerModeChanged event relies upon the WM_POWERBROADCAST windows event. We note from the documentation for that event that there are two different resume events - PBT_APMRESUMEAUTOMATIC and PBT_APMRESUMESUSPEND.
We can see from the reference source that .NET only looks for the second of these events, not the first, and the second is defined only to fire
if the resume is triggered by user input, such as pressing a key.
Which appears to match your experience. You may wish to put your own message handler in place to watch for the first event. But note -
In Windows 10, version 1507 systems or later, if the system is resuming from sleep only to immediately enter hibernation, this event is not delivered. A WM_POWERBROADCAST message is not sent in this case.
I develop an application on windowCE 5.0 with opennetcf library.
I want to check WHEN my Device is connected to Cradle. It means I want to handle the event of plugged the device to cradle or other similar.
My purpose is that when Device is connected to Cradle, I disable all forms of my application,
and when it is removed from cradle, all the forms are enabled.
I search much. But the answer is not matched to my expect.
Please help me.
After reading reference of opennetcf, I found out the two events: ACPowerApplied and ACPowerRemoved
Here is my code:
public static event DeviceNotification ACPowerApplied;
public static event DeviceNotification ACPowerRemoved;
void Form1_ACPowerRemoved()
{
MessageBox.Show("Un-cradle");
}
void Form1_ACPowerApplied()
{
MessageBox.Show("Cradle");
}
private void Form1_Load(object sender, EventArgs e)
{
ACPowerApplied += new DeviceNotification(Form1_ACPowerApplied);
ACPowerRemoved += new DeviceNotification(Form1_ACPowerRemoved);
}
But the process did not step into Form1_ACPowerRemoved() and Form1_ACPowerApplied().
Is there any idea for that? Sorry for my poor English. Thank you in advance.
Your code is wrong. You've subscribed to the form's event, and nobody raise it.
Documentation doesn't show how-to-use code, I think. It shows declaraion.
Maybe it will work (not tested):
OpenNETCF.WindowsCE.DeviceManagement.ACPowerApplied += Form1_ACPowerApplied
OpenNETCF.WindowsCE.DeviceManagement.ACPowerRemoved += Form1_ACPowerRemoved
Also you can try to use WinAPI calls: http://blogs.msdn.com/b/davidklinems/archive/2005/02/10/370591.aspx
If you want, I have complete code, but there are a lot of waste and "OnRs232Connect" event.
By the way, what does "craddled" mean for you? Craddle can be disconneted from both AC and PC. I mean, do you want to handle when your device started to get electricity power, or when it started connecting to the PC via Active Sync? If the second, you want to catch "OnRs232Connect" event
I am using this simple way to detect network connect/disconnect events:
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
...
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
RaiseNewtorkChange();
}
static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
RaiseNewtorkChange();
}
static void RaiseNewtorkChange()
{
...
}
The problem is that sometimes NetworkAddressChanged event just stops working, after it was fired several times. Does anyone have an idea why this can happen?
Alternatively, is there another way to handle network connect/disconnect events, using C# or C/C++. Maybe there is such functionality in Windows API or WMI? I need notification on LAN/WiFi network connecting/disconnecting, without polling.
It seems to me that you need to use the Native Wifi API.
Take a look at the function WlanRegisterNotification, in particular this notification:
WLAN_NOTIFICATION_SOURCE_ACM: Registers for notifications generated by the auto configuration module.
Windows XP with SP3 and
Wireless LAN API for Windows XP with SP2: Only the
wlan_notification_acm_connection_complete and
wlan_notification_acm_disconnected notifications are available.
Disclaimer: I have used Native Wifi API in the past, but I have never used this particular feature.