private async void DevWatcher_DeviceAdded(object sender, NurDeviceWatcherInfo e) { // Create new api instance for known device NurApi api = new NurApi(); api.UserData = e; e.Tag = api; AttachNurApiEvents(api); // We must update the collection on the UI thread because the collection is databound to a UI element. await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Attempt to find known device with same address // This might be true, if known device is added manually NurDeviceWatcherInfo devToDelete = null; foreach (var dev in KnownDevices) { if (dev.Address == e.Address) { devToDelete = dev; break; } } if (devToDelete != null) { DevWatcher_DeviceRemoved(sender, devToDelete); } KnownDevices.Add(e); }); }
private async void DevWatcher_DeviceAddedAsync(object sender, NurDeviceWatcherInfo e) { // We must update the collection on the UI thread because the collection is databound to a UI element. await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { KnownDevices.Add(e); }); }
private async void DevWatcher_DeviceUpdatedAsync(object sender, NurDeviceWatcherInfo e) { // We must update the item on the UI thread because the item is databound to a UI element. await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Update listview item e.UpdateInfo(); }); }
private async void ConnButton_Click(object sender, RoutedEventArgs e) { // Disable buttons while (dis)connecting ConnButton.IsEnabled = false; InventoryButton.IsEnabled = false; try { if (mApi.IsConnected()) { // Disconnect ConnButton.Content = "Disconneting.."; await mApi.DisconnectAsync(); } else { // Connect NurDeviceWatcherInfo dev = null; try { dev = KnownDevices[ConnList.SelectedIndex]; } catch { } if (dev == null) { var dialog = new MessageDialog("Please select device to connect"); await dialog.ShowAsync(); return; } // Connect with device connection spec ConnButton.Content = "Conneting.."; mConnectedDev = dev; await mApi.ConnectAsync(dev.Spec); } } catch (Exception ex) { // Update UI MApi_DisconnectedEvent(mApi, new NurApi.NurEventArgs(0)); var dialog = new MessageDialog(ex.Message, "Connection Error"); await dialog.ShowAsync(); } ConnButton.IsEnabled = true; }
// This is called when NurApi is fully connected to device and ready to communicate private async void Api_ConnectedEvent(object sender, NurApi.NurEventArgs e) { System.Diagnostics.Debug.WriteLine("Api_ConnectedEvent"); NurApi connApi = sender as NurApi; App.NurApi = connApi; await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { NurDeviceWatcherInfo knownDev = connApi.UserData as NurDeviceWatcherInfo; // Save connect spec to settings //mLocalSettings.Values["devicespec"] = DeviceSpecStr; mLocalSettings.Values["devicespec"] = knownDev.SpecStr; // Update listview entry knownDev.ConnState = NurApiTransport.State.Connected; knownDev.UpdateInfo(); if (knownDev == SelectedDev) { ConnButtonText = "Disconnect"; } }); // Update reader info await Task.Run(async() => { try { NurApi.ReaderInfo rdrInfo = App.NurApi.GetReaderInfo(); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { ConnInfo = string.Format("Connected {0} | Serial {1} | FW {2}", rdrInfo.name, rdrInfo.serial, rdrInfo.GetVersionString()); }); } catch (Exception ex) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { ConnInfo = "Error"; await App.ShowException(ex); }); } }); }
private async void DevWatcher_DeviceRemoved(object sender, NurDeviceWatcherInfo e) { // Free api instance NurApi api = e.Tag as NurApi; if (api != null) { api.Dispose(); api = null; } // We must update the collection on the UI thread because the collection is databound to a UI element. await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { KnownDevices.Remove(e); }); }
// This is called when NurApi is fully disconnect (and released resources) private async void MApi_DisconnectedEvent(object sender, NurApi.NurEventArgs e) { // Update UI in UI thread await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Update listview item if (mConnectedDev != null) { mConnectedDev.ConnState = NurApiTransport.State.Disconnected; mConnectedDev.UpdateInfo(); mConnectedDev = null; } // Update UI InventoryButton.IsEnabled = false; ConnInfo.Text = "Disconnected"; ConnButton.Content = "Connect"; }); }
// This event gets called by NurApiTransport on connection state change. // NOTE: Transport level connection states are not same as NurApi Connected/Disconnected event states. private void Api_TransportConnStateChanged(object sender, EventArgs e) { NurApi api = sender as NurApi; System.Diagnostics.Debug.WriteLine("Api_TransportConnStateChanged " + api.Transport.ConnState); NurDeviceWatcherInfo info = api.UserData as NurDeviceWatcherInfo; // We're only interested about Connecting state if (api.Transport.ConnState == NurApiTransport.State.Connecting && info != null) { // Not awaited in purpose CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Update listview entry info.ConnState = api.Transport.ConnState; info.UpdateInfo(); }); } }
// This is called when NurApi is fully disconnect (and released resources) private async void Api_DisconnectedEvent(object sender, NurApi.NurEventArgs e) { NurApi connApi = sender as NurApi; System.Diagnostics.Debug.WriteLine("Api_DisconnectedEvent " + sender); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { NurDeviceWatcherInfo knownDev = connApi.UserData as NurDeviceWatcherInfo; // Update listview entry knownDev.ConnState = NurApiTransport.State.Disconnected; knownDev.UpdateInfo(); if (knownDev == SelectedDev) { // Set UI text's ConnButtonText = "Connect"; ConnInfo = "Disconnected"; } }); }